javascript - 语义 ui react 下拉列表中的默认选定选项

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

我想在我的下拉列表中选择默认选项。当我添加选定选项但不使用默认选定选项呈现时,下面的代码有效:

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>
        );
    }

但我收到以下警告:

警告: Prop 类型失败:提供给 Dropdown 的 Prop defaultSelectedLabel 无效。

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

如何在我的下拉菜单中获得默认选择的选项?

最佳答案

你离结果不远了。

您可以在 defaultValue 属性中提供一组值作为 the docs said .

defaultValue {number|string|arrayOf} Initial value or value array if multiple.

举个例子:

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

关于javascript - 语义 ui react 下拉列表中的默认选定选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45577092/

相关文章:

javascript - 类型错误 : SimpleSchema is not a constructor in Meteor 1. 6 项目

html - 语义用户界面 : Get clearing segment to expand to the height of it's contents

javascript - 切换 JQuery Accordion 的背景颜色

javascript - 我可以更改 javascript 数组项目中的属性 (JSON.stringify) 吗?

javascript - 将源更改为 <audio> html5 元素

javascript - 在用户输入不存在时隐藏元素

reactjs - 如何使用 react-select 在每个下拉项下自定义渲染潜台词?

javascript - 即使设置了 %PYTHON%,Node.js (npm) 也拒绝找到 python

javascript - 语义 UI 动态下拉菜单

javascript - react 语义 Ui 按钮悬停