Skip to content

Commit

Permalink
fix(TreeSelect): fix v-model error when lazy load and valueType="obje…
Browse files Browse the repository at this point in the history
…ct" (#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] <github-actions[bot]@users.noreply.github.com>
  • Loading branch information
ylunwang and github-actions[bot] authored Nov 20, 2024
1 parent 2b8206d commit 15bddf0
Showing 1 changed file with 34 additions and 33 deletions.
67 changes: 34 additions & 33 deletions src/tree-select/tree-select.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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');
};
Expand All @@ -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];
}
Expand Down Expand Up @@ -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<TreeSelectValue>).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<TreeOptionData>, 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;
}
Expand Down

0 comments on commit 15bddf0

Please sign in to comment.