Created
November 12, 2018 04:32
-
-
Save fakenickels/9ab0c5bf54f0bbde711db659ad30e75e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module FormConfig = { | |
type field(_) = | |
| Email: field(string) | |
| Age: field(int); | |
type state = { | |
email: string, | |
age: int, | |
}; | |
let get: type value. (state, field(value)) => value = | |
(state, field) => | |
switch (field) { | |
| Email => state.email | |
| Age => state.age | |
}; | |
let set: type value. (state, field(value), value) => state = | |
(state, field, value) => | |
switch (field) { | |
| Email => {...state, email: value} | |
| Age => {...state, age: value} | |
}; | |
}; | |
module SignUpForm = ReFormNext.Make(FormConfig); | |
let component = ReasonReact.statelessComponent("SignUp"); | |
let make = _children => { | |
...component, | |
render: _self => { | |
let getValue = event => ReactEvent.Form.target(event)##value; | |
<SignUpForm | |
initialState={email: "", age: 0} | |
schema={SignUpForm.Validation.Schema([|Email(Email), Min(Age, 18)|])}> | |
...{ | |
(~send, ~state) => | |
<div> | |
<input | |
value={state.values.email} | |
placeholder="Email" | |
onChange={e => send(FieldChangeValue(Email, e->getValue))} | |
/> | |
<input | |
value={string_of_int(state.values.age)} | |
placeholder="Your age" | |
onChange={ | |
e => | |
send(FieldChangeValue(Age, e->getValue->int_of_string)) | |
} | |
/> | |
</div> | |
} | |
</SignUpForm>; | |
}, | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment