React multi select 无法 select 负值

React multi select not able to select negative value

嗨,我正在使用 react multi select 负和正 values.When i select -1 它会自动更改为 1。因此无法 select -1。其他值工作正常。

 class FlavorForm extends React.Component {
  constructor(props) {
    super(props);
    this.state = {value: '1'};

    this.handleChange = this.handleChange.bind(this);
    this.handleSubmit = this.handleSubmit.bind(this);
  }

  handleChange(event) {
    this.setState({value: event.target.value});
  }

  handleSubmit(event) {
    alert('Your favorite flavor is: ' + this.state.value);
    event.preventDefault();
  }

  render() {
    return (
      <form onSubmit={this.handleSubmit}>
        <label>
          Pick your favorite flavor:
          <select value={this.state.value} onChange={this.handleChange} multiple>
            <option value="-1">Grapefruit</option>
            <option value="0">Lime</option>
            <option value="1">Coconut</option>
            <option value="2">Mango</option>
          </select>
        </label>
        <input type="submit" value="Submit" />
      </form>
    );
  }
}

ReactDOM.render(
  <FlavorForm />,
  document.getElementById('root')
);

请帮助如何 select -1 with react multi select.

Demo

我注意到我的控制台正在记录以下错误

Warning: The value prop supplied to must be an array if multiple is true.

我冒昧地修复了这个问题,巧合的是 -1 选择开始工作:

class FlavorForm extends React.Component {
  constructor (props) {
    super(props)
    this.state = { values: [] }

    this.handleChange = this.handleChange.bind(this)
    this.handleSubmit = this.handleSubmit.bind(this)
  }

  handleChange (event) {
    const values = this.state.values.includes(event.target.value)
      ? this.state.values
      : this.state.values.concat(event.target.value)
    this.setState({ values: values })
  }

  handleSubmit (event) {
    alert(`Your favorite flavor is: ${this.state.values}`)
    event.preventDefault()
  }

  render () {
    return (
      <form onSubmit={this.handleSubmit}>
        <label>
          Pick your favorite flavor:
          <select
            value={this.state.values}
            onChange={this.handleChange}
            multiple
          >
            <option value="-1">Grapefruit</option>
            <option value="0">Lime</option>
            <option value="1">Coconut</option>
            <option value="2">Mango</option>
          </select>
        </label>
        <input type="submit" value="Submit" />
      </form>
    )
  }
}

从 select 元素中删除值属性,它在那里无效。

<select onChange={this.handleChange} multiple>...</select>

您需要将值 属性 作为数组存储在状态中,即:

this.state = { 
  values: []
}

然后,将 handleChange 更改为:

handleChange(event) {
    let choices = event.target.selectedOptions;
    let final = [];
    for(let i=0; i<choices.length; i++) {
      final.push(choices[i].value);
    }
    this.setState({ value: final });
  }

它应该可以工作。

这是一个 link 的工作版本: https://codesandbox.io/s/p3705q0zm

干杯!

如果基于 documentation 的倍数为真,则使用数组作为值。 对于多选行为,请使用 .

中描述的选项

class FlavorForm extends React.Component {
  constructor(props) {
    super(props);
    this.state = {value: ['1']};

    this.handleChange = this.handleChange.bind(this);
    this.handleSubmit = this.handleSubmit.bind(this);
  }

  handleChange = ({target}) => {
    this.setState(prevState => ({
      value: [].filter.call(target.options, o => o.selected).map(o => o.value)
    }));
  }

  handleSubmit(event) {
    alert('Your favorite flavor is: ' + this.state.value);
    event.preventDefault();
  }

  render() {
    return (
      <form onSubmit={this.handleSubmit}>
        <label>
          Pick your favorite flavor:
          <select value={this.state.value} onChange={this.handleChange} multiple>
            <option value="-1">Grapefruit</option>
            <option value="0">Lime</option>
            <option value="1">Coconut</option>
            <option value="2">Mango</option>
          </select>
        </label>
        <input type="submit" value="Submit" />
      </form>
    );
  }
}

ReactDOM.render(
  <FlavorForm />,
  document.getElementById('root')
);
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
<div id="root"></div>