简体   繁体   English

渲染条件表单元素

[英]Render a conditional form element

I have a select (aka picklist) field on a form . 我在表单上有一个选择(又名选择列表)字段。 Based on its value, it affects the next field (dependent field), which is either: another select, input type text, or disabled input. 基于其值,它将影响下一个字段(从属字段),该字段可以是:另一个选择,输入类型文本或禁用的输入。

The pseudocode 伪码

// based on a state value, leadField, determine valueField

// if leadField === null, return a disabled input
// else, go through the array of leadField values

// if a leadFieldValue === leadField
// then go through leadFieldValue

// if leadFieldValue.pickListValues is not empty
// render the select options
// else render an input type text

The code 编码

  renderValueField() {
    if(this.state.leadField === null) {
            return <input type="text" id="input1" className="slds-input" disable/>
    } else {
      return this.props.territoryCriteriaFields.map((criteriaField) => {
        const shouldRender = criteriaField.name === this.state.leadField;
        if (shouldRender) {
          if (typeof criteriaField.pickListValues !== 'undefined' && criteriaField.pickListValues.length > 0) {
            return criteriaField.picklistValues.map((option) => {
                return (
                  <option value={option.value}>{option.label}</option>
                );
            });
          } else {
            return <input type="text" id="input1" className="slds-input" />
          }
        } 
      });      
    }
  }

My problem: when I call the above {this.renderValueField} on the page, it needs to be between <select> 's when pickListValues !== null , like so 我的问题:当我在页面上调用上面的{this.renderValueField}时,它必须在pickListValues !== null <select>之间pickListValues !== null

<select>
 {this.renderValueField()}
</select>

but I need the <selects> 's to not be there if an input is rendered. 但是如果呈现输入,我需要<selects>不在那里。

tl;dr - conditionally add remove the <select> tags, depending on the return values of my renderValueField() tl; renderValueField()根据我的renderValueField()的返回值,有条件地添加删除<select>标记

You can wrap the <option> s in a <select> inside the function: 您可以将<option>包裹在函数内的<select>

renderValueField() {
  if(this.state.leadField === null) {
    return <input type="text" id="input1" className="slds-input" disable/>
  } else {
    return this.props.territoryCriteriaFields.map((criteriaField) => {
      const shouldRender = criteriaField.name === this.state.leadField;
      if (shouldRender) {
        if (typeof criteriaField.pickListValues !== 'undefined' && criteriaField.pickListValues.length > 0) {
          return (
            <select>
              {criteriaField.picklistValues.map((option) => {
                return (
                  <option value={option.value}>{option.label}</option>
                );
              })}
            </select>
          );
        } else {
          return <input type="text" id="input1" className="slds-input" />
        }
      } 
    });      
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM