我想在我的下拉列表
中选择默认选项。当我添加选定选项但不使用默认选定选项呈现时,下面的代码有效:
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>
);
}
}
关于javascript - 语义 ui react 下拉列表中的默认选定选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45577092/