rescriptbr / reform

📋 Reasonably making forms sound good
https://rescript-reform.netlify.app/
MIT License
353 stars 41 forks source link

Nested form fields? #210

Closed pheuter closed 2 years ago

pheuter commented 3 years ago
type address = {
  line1: string,
  line2: option<string>,
  zip: int,
}

module FormFields = %lenses(
  type state = {
    name: string,
    email: string,
    address: address,
  }
)

Is it possible for lenses to support nested records? It doesn't seem like it's possible to make it work with reform:

<input
  className="input"
  value={form.values.address.line2->Belt_Option.getWithDefault("")}
  onChange={ReForm.Helpers.handleChange(form.handleChange(FormFields.Address))}
  type_="text"
/>

Is this even a valid way to think about the use case? Should all form fields be top-level by design? Any guidance or feedback appreciated.

fakenickels commented 3 years ago

unfortunately it's not possible currently and we haven't thought of a good way to make it

buuut what we've been doing for is to treat nested data as nested forms, so we create a new "ReForm instance" for every

module AddressState = %lenses(
type state = {
  line1: string,
  line2: option<string>,
  zip: int,
}
)

module FormFields = %lenses(
  type state = {
    name: string,
    email: string,
    address: AddressState.state,
  }
)
pheuter commented 3 years ago

Thanks for the suggestion, makes sense.

vmarcosp commented 2 years ago

Can we close? @fakenickels