【问题标题】:Tcomb-form-native: How to add new options to a form dynamicallyTcomb-form-native:如何动态向表单添加新选项
【发布时间】:2018-09-12 16:31:10
【问题描述】:

我的组件中有这个表单:

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
 });
 };

我试过了,但是没用。

【问题讨论】:

    标签: javascript react-native tcomb-form-native


    【解决方案1】:

    此代码适用于我,创建调用函数的结构表单模型,并在那里使用您的条件

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

    然后您创建 customFields 函数,例如,

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

    【讨论】:

    • 我正在尝试使用您的代码添加单个 t.Date 字段。你会怎么做?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多