(component): select / tab slots 解析逻辑优化

This commit is contained in:
就眠儀式 2022-11-23 04:49:13 +08:00
parent 753ef0fab2
commit e2b14f9fd4
5 changed files with 15 additions and 9 deletions

View File

@ -26,7 +26,7 @@ import LayTagInput from "../tagInput/index.vue";
import LayDropdown from "../dropdown/index.vue"; import LayDropdown from "../dropdown/index.vue";
import LaySelectOption, { SelectOptionProps } from "../selectOption/index.vue"; import LaySelectOption, { SelectOptionProps } from "../selectOption/index.vue";
import { SelectSize } from "./interface"; import { SelectSize } from "./interface";
import { ShapeFlags } from "../dropdown/util"; import { isArrayChildren } from "src/utils";
export interface SelectProps { export interface SelectProps {
name?: string; name?: string;
@ -76,7 +76,7 @@ var timer: any;
const getOption = (nodes: VNode[], newOptions: any[]) => { const getOption = (nodes: VNode[], newOptions: any[]) => {
nodes?.map((item) => { nodes?.map((item) => {
if (item && item.shapeFlag && item.shapeFlag == ShapeFlags.ARRAY_CHILDREN) { if (isArrayChildren(item, item.children)) {
getOption(item.children as VNode[], newOptions); getOption(item.children as VNode[], newOptions);
} else { } else {
if ((item.type as Component).name == LaySelectOption.name) { if ((item.type as Component).name == LaySelectOption.name) {

View File

@ -29,7 +29,7 @@ import {
} from "vue"; } from "vue";
import { useResizeObserver } from "@vueuse/core"; import { useResizeObserver } from "@vueuse/core";
import { TabData, TabInjectKey, TabPosition } from "./interface"; import { TabData, TabInjectKey, TabPosition } from "./interface";
import { ShapeFlags } from "../dropdown/util"; import { isArrayChildren } from "src/utils";
export interface TabProps { export interface TabProps {
type?: string; type?: string;
@ -47,7 +47,7 @@ const childrens: Ref<VNode[]> = ref([]);
const setItemInstanceBySlot = function (nodes: VNode[]) { const setItemInstanceBySlot = function (nodes: VNode[]) {
nodes?.map((item) => { nodes?.map((item) => {
if (item && item.shapeFlag && item.shapeFlag == ShapeFlags.ARRAY_CHILDREN) { if (isArrayChildren(item, item.children)) {
setItemInstanceBySlot(item.children as VNode[]); setItemInstanceBySlot(item.children as VNode[]);
} else { } else {
if ((item.type as Component).name == tabItem.name) { if ((item.type as Component).name == tabItem.name) {

View File

@ -18,10 +18,16 @@ export const isElement = (vn: VNode) => {
return Boolean(vn && vn.shapeFlag & ShapeFlags.ELEMENT); return Boolean(vn && vn.shapeFlag & ShapeFlags.ELEMENT);
}; };
export const isComponent = (vn: VNode, type?: VNodeTypes): type is Component => { export const isComponent = (
vn: VNode,
type?: VNodeTypes
): type is Component => {
return Boolean(vn && vn.shapeFlag & ShapeFlags.COMPONENT); return Boolean(vn && vn.shapeFlag & ShapeFlags.COMPONENT);
}; };
export const isArrayChildren = (vn: VNode, children: VNode["children"]): children is VNode[] => { export const isArrayChildren = (
vn: VNode,
children: VNode["children"]
): children is VNode[] => {
return Boolean(vn && vn.shapeFlag & ShapeFlags.ARRAY_CHILDREN); return Boolean(vn && vn.shapeFlag & ShapeFlags.ARRAY_CHILDREN);
}; };