Tcomb-form-native: How to add new options to a form dynamically

360 Views Asked by At

I have this form in my component :

this.state.customFieldsArray.length > 0 &&
          (
            <Form
              ref="customForm"
              type={this.customForm}
              options={this.customFormOptions}
            />
          )}

I want to add more options to the form (so it render more fields) when i click on a button. This is the methode that handle the click on the button:

  addCustomField = () => {
 let newFieldName = this.state.customFieldLabel;
 let newField = { newFieldName: "" };
 this.customFormOptions.fields[newFieldName] = {
  label: newFieldName
 };
  tempCustomFieldsArray.push(newField);
  this.setState({
   customFieldsArray: tempCustomFieldsArray
 });
 };

i have tried this but it didn't work.

1

There are 1 best solutions below

1
On

this code works for me, create struct form model that call function, and user your condition there

formModel: t.struct({
  customFields: this.getCustomFields(),
}),

then you create customFields function like,

  getCustomFields = () => {
    const customFields = {}
    //write your condition here
    return t.enums(customFields)
  }