Skip to content

Instantly share code, notes, and snippets.

@fakenickels
Last active July 30, 2020 18:22
Show Gist options
  • Save fakenickels/276df55e0f202eacf88793c1dd6ecc6c to your computer and use it in GitHub Desktop.
Save fakenickels/276df55e0f202eacf88793c1dd6ecc6c to your computer and use it in GitHub Desktop.
module type Config = BsReform.ReForm.Config;
open ReactNative;
type meta = | Money | Password | Text | Mask(string);
module Make = (Config: Config) => {
module Form = BsReform.ReForm.Make(Config);
[@react.component]
let make = (~schema, ~initialState, ~onSubmit) => {
open Form.Validation;
let reform: Form.api = Form.use(~schema, ~initialState, ~onSubmit, ());
let Schema(inner) = schema;
<View>
<Form.Provider value=reform>
{inner
->Belt.Array.map(validator => {
switch (validator) {
| IntMin({field, meta: Some(Money)}) =>
<Form.Field field render={api => {React.null}} />
| IntMin({field, meta: Some(Password)}) =>
<Form.Field field render={api => {
<Input
onChangeText={value => api.handleChange(int_of_string(value))}
value=string_of_int(api.value)
/>
}} />
| IntMax({field}) => React.null
| FloatMin({field}) => React.null
| FloatMax({field}) => React.null
| Email({field}) =>
<Form.Field field render={api => {
<Input
onChangeText={api.handleChange}
keyboardType=`emailAddress
value=(api.value)
/>
}} />
| StringNonEmpty({field}) => React.null
| StringRegExp({field}) => React.null
| StringMin({field}) => React.null
| StringMax({field}) => React.null
| Custom({field}) => React.null
| _ => React.null
}
})
->React.array}
</Form.Provider>
</View>;
};
include Form;
};
module StateLenses = [%lenses
type state = {
email: string,
password: string,
}
];
module Form = FastForm.Make(StateLenses);
[@react.component]
let make = () => {
<Form
schema=Form.Validation.(
Schema(
email(~meta=FastForm.Text, Email)
+ nonEmpty(~meta=FastForm.Password, ~error="Fill your password", Password),
)
)
onSubmit={_ => None}
initialState=StateLenses.{email: "", password: ""}
/>
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment