From 15bddf07f383fa1d9c9ff23db7f6de1508e198f1 Mon Sep 17 00:00:00 2001 From: Ylun Wang Date: Wed, 20 Nov 2024 14:50:01 +0800 Subject: [PATCH] fix(TreeSelect): fix v-model error when lazy load and valueType="object" (#4734) * fix(TreeSelect): fix v-model error when lazy load and valueType="object" * chore: update common repo * chore: update common --------- Co-authored-by: github-actions[bot] --- src/tree-select/tree-select.tsx | 67 +++++++++++++++++---------------- 1 file changed, 34 insertions(+), 33 deletions(-) diff --git a/src/tree-select/tree-select.tsx b/src/tree-select/tree-select.tsx index f7326c98b1..855a9fb24e 100644 --- a/src/tree-select/tree-select.tsx +++ b/src/tree-select/tree-select.tsx @@ -198,7 +198,7 @@ export default defineComponent({ ) => { let current: TreeSelectValue = valueParam; if (isObjectValue.value) { - current = valueParam.map((nodeValue) => getTreeNode(props.data, nodeValue)); + current = valueParam.map(getNodeItem); } change(current, context.node, 'check'); }; @@ -221,7 +221,7 @@ export default defineComponent({ let current: TreeSelectValue = valueParam; if (isObjectValue.value) { const nodeValue = isEmpty(valueParam) ? '' : valueParam[0]; - current = getTreeNode(props.data, nodeValue); + current = getNodeItem(nodeValue); } else { current = isEmpty(valueParam) ? '' : valueParam[0]; } @@ -283,48 +283,49 @@ export default defineComponent({ const getSingleNodeInfo = () => { const nodeValue = isObjectValue.value ? (treeSelectValue.value as INodeOptions).value : treeSelectValue.value; - if (treeRef.value && (props.treeProps as TreeProps)?.load) { - if (!isEmpty(props.data)) { - const node = treeRef.value.getItem(nodeValue); - if (node) { - return { ...node.data, label: node.data[realLabel.value], value: node.data[realValue.value] }; - } - } - return { label: nodeValue, value: nodeValue }; - } - const node = getTreeNode(props.data, nodeValue); - if (!node) { - return { label: nodeValue, value: nodeValue }; - } - return node; + return getNodeItem(nodeValue); }; const getMultipleNodeInfo = () => { return (treeSelectValue.value as Array).map((value) => { const nodeValue = isObjectValue.value ? (value as INodeOptions).value : value; - if (treeRef.value && (props.treeProps as TreeProps)?.load) { - if (!isEmpty(props.data)) { - const node = treeRef.value.getItem(nodeValue); - if (node) { - return { ...node.data, label: node.data[realLabel.value], value: node.data[realValue.value] }; - } - } - return { label: nodeValue, value: nodeValue }; - } - const node = getTreeNode(props.data, nodeValue); - if (!node) { - return { label: nodeValue, value: nodeValue }; + return getNodeItem(nodeValue); + }); + }; + + const getNodeItem = (targetValue: TreeSelectValue) => { + if (treeRef.value) { + const node = treeRef.value.getItem(targetValue); + if (node) { + return { + ...node.data, + label: node.data[realLabel.value], + value: node.data[realValue.value], + }; } + } + const node = getTreeNode(props.data, targetValue); + if (node) { return node; - }); + } + return { + label: targetValue, + value: targetValue, + }; }; + const getTreeNode = (data: Array, targetValue: TreeSelectValue): TreeSelectValue | null => { for (let i = 0, len = data.length; i < len; i++) { - if (data[i][realValue.value] === targetValue) { - return { ...data[i], label: data[i][realLabel.value], value: data[i][realValue.value] }; + const item = data[i]; + if (item[realValue.value] === targetValue) { + return { + ...item, + label: item[realLabel.value], + value: item[realValue.value], + }; } - if (data[i]?.[realChildren.value]) { - const result = getTreeNode(data[i]?.[realChildren.value], targetValue); + if (item?.[realChildren.value]) { + const result = getTreeNode(item?.[realChildren.value], targetValue); if (!isNil(result)) { return result; }