diff --git a/src/cascader/core/helper.ts b/src/cascader/core/helper.ts index b1f69d434b..cc5a53cec9 100644 --- a/src/cascader/core/helper.ts +++ b/src/cascader/core/helper.ts @@ -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; } @@ -118,10 +118,11 @@ export const getCascaderValue = (value: CascaderValue, valueType: TdCascaderProp if (valueType === 'single') { return value; } + const val = value as Array; if (multiple) { - return (value as Array).map((item: TreeNodeValue[]) => item[item.length - 1]); + return val.map((item: TreeNodeValue[]) => item.at(-1)); } - return value[(value as Array).length - 1]; + return val.at(-1); }; /** @@ -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); } diff --git a/src/cascader/hooks.ts b/src/cascader/hooks.ts index 2bd6eb9ac1..295790c4b4 100644 --- a/src/cascader/hooks.ts +++ b/src/cascader/hooks.ts @@ -51,6 +51,7 @@ export const useContext = ( showAllLevels, minCollapsedNum, valueType, + modelValue, } = props; return { value: statusContext.scopeVal, @@ -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,