Comments (3)
Sorry for the late response 😅
Thanks for using ReForm, FYI we are almost getting ready to launch ReFormNext as stable with a hooks API
from lenses-ppx.
Actually this is already ReForm does internally, the trick is to iterate over the validation schema!
https://github.com/Astrocoders/reform/blob/master/packages/bs-reform/src/ReFormNext.re#L57
To have a way to iterate over the list of GADTs directly is very cumbersome, see here https://discuss.ocaml.org/t/packing-gadt-constructors-in-iterable-data-structure/2678/4
from lenses-ppx.
I've been using this pattern:
let mergeWholeState =
(
formState,
~schema:
ReSchema.Make(OnboardingProfileFormTypes.StateLenses).Validation.schema(
option(ProfileFastForm.meta),
),
) => {
open OnboardingProfileFormTypes.StateLenses;
let Schema(schema) = schema;
let changeField = field => {
let currentState = currentWholeFormState->React.Ref.current;
let newState = currentState->set(field, formState->get(field));
currentWholeFormState->React.Ref.setCurrent(newState);
};
schema->Belt.Array.forEach(validator => {
switch (validator) {
| ProfileFastForm.Form.Validation.StringNonEmpty({field})
| ProfileFastForm.Form.Validation.StringRegExp({field})
| ProfileFastForm.Form.Validation.StringMin({field})
| ProfileFastForm.Form.Validation.StringMax({field})
| ProfileFastForm.Form.Validation.Email({field}) =>
let currentState = currentWholeFormState->React.Ref.current;
let newState =
currentState->set(field, formState->get(field)->Js.String.trim);
currentWholeFormState->React.Ref.setCurrent(newState);
| ProfileFastForm.Form.Validation.IntMin({field})
| ProfileFastForm.Form.Validation.IntMax({field}) => changeField(field)
| ProfileFastForm.Form.Validation.FloatMin({field})
| ProfileFastForm.Form.Validation.FloatMax({field}) =>
changeField(field)
| ProfileFastForm.Form.Validation.Custom({field}) => changeField(field)
| ProfileFastForm.Form.Validation.NoValidation({field}) =>
changeField(field)
}
});
Hooks.OnboardingFormData.persist(currentWholeFormState->React.Ref.current)
|> ignore;
};
though the list of validators is pretty huge it's mostly boilerplate and if you are using ocamllsp you can use the destruct
code action to generate it automatically for you
from lenses-ppx.
Related Issues (20)
- Takes ages to install HOT 4
- Add support for Windows HOT 1
- bsb -make-world + lenses-ppx fails in bs-platform 6.x
- Setup auto
- A warning is triggered when defining StateLenses with a single field
- Inspired by #10 HOT 2
- Fail to run demo HOT 3
- Support poly variants
- Add links to article explaining how GADTs work
- [Help] GLIBC_2.27 not found HOT 5
- Update GitHub Actions user commit
- Conflict in BS@9 when using @deriving(abstract): Error: Ppxlib.Deriving: 'abstract' is not a supported type deriving generator HOT 2
- Melange compatibility
- Building Ppx.exe HOT 1
- Build failure w/ ReScript v9 HOT 3
- Achieve composability
- Clean up code to use ppx helpers
- install fails due to `"ppx-flags": ["./ppx"]`
- Use esy HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from lenses-ppx.