ant design select多选时,选项中有“全选”和其他选项互斥:
选了“全部”,选中项中就不能有省地市;选择省地市的时候,将“全部”从选中项中去除,只保留省地市。
const handleSelectAreas = (values) => {
let newVals = [];
if (values[values.length - 1]) { // 最后选择的不是“全部”
if (values.includes('')) { // values中已经包含“全部”
// 去除“全部”
newVals = values.filter(item => item);
} else {
newVals = [...values];
}
} else {
newVals = [''];
}
setAreaId(newVals);
};
<Select
mode="multiple"
options={areaList}
onChange={handleSelectAreas}
value={areaId}
/>