Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Form composition. #18

Closed
stellarhoof opened this issue May 28, 2019 · 1 comment
Closed

Form composition. #18

stellarhoof opened this issue May 28, 2019 · 1 comment

Comments

@stellarhoof
Copy link
Member

stellarhoof commented May 28, 2019

Say you've got a couple forms form1 and form2 like the example in the readme:

let form = Form({
  fields: {
    email: {
      label: 'Email Address',
      rules: 'required|email'
    },
    password: {
      rules: 'required',
    },
  },
  submit: async snapshot => {
    await serviceCall(snapshot)
  },
  validate: validatorJS(V),
})

There's currently no way of composing them. Such composition should:

  • Return the same data structure as the form itself enriched with extra context, so the form would have to be "lifted" in monadic terms.
  • On submitting the entire form, carry over results from earlier forms, effectively making the composition a monad. This allows us to compose submits generically, since we can have a subform that depends on results from previous subforms.

Those are the harder ones. The semantics of composing reset, validate, etc... are trivial. We just iterate through the composed forms and call those methods in order.

@daedalus28
Copy link
Contributor

daedalus28 commented May 29, 2019

Just summarizing our off-github conversation: I'm not sure I see the value in this because it's trivial to compose fields (just use object spread) and submit methods are just functions (so you can compose them as you would other functions). The net savings would likely be a few lines of code but would come at the cost of introducing new concepts and API surface for form composition (instead of just regular functional/object composition).

With that said, we definitely should at least create a canonical example of doing this, even if there's no official API (like how focusing a field has a demo, but no underlying actual API is needed in this library).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants