最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Creating dependant fields in Reactjs? - Stack Overflow

programmeradmin2浏览0评论

This is what i render

<select>
   <option>1</option>
   <option>2</option>
</select>

On selecting any of the options from the dropdown.I must render another dropdown list next to it.

<select>
   <option>1</option>
   <option>2</option>
</select>
<select>
   <option>1.1</option>
   <option>1.2</option>
</select>

then on selecting options from the second dropdown list.I must render input field of type text next to it.

How do i implement this in react?

var React = require('react');
var ReactDOM = require('react-dom');

var View = React.createClass({  

getInitialState: function() {
     return {
         value: '------'
     }
},
handleChange: function(event){
     this.setState({value: event.target.value});
     this.getFields(event.target.value);

},
handleClick : function(){

},
render : function(){
    return (<div>
            <p>
                <i className="plusSymbol fa fa-minus-circle"></i>
                <select onChange={this.handleChange} value={this.state.value} className="js-example-basic-single">
                <option value="">------</option>
                <option value="1">1</option>
                <option value="2">2</option>
                </select>
            </p>
            <p>
                <i onClick={this.handleClick} className="plusSymbol fa fa-plus-circle"></i>
                <span>Add a new condition</span>
            </p>
        </div>);
    }});
   module.exports = View;

So my view should look like this [Dropdown][Dropdown][Text Field]

This is what i render

<select>
   <option>1</option>
   <option>2</option>
</select>

On selecting any of the options from the dropdown.I must render another dropdown list next to it.

<select>
   <option>1</option>
   <option>2</option>
</select>
<select>
   <option>1.1</option>
   <option>1.2</option>
</select>

then on selecting options from the second dropdown list.I must render input field of type text next to it.

How do i implement this in react?

var React = require('react');
var ReactDOM = require('react-dom');

var View = React.createClass({  

getInitialState: function() {
     return {
         value: '------'
     }
},
handleChange: function(event){
     this.setState({value: event.target.value});
     this.getFields(event.target.value);

},
handleClick : function(){

},
render : function(){
    return (<div>
            <p>
                <i className="plusSymbol fa fa-minus-circle"></i>
                <select onChange={this.handleChange} value={this.state.value} className="js-example-basic-single">
                <option value="">------</option>
                <option value="1">1</option>
                <option value="2">2</option>
                </select>
            </p>
            <p>
                <i onClick={this.handleClick} className="plusSymbol fa fa-plus-circle"></i>
                <span>Add a new condition</span>
            </p>
        </div>);
    }});
   module.exports = View;

So my view should look like this [Dropdown][Dropdown][Text Field]

Share Improve this question asked Jan 27, 2016 at 8:01 LoneRangerLoneRanger 69313 silver badges32 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 6

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 <option key={i} value={el.value}>{el.label}</option>
    });
  },

  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 (
        <select onChange={this.handleSecondLevelChange} value={this.state.secondValue}>
        <option value="">---</option>
        {this.getOptions(this.state.firstValue)}
      </select>
    )
  },

  getThirdLevelField: function () {
    if (!this.state.secondValue) {
        return null;
    }

    return (
        <input type="text" onChange={this.handleThirdLevelChange} value={this.state.thirdValue} />
    )
  },
  render: function() {
    return (
    <div>
        <select onChange={this.handleFirstLevelChange} value={this.state.firstValue}>
        <option value="">---</option>
        <option value="1">1</option>
        <option value="2">2</option>
      </select>
      {this.getSecondLevelField()}
      {this.getThirdLevelField()}
    </div>
    );
  }
});

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

发布评论

评论列表(0)

  1. 暂无评论