优化Select
parent
4ddef4178d
commit
f98dbd9a85
|
|
@ -15,6 +15,8 @@ export interface BasicSelectProps extends SelectProps {
|
|||
placeholder?: string;
|
||||
/** 获取 label */
|
||||
onGetLabel?: (label: string | string[]) => void;
|
||||
/** 获取选中的项 */
|
||||
onGetOption?: (option: Record<string, any> | Record<string, any>[]) => void;
|
||||
/** 获取数据 */
|
||||
onGetData?: (data: Record<string, any>[]) => void;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,5 @@
|
|||
import { Select } from "antd";
|
||||
import { useEffect } from "react";
|
||||
import { getLabelName } from "../../../utils";
|
||||
|
||||
/**
|
||||
* 基础下拉组件(不建议直接使用此组件,二次继承使用)
|
||||
|
|
@ -10,6 +9,7 @@ function BasicSelect(props) {
|
|||
onGetData,
|
||||
onChange,
|
||||
onGetLabel,
|
||||
onGetOption,
|
||||
placeholder = "",
|
||||
data = [],
|
||||
nameKey = "name",
|
||||
|
|
@ -20,21 +20,33 @@ function BasicSelect(props) {
|
|||
const handleChange = (event, option) => {
|
||||
if (Array.isArray(event)) {
|
||||
if (event.length > 0) {
|
||||
const name = [];
|
||||
const findItems = [];
|
||||
const names = [];
|
||||
event.forEach((item) => {
|
||||
name.push(getLabelName({ list: data, status: item, idKey, nameKey }));
|
||||
const findItem = data.find(dataItem => dataItem[idKey] === item);
|
||||
if (findItem) {
|
||||
findItems.push(findItem);
|
||||
names.push(findItem[nameKey]);
|
||||
}
|
||||
});
|
||||
onGetLabel?.(name);
|
||||
onGetLabel?.(names);
|
||||
onGetOption?.(findItems);
|
||||
}
|
||||
else {
|
||||
onGetLabel?.([]);
|
||||
onGetOption?.([]);
|
||||
}
|
||||
}
|
||||
else {
|
||||
if (event)
|
||||
onGetLabel?.(getLabelName({ list: data, status: event, idKey, nameKey }));
|
||||
else
|
||||
if (event) {
|
||||
const findItem = data.find(item => item[idKey] === event);
|
||||
onGetLabel?.(findItem[nameKey]);
|
||||
onGetOption?.(findItem);
|
||||
}
|
||||
else {
|
||||
onGetLabel?.("");
|
||||
onGetOption?.({});
|
||||
}
|
||||
}
|
||||
onChange?.(event, option);
|
||||
};
|
||||
|
|
|
|||
Loading…
Reference in New Issue