forked from vueComponent/ant-design-vue
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlegacyUtil.ts
57 lines (53 loc) · 1.66 KB
/
legacyUtil.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import { flattenChildren, isValidElement } from '../../_util/props-util';
import { VNode, VNodeChild } from 'vue';
import { OptionData, OptionGroupData, OptionsType } from '../interface';
function convertNodeToOption(node: VNode): OptionData {
const {
key,
children,
props: { value, disabled, ...restProps },
} = node as VNode & {
children: { default?: () => any };
};
const child = children && children.default ? children.default() : undefined;
return {
key,
value: value !== undefined ? value : key,
children: child,
disabled: disabled || disabled === '', // support <a-select-option disabled />
...restProps,
};
}
export function convertChildrenToData(
nodes: VNodeChild | JSX.Element,
optionOnly = false,
): OptionsType {
const dd = flattenChildren(nodes as [])
.map((node: VNode, index: number): OptionData | OptionGroupData | null => {
if (!isValidElement(node) || !node.type) {
return null;
}
const {
type: { isSelectOptGroup },
key,
children,
props,
} = node as VNode & {
type: { isSelectOptGroup?: boolean };
children: { default?: () => any; label?: () => any };
};
if (optionOnly || !isSelectOptGroup) {
return convertNodeToOption(node);
}
const child = children && children.default ? children.default() : undefined;
const label = props?.label || children.label?.() || key;
return {
key: `__RC_SELECT_GRP__${key === null ? index : key}__`,
...props,
label,
options: convertChildrenToData(child || []),
} as any;
})
.filter(data => data);
return dd;
}