You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
51 lines
1.4 KiB
51 lines
1.4 KiB
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, ...restProps },
|
|
} = node as VNode & {
|
|
children: { default?: () => any };
|
|
};
|
|
const child = children.default ? children.default() : undefined;
|
|
return { key, value: value !== undefined ? value : key, children: child, ...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 };
|
|
};
|
|
|
|
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;
|
|
}
|