Creating dependant fields in Reactjs?

前端 未结 1 1094
忘了有多久
忘了有多久 2021-02-06 06:43

This is what i render


         


        
1条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-02-06 06:48

    The following example should point you in the right direction...

    var Hello = React.createClass({
    
      getDefaultProps: function () {
        return {
          fieldMap: {
            "1": [
              { value: "1.1", label: "1.1" },
              { value: "1.2", label: "1.2" }
            ],
            "2": [
              { value: "2.1", label: "2.1" },
              { value: "2.2", label: "2.2" }
            ]
          }
        }
      },
    
      getInitialState: function() {
         return {
             firstValue: '',
             secondValue: '',
             thirdValue: ''
         }
      },
    
      getOptions: function (key) {    
        if (!this.props.fieldMap[key]) {
          return null;
        }
    
        return this.props.fieldMap[key].map(function (el, i) {
            return 
        });
      },
    
      handleFirstLevelChange: function (event) {
        this.setState({
          firstValue: event.target.value,
          secondValue: '',
          thirdValue: ''
        });
      },
    
      handleSecondLevelChange: function (event) {
        this.setState({
            secondValue: event.target.value,
          thirdValue: ''
        });
      },
    
      handleThirdLevelChange: function (event) {
          this.setState({
            thirdValue: event.target.value
        });
      },
    
      getSecondLevelField: function () {
        if (!this.state.firstValue) {
            return null;
        }
    
        return (
            
        )
      },
    
      getThirdLevelField: function () {
        if (!this.state.secondValue) {
            return null;
        }
    
        return (
            
        )
      },
      render: function() {
        return (
        
    {this.getSecondLevelField()} {this.getThirdLevelField()}
    ); } });

    See it live on https://jsfiddle.net/27u9Lw4t/1/

    0 讨论(0)
提交回复
热议问题