ant-design-vue/components/vc-select/utils/legacyUtil.ts

57 lines
1.6 KiB
TypeScript
Raw Normal View History

2020-10-07 14:49:01 +00:00
import { flattenChildren, isValidElement } from '../../_util/props-util';
2020-10-10 09:01:39 +00:00
import { VNode, VNodeChild } from 'vue';
2020-10-07 14:49:01 +00:00
import { OptionData, OptionGroupData, OptionsType } from '../interface';
function convertNodeToOption(node: VNode): OptionData {
const {
key,
children,
2020-10-24 15:52:51 +00:00
props: { value, disabled, ...restProps },
2020-10-07 14:49:01 +00:00
} = node as VNode & {
children: { default?: () => any };
};
const child = children.default ? children.default() : undefined;
2020-10-24 15:52:51 +00:00
return {
key,
value: value !== undefined ? value : key,
children: child,
disabled: disabled || disabled === '', // support <a-select-option disabled />
...restProps,
};
2020-10-07 14:49:01 +00:00
}
2020-10-10 09:01:39 +00:00
export function convertChildrenToData(
nodes: VNodeChild | JSX.Element,
optionOnly = false,
): OptionsType {
const dd = flattenChildren(nodes as [])
2020-10-07 14:49:01 +00:00
.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 };
};
if (optionOnly || !isSelectOptGroup) {
return convertNodeToOption(node);
}
const child = children.default ? children.default() : undefined;
return {
key: `__RC_SELECT_GRP__${key === null ? index : key}__`,
label: key,
...props,
options: convertChildrenToData(child || []),
} as any;
})
.filter(data => data);
return dd;
}