gcanti / tcomb-form

Forms library for react
https://gcanti.github.io/tcomb-form
MIT License
1.16k stars 136 forks source link

Options for nested fields #250

Closed monarchwadia closed 8 years ago

monarchwadia commented 8 years ago

It would be great to be able to render options like help text for nested fields (for example, for the fields in a struct passed into a t.list)

Something like the following would be cool; Note though that I'm not sure that the structure below could be used for multiple nesting levels.

var FormStruct = t.struct({
    formField1: t.list(t.struct({
        foo: t.String,
        bar: t.String,
        baz: t.maybe(t.String)
    }))
});

var options = {
    fields: {
        formField1: {
            foo: {
                help: 'Foo refers to something'
            },
            bar: {
                help: 'Bar refers to something else'
            },
            baz: {
                help: 'Baz is rarely used'
            }
        }
    }
}
gcanti commented 8 years ago

Actually you can nest at arbitrary level:

var options = {
  fields: { // <= first level of nesting: struct
    formField1: {
      item: { // <= second level of nesting: list
        fields: { // <= third level of nesting: struct
          foo: {
            help: 'Foo refers to something'
          },
          bar: {
            help: 'Bar refers to something else'
          },
          baz: {
            help: 'Baz is rarely used'
          }
        }
      }
    }
  }
};
asbjornenge commented 8 years ago

@gcanti I have a similar problem. I have to modify the options of bar based on what is selected in foo individually for each item in the formField1 list. Is that possible atm.? Using item I can modify all the items in the list, but not a particular one, or?

gcanti commented 8 years ago

@asbjornenge

Is that possible atm?

In general and with the current API I'd say no, however this is an example with an hacky solution: https://github.com/gcanti/tcomb-form/issues/238 maybe can be useful in your use case

asbjornenge commented 8 years ago

@gcanti worked very well, thanks a bunch! :smile:

gcanti commented 8 years ago

:+1: