semantic ui在下拉列表中反应默认选定的选项

时间:2017-08-08 19:58:55

标签: javascript reactjs semantic-ui semantic-ui-react

我想在dropdown中选择默认选项。当我添加所选选项但不使用默认选定选项进行渲染时,以下代码有效:

render() {
        return (
            <Form onSubmit={this.handleFormSubmit}>
                <Form.Dropdown
                  placeholder="Select Options"
                  fluid multiple selection
                  options={this.state.options}
                  onChange={this.handleMultiChange}
                />
                <Button type="submit">Submit</Button>
            </Form>
        );
    }

我尝试添加defaultSelectedLabel={this.state.selected}

this.state.selected是一个选项数组,默认选择的值为true:

render() {
        return (
            <Form onSubmit={this.handleFormSubmit}>
                <Form.Dropdown
                  placeholder="Select Options"
                  fluid multiple selection
                  options={this.state.options}
                  onChange={this.handleMultiChange}
                  defaultSelectedLabel={this.state.selected}
                />
                <Button type="submit">Submit</Button>
            </Form>
        );
    }

但我收到以下警告:

Warning: Failed prop type: Invalid prop defaultSelectedLabel supplied to Dropdown.

我对defaultValue道具做了同样的事情,但得到了同样的错误

如何在dropdown中找到默认选定的选项?

2 个答案:

答案 0 :(得分:13)

你离结果不远。

您可以在defaultValue道具中提供一系列值the docs said

  

defaultValue {number | string | arrayOf}初始值或值数组(如果是多个)。

这是一个例子:

class YourComponent extends Component {
  componentWillMount() {
    this.setState({
      options: [
        {value:'1', text:'A'},
        {value:'2', text:'B'},
        {value:'3', text:'C'},
      ],
      selected: ['1', '2'], // <== Here, the values of selected options
    });
  }

  ...

  render() {
    return (
      <Form onSubmit={this.handleFormSubmit}>
        <Form.Dropdown
          placeholder="Select Options"
          fluid multiple selection
          options={this.state.options}
          onChange={this.handleMultiChange}
          defaultValue={this.state.selected} // <== here the default values
        />
        <Button type="submit">Submit</Button>
      </Form>
    );
  }
}

编辑Here is a live example

答案 1 :(得分:0)

也适用于单个选择:

California