问题描述
在这里,我使用了disablePreSelectedValues属性来禁用选定的值。但在下拉菜单中显示了所有选项,并且可以取消选中该选项。知道如何禁用复选框吗?
<Multiselect
options={this.state.options} // Options to display in the dropdown
selectedValues={this.state.selectedValue} // Preselected value to persist in dropdown
onSelect={this.onSelect} // Function will trigger on select event
onRemove={this.onRemove} // Function will trigger on remove event
displayValue="name" // Property name to display in the dropdown options
showCheckBox={true}
disablePreSelectedValues={this.state.disablePreSelectedValues} />
解决方法
根据此处的源代码:
isDisablePreSelectedValues(value) {
const { isObject,disablePreSelectedValues,displayValue } = this.props;
const { preSelectedValues } = this.state;
if (!disablePreSelectedValues || !preSelectedValues.length) {
return false;
}
if (isObject) {
return (
preSelectedValues.filter(i => i[displayValue] === value[displayValue])
.length > 0
);
}
return preSelectedValues.filter(i => i === value).length > 0;
}
如果要禁用它们,还需要传递preSelectedValues数组。尝试以下操作:
<Multiselect
options={this.state.options} // Options to display in the dropdown
selectedValues={this.state.selectedValue} // Preselected value to persist in dropdown
onSelect={this.onSelect} // Function will trigger on select event
onRemove={this.onRemove} // Function will trigger on remove event
displayValue="name" // Property name to display in the dropdown options
showCheckbox={true}
disablePreSelectedValues={this.state.disablePreSelectedValues}
preSelectedValues={selectedValues}
/>