refactor(form): 优化选择类组件的渲染方式

- 将 Select、Radio、Checkbox 组件的 options 属性改为直接传入选项数组,简化子组件结构
- Select 组件更改 showSearch 的 optionFilterProp 为 label,提升搜索匹配准确度
- Basic Select 组件同样改用 options 属性传递选项,移除手动渲染 Option 子组件
- SelectCreate 组件改为使用 options 传递选项,删除手动渲染逻辑
- MapSelector 中的固定区域选择改为使用 options 传递选项,代码更简洁易读
2.0
LiuJiaNan 2026-07-07 13:53:50 +08:00
parent 3903492847
commit 487cdd142a
4 changed files with 47 additions and 76 deletions

View File

@ -302,64 +302,38 @@ const FormItemsRenderer = ({
case FORM_ITEM_RENDER_ENUM.SELECT: case FORM_ITEM_RENDER_ENUM.SELECT:
return ( return (
<Select placeholder={placeholder} showSearch={{ optionFilterProp: "children" }} allowClear {...componentProps}> <Select
{(option.items || []).map((item) => { placeholder={placeholder}
const { value, label, disabled } = getSelectableItemAttributes(item, itemsFieldKey); showSearch={{ optionFilterProp: "label" }}
return ( allowClear
<Select.Option key={value} value={value} disabled={disabled}> options={(option.items || []).map(item => getSelectableItemAttributes(item, itemsFieldKey))}
{label} {...componentProps}
</Select.Option> />
);
})}
</Select>
); );
case FORM_ITEM_RENDER_ENUM.RADIO: case FORM_ITEM_RENDER_ENUM.RADIO:
return ( return <Radio.Group options={(option.items || []).map(item => getSelectableItemAttributes(item, itemsFieldKey))} {...componentProps} />;
<Radio.Group {...componentProps}>
{(option.items || []).map((item) => {
const { value, label, disabled } = getSelectableItemAttributes(item, itemsFieldKey);
return (
<Radio key={value} value={value} disabled={disabled}>
{label}
</Radio>
);
})}
</Radio.Group>
);
case FORM_ITEM_RENDER_ENUM.CHECKBOX: case FORM_ITEM_RENDER_ENUM.CHECKBOX: {
const selectableOptions = (option.items || []).map(item => getSelectableItemAttributes(item, itemsFieldKey));
// checkboxCol 时需要栅格布局Checkbox.Group 的 options 不支持列布局,此处使用 children + Row/Col 保持原有行为
if (option.checkboxCol) {
return ( return (
<Checkbox.Group {...componentProps}> <Checkbox.Group {...componentProps}>
{
option.checkboxCol
? (
<Row> <Row>
{(option.items || []).map((item) => { {selectableOptions.map(({ value, label, disabled }) => (
const { value, label, disabled } = getSelectableItemAttributes(item, itemsFieldKey);
return (
<Col span={option.checkboxCol} key={value}> <Col span={option.checkboxCol} key={value}>
<Checkbox value={value} disabled={disabled}> <Checkbox value={value} disabled={disabled}>
{label} {label}
</Checkbox> </Checkbox>
</Col> </Col>
); ))}
})}
</Row> </Row>
)
: (
(option.items || []).map((item) => {
const { value, label, disabled } = getSelectableItemAttributes(item, itemsFieldKey);
return (
<Checkbox key={value} value={value} disabled={disabled}>
{label}
</Checkbox>
);
})
)
}
</Checkbox.Group> </Checkbox.Group>
); );
}
return <Checkbox.Group options={selectableOptions} {...componentProps} />;
}
case FORM_ITEM_RENDER_ENUM.DATE: case FORM_ITEM_RENDER_ENUM.DATE:
return <DatePicker placeholder={placeholder} format="YYYY-MM-DD" style={{ width: "100%" }} {...componentProps} />; return <DatePicker placeholder={placeholder} format="YYYY-MM-DD" style={{ width: "100%" }} {...componentProps} />;

View File

@ -286,10 +286,12 @@ const MapSelector = (props) => {
<Row gutter={24}> <Row gutter={24}>
<Col span={12}> <Col span={12}>
<Form.Item label="所属区域"> <Form.Item label="所属区域">
<Select value={currentArea} onChange={e => setCurrentArea(e)} allowClear> <Select
<Select.Option value="1">东港区</Select.Option> value={currentArea}
<Select.Option value="2">西港区</Select.Option> onChange={e => setCurrentArea(e)}
</Select> allowClear
options={[{ value: "1", label: "东港区" }, { value: "2", label: "西港区" }]}
/>
</Form.Item> </Form.Item>
</Col> </Col>
</Row> </Row>

View File

@ -59,17 +59,17 @@ function BasicSelect(props) {
}, [data]); }, [data]);
return ( return (
<Select placeholder={`请选择${placeholder}`} showSearch={{ optionFilterProp: "children" }} allowClear onChange={handleChange} {...restProps}> <Select
{data.map((item) => { placeholder={`请选择${placeholder}`}
const value = item[idKey]; showSearch={{ optionFilterProp: "children" }}
const label = labelRender ? labelRender(item) : item[nameKey]; allowClear
return ( onChange={handleChange}
<Select.Option key={value} value={value}> options={data.map(item => ({
{label} value: item[idKey],
</Select.Option> label: labelRender ? labelRender(item) : item[nameKey],
); }))}
})} {...restProps}
</Select> />
); );
} }

View File

@ -39,14 +39,9 @@ function SelectCreate(props) {
} }
</div> </div>
)} )}
options={items.map(item => ({ value: item.id, label: item.name }))}
{...restProps} {...restProps}
> />
{
items.map(item => (
<Select.Option value={item.id} key={item.id}>{item.name}</Select.Option>
))
}
</Select>
); );
} }