Skip to content

Commit a24ebc8

Browse files
authored
fix(cascader): verify invalid value logic errors and optimize code (#3438)
1 parent 3134451 commit a24ebc8

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

src/cascader/core/helper.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export function getSingleContent(cascaderContext: CascaderContextType): string {
2727
}
2828
const path = node && node[0].getPath();
2929
if (path && path.length) {
30-
return showAllLevels ? path.map((node: TreeNode) => node.label).join(' / ') : path[path.length - 1].label;
30+
return showAllLevels ? path.map((node: TreeNode) => node.label).join(' / ') : path.at(-1).label;
3131
}
3232
return value as string;
3333
}
@@ -120,10 +120,11 @@ export const getCascaderValue = (value: CascaderValue, valueType: TdCascaderProp
120120
if (valueType === 'single') {
121121
return value;
122122
}
123+
const val = value as Array<CascaderValue>;
123124
if (multiple) {
124-
return (value as Array<CascaderValue>).map((item: TreeNodeValue[]) => item[item.length - 1]);
125+
return val.map((item: TreeNodeValue[]) => item.at(-1));
125126
}
126-
return value[(value as Array<CascaderValue>).length - 1];
127+
return val.at(-1);
127128
};
128129

129130
/**
@@ -144,6 +145,9 @@ export function isEmptyValues(value: unknown): boolean {
144145
* @returns boolean
145146
*/
146147
export function isValueInvalid(value: CascaderValue, cascaderContext: CascaderContextType) {
147-
const { multiple, showAllLevels } = cascaderContext;
148-
return (multiple && !Array.isArray(value)) || (!multiple && Array.isArray(value) && !showAllLevels);
148+
const { multiple, showAllLevels, valueType } = cascaderContext;
149+
return (
150+
(multiple && !Array.isArray(value))
151+
|| (!multiple && Array.isArray(value) && valueType === 'single' && !showAllLevels)
152+
);
149153
}

0 commit comments

Comments
 (0)