Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(TreeSelect): fix v-model error when lazy load and valueType="object" #4734

Merged
merged 4 commits into from
Nov 20, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading