Tcomb-form-native: Как динамически добавлять новые параметры в форму

У меня есть эта форма в моем компоненте:

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

Я хочу добавить больше параметров в форму (чтобы она отображала больше полей), когда я нажимаю на кнопку. Это метод, который обрабатывает нажатие на кнопку:

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

Я попробовал это, но это не сработало.

0 ответов

Этот код работает для меня, создайте модель структурной формы, которая вызывает функцию, и используйте свое состояние там

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

затем вы создаете функцию customFields, например,

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

Другие вопросы по тегам