Giter VIP home page Giter VIP logo

Comments (3)

fakenickels avatar fakenickels commented on July 23, 2024 1

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.

fakenickels avatar fakenickels commented on July 23, 2024

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.

fakenickels avatar fakenickels commented on July 23, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.