Skip to content

Commit

Permalink
fix(cascader): logical error in setting and verifying value
Browse files Browse the repository at this point in the history
  • Loading branch information
betavs committed Dec 27, 2024
1 parent b27a222 commit aac22bb
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
11 changes: 6 additions & 5 deletions src/cascader/core/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export function getSingleContent(cascaderContext: CascaderContextType): string {
}
const path = node && node[0].getPath();
if (path && path.length) {
return showAllLevels ? path.map((node: TreeNode) => node.label).join(' / ') : path[path.length - 1].label;
return showAllLevels ? path.map((node: TreeNode) => node.label).join(' / ') : path.at(-1).label;
}
return value as string;
}
Expand Down Expand Up @@ -118,10 +118,11 @@ export const getCascaderValue = (value: CascaderValue, valueType: TdCascaderProp
if (valueType === 'single') {
return value;
}
const val = value as Array<CascaderValue>;
if (multiple) {
return (value as Array<CascaderValue>).map((item: TreeNodeValue[]) => item[item.length - 1]);
return val.map((item: TreeNodeValue[]) => item.at(-1));
}
return value[(value as Array<CascaderValue>).length - 1];
return val.at(-1);
};

/**
Expand All @@ -142,6 +143,6 @@ export function isEmptyValues(value: unknown): boolean {
* @returns boolean
*/
export function isValueInvalid(value: CascaderValue, cascaderContext: CascaderContextType) {
const { multiple, showAllLevels } = cascaderContext;
return (multiple && !isArray(value)) || (!multiple && isArray(value) && !showAllLevels);
const { multiple, showAllLevels, valueType } = cascaderContext;
return (multiple && !isArray(value)) || (!multiple && isArray(value) && valueType === 'single' && !showAllLevels);
}
3 changes: 2 additions & 1 deletion src/cascader/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ export const useContext = (
showAllLevels,
minCollapsedNum,
valueType,
modelValue,
} = props;
return {
value: statusContext.scopeVal,
Expand All @@ -72,7 +73,7 @@ export const useContext = (
statusContext.treeNodes = nodes;
},
setValue: (val: CascaderValue, source: CascaderChangeSource, node?: TreeNodeModel) => {
if (isEqual(val, statusContext.scopeVal)) return;
if (isEqual(val, modelValue)) return;
setInnerValue(val, { source, node });
},
setVisible: setPopupVisible,
Expand Down

0 comments on commit aac22bb

Please sign in to comment.