From 80f4528a886d32c9d019771bb5d529b10ecd1320 Mon Sep 17 00:00:00 2001 From: LiuJiaNan <15703339975@163.com> Date: Thu, 6 Nov 2025 16:28:49 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96Select=E3=80=81SelectTree?= =?UTF-8?q?=E3=80=81Cascader?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- components/Cascader/Basic/index.js | 2 +- components/Select/Basic/index.d.ts | 2 ++ components/Select/Basic/index.js | 13 ++----------- components/SelectTree/Basic/index.d.ts | 4 +++- components/SelectTree/Basic/index.js | 16 +++++++++++----- 5 files changed, 19 insertions(+), 18 deletions(-) diff --git a/components/Cascader/Basic/index.js b/components/Cascader/Basic/index.js index 5f45247..eaa2f58 100644 --- a/components/Cascader/Basic/index.js +++ b/components/Cascader/Basic/index.js @@ -33,7 +33,7 @@ function BasicCascader(props) { if (!onGetNodePathsIsIncludeOneself && selectedOptions) { nodePaths = selectedOptions.slice(0, -1); } - return nodePaths; + return nodePaths || []; }; const handleChange = (value, selectedOptions) => { diff --git a/components/Select/Basic/index.d.ts b/components/Select/Basic/index.d.ts index 0ca87a5..b052c20 100644 --- a/components/Select/Basic/index.d.ts +++ b/components/Select/Basic/index.d.ts @@ -13,6 +13,8 @@ export interface BasicSelectProps extends SelectProps { idKey?: string; /** 占位符 */ placeholder?: string; + /** 获取 label */ + onGetLabel?: (label: string) => void; } /** diff --git a/components/Select/Basic/index.js b/components/Select/Basic/index.js index a10297e..7879b50 100644 --- a/components/Select/Basic/index.js +++ b/components/Select/Basic/index.js @@ -14,23 +14,14 @@ function BasicSelect(props) { ...restProps } = props; - // const handleSelect = (event) => { - // onGetLabel?.(getLabelName({ list: data, status: event, idKey, nameKey })); - // }; - // - // const handleClear = () => { - // onGetLabel?.(undefined); - // }; - const handleChange = (event) => { if (event) onGetLabel?.(getLabelName({ list: data, status: event, idKey, nameKey })); else - onGetLabel?.(undefined); - } + onGetLabel?.(""); + }; return ( - // {data.map((item) => { const value = item[idKey]; diff --git a/components/SelectTree/Basic/index.d.ts b/components/SelectTree/Basic/index.d.ts index ec82365..6f04401 100644 --- a/components/SelectTree/Basic/index.d.ts +++ b/components/SelectTree/Basic/index.d.ts @@ -19,6 +19,8 @@ export interface BasicSelectTreeProps extends TreeSelectProps { placeholder?: string; /** 控制只能选择到第几级 */ level?: number; + /** 获取 label */ + onGetLabel?: (label: string) => void; } /** @@ -26,4 +28,4 @@ export interface BasicSelectTreeProps extends TreeSelectProps { */ declare const BasicSelectTree: FC; -export default BasicSelectTree; \ No newline at end of file +export default BasicSelectTree; diff --git a/components/SelectTree/Basic/index.js b/components/SelectTree/Basic/index.js index 7525667..979e4b9 100644 --- a/components/SelectTree/Basic/index.js +++ b/components/SelectTree/Basic/index.js @@ -6,7 +6,8 @@ import { getTreeNodePaths, processTreeDataByLevel } from "../../../utils"; */ function BasicSelectTree(props) { const { - onSelect, + onChange, + onGetLabel, onGetNodePaths, onGetNodePathsIsIncludeOneself = true, placeholder = "", @@ -28,8 +29,8 @@ function BasicSelectTree(props) { }) : treeData; - const handleSelect = (value, node, extra) => { - if (value.length > 0) { + const handleChange = (value, label, extra) => { + if (value) { const parentNodes = getTreeNodePaths({ data: treeData, targetId: value, @@ -38,8 +39,13 @@ function BasicSelectTree(props) { isIncludeOneself: onGetNodePathsIsIncludeOneself, }); onGetNodePaths?.(parentNodes); + onGetLabel?.(parentNodes[parentNodes.length - 1][nameKey]); } - onSelect?.(value, node, extra); + else { + onGetNodePaths?.([]); + onGetLabel?.(""); + } + onChange?.(value, label, extra); }; return ( @@ -50,7 +56,7 @@ function BasicSelectTree(props) { popup: { root: { maxHeight: 400, overflow: "auto" } }, }} placeholder={`请选择${placeholder}`} - onSelect={handleSelect} + onChange={handleChange} allowClear treeData={processedTreeData} fieldNames={{ label: nameKey, value: idKey, children: childrenKey }}