open Prop;
let handleChange = (handleChange, event) =>
handleChange(ReactEvent.Form.target(event)##value);
type onChange = option(Value.t) => unit;
module ConditionalReducer = {
type action =
| ADD_OR_UPDATE_CONDITIONAL(Value.conditional)
| REMOVE_CONDITIONAL(Value.conditional);
let reducer = (items: array(Value.conditional), action: action) =>
switch (action) {
| ADD_OR_UPDATE_CONDITIONAL(conditional) =>
items->E.A.hasBy(c => c.name == conditional.name)
? items
|> E.A.fmap((r: Value.conditional) =>
r.name == conditional.name ? conditional : r
)
: E.A.append(items, [|conditional|])
| REMOVE_CONDITIONAL(conditional) =>
items
|> E.A.filter((c: Value.conditional) => c.name != conditional.name)
};
};
[@react.component]
let make =
(
~type_: TypeWithMetadata.t,
~value: option(Value.t),
~onChange: onChange,
) => {
switch (type_.type_, value) {
| (Conditionals(l), Some(ConditionalArray(n))) =>
{n
|> E.A.fmap((r: Value.conditional) =>
onChange(
Some(
Value.ConditionalArray(
ConditionalReducer.reducer(
n,
REMOVE_CONDITIONAL({name: r.name, truthValue: true}),
),
),
),
)
}>
{r.name |> ReasonReact.string}
{(r.truthValue ? "TRUE" : "FALSE") |> ReasonReact.string}
)
|> ReasonReact.array}
{l.options
|> E.A.fmap(r =>
{r |> ReasonReact.string}
)
|> ReasonReact.array}
| (Conditionals(l), _) =>
l.options |> E.A.fmap(r => r |> ReasonReact.string) |> ReasonReact.array
| (Year(_), Some(FloatPoint(r))) =>
Js.Float.toString}
onChange={handleChange(r =>
switch (Js.Float.fromString(r)) {
| r => onChange(Some(Value.FloatPoint(r)))
}
)}
/>
| (FloatPoint(_), Some(FloatPoint(r))) =>
Js.Float.toString}
onChange={handleChange(r =>
switch (Js.Float.fromString(r)) {
| r => onChange(Some(Value.FloatPoint(r)))
}
)}
/>
| (BinaryConditional, Some(BinaryConditional(r))) =>
switch (r) {
| Unselected =>
onChange(Some(BinaryConditional(Selected(true))))}>
{"Select" |> ReasonReact.string}
| Selected(true) =>
{"YES!" |> ReasonReact.string}
onChange(Some(BinaryConditional(Selected(false))))}>
{"No" |> ReasonReact.string}
onChange(Some(BinaryConditional(Unselected)))}>
{"Deselect" |> ReasonReact.string}
| Selected(false) =>
{"NO!" |> ReasonReact.string}
onChange(Some(BinaryConditional(Selected(true))))}>
{"Yes" |> ReasonReact.string}
onChange(Some(BinaryConditional(Unselected)))}>
{"Deselect" |> ReasonReact.string}
}
| (Year(_), _)
| (FloatPoint(_), _) =>
| (SelectSingle(t), Some(SelectSingle(r))) =>
| (DateTime(_), Some(DateTime((d: MomentRe.Moment.t)))) =>
onChange(
Some(Value.DateTime(MomentRe.momentWithFormat(r, "YYYY-MM-DD"))),
)
)}
/>
};
};