Skip to content

Commit 78e1cb7

Browse files
committed
refactor
1 parent bb168c3 commit 78e1cb7

File tree

3 files changed

+78
-60
lines changed

3 files changed

+78
-60
lines changed

packages/runtime-vapor/src/apiCreateComponent.ts

Lines changed: 51 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,15 @@ import {
44
currentInstance,
55
} from './component'
66
import { type Block, setupComponent } from './apiRender'
7-
import type { NormalizedRawProps, RawProps } from './componentProps'
7+
import {
8+
type NormalizedRawProps,
9+
type RawProps,
10+
normalizeRawProps,
11+
walkRawProps,
12+
} from './componentProps'
813
import type { DynamicSlots, Slots } from './componentSlots'
9-
import { walkRawProps, withAttrs } from './componentAttrs'
10-
import { isArray, isString } from '@vue/shared'
14+
import { withAttrs } from './componentAttrs'
15+
import { isString } from '@vue/shared'
1116
import { renderEffect } from './renderEffect'
1217
import { normalizeBlock } from './dom/element'
1318
import { setDynamicProp } from './dom/prop'
@@ -21,40 +26,9 @@ export function createComponent(
2126
once: boolean = false,
2227
) {
2328
if (isString(comp)) {
24-
// eslint-disable-next-line no-restricted-globals
25-
const el = document.createElement(comp)
26-
27-
if (rawProps) {
28-
if (!isArray(rawProps)) rawProps = [rawProps]
29-
renderEffect(() => {
30-
walkRawProps(rawProps as NormalizedRawProps, (key, value, getter) => {
31-
setDynamicProp(el, key, getter ? value() : value)
32-
})
33-
})
34-
}
35-
36-
if ((dynamicSlots && dynamicSlots.length) || (slots && slots.default)) {
37-
renderEffect(() => {
38-
let block: Block | undefined
39-
40-
if (slots && slots.default) {
41-
block = slots.default()
42-
} else {
43-
for (const slotFn of dynamicSlots!) {
44-
const slot = slotFn()
45-
if (slot.name === 'default') {
46-
block = slot.fn()
47-
break
48-
}
49-
}
50-
}
51-
52-
if (block) el.append(...normalizeBlock(block))
53-
})
54-
}
55-
56-
return el
29+
return fallbackComponent(comp, rawProps, slots, dynamicSlots, singleRoot)
5730
}
31+
5832
const current = currentInstance!
5933
const instance = createComponentInstance(
6034
comp,
@@ -70,3 +44,44 @@ export function createComponent(
7044

7145
return instance
7246
}
47+
48+
function fallbackComponent(
49+
comp: string,
50+
rawProps: RawProps | null = null,
51+
slots: Slots | null = null,
52+
dynamicSlots: DynamicSlots | null = null,
53+
singleRoot: boolean = false,
54+
) {
55+
// eslint-disable-next-line no-restricted-globals
56+
const el = document.createElement(comp)
57+
58+
if (rawProps) {
59+
rawProps = normalizeRawProps(rawProps)
60+
renderEffect(() => {
61+
walkRawProps(rawProps as NormalizedRawProps, (key, value, getter) => {
62+
setDynamicProp(el, key, getter ? value() : value)
63+
})
64+
})
65+
}
66+
67+
if ((dynamicSlots && dynamicSlots.length) || (slots && slots.default)) {
68+
renderEffect(() => {
69+
let block: Block | undefined
70+
71+
if (slots && slots.default) {
72+
block = slots.default()
73+
} else {
74+
for (const slotFn of dynamicSlots!) {
75+
const slot = slotFn()
76+
if (slot.name === 'default') {
77+
block = slot.fn()
78+
break
79+
}
80+
}
81+
}
82+
83+
if (block) el.append(...normalizeBlock(block))
84+
})
85+
}
86+
return { __return: el, rawProps }
87+
}

packages/runtime-vapor/src/componentAttrs.ts

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import { camelize, isArray, isFunction } from '@vue/shared'
1+
import { camelize, isArray } from '@vue/shared'
22
import { type ComponentInternalInstance, currentInstance } from './component'
33
import { isEmitListener } from './componentEmits'
44
import { setDynamicProps } from './dom/prop'
5-
import type { NormalizedRawProps, RawProps } from './componentProps'
5+
import { type RawProps, walkRawProps } from './componentProps'
66
import { renderEffect } from './renderEffect'
77

88
export function patchAttrs(instance: ComponentInternalInstance) {
@@ -42,24 +42,6 @@ export function patchAttrs(instance: ComponentInternalInstance) {
4242
}
4343
}
4444

45-
export function walkRawProps(
46-
rawProps: NormalizedRawProps,
47-
cb: (key: string, value: any, getter?: boolean) => void,
48-
) {
49-
for (const props of Array.from(rawProps).reverse()) {
50-
if (isFunction(props)) {
51-
const resolved = props()
52-
for (const rawKey in resolved) {
53-
cb(rawKey, resolved[rawKey])
54-
}
55-
} else {
56-
for (const rawKey in props) {
57-
cb(rawKey, props[rawKey], true)
58-
}
59-
}
60-
}
61-
}
62-
6345
export function withAttrs(props: RawProps): RawProps {
6446
const instance = currentInstance!
6547
if (instance.component.inheritAttrs === false) return props

packages/runtime-vapor/src/componentProps.ts

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,7 @@ export function initProps(
8383
isStateful: boolean,
8484
once: boolean,
8585
) {
86-
if (!rawProps) rawProps = []
87-
else if (!isArray(rawProps)) rawProps = [rawProps]
88-
instance.rawProps = rawProps
89-
86+
instance.rawProps = rawProps = normalizeRawProps(rawProps)
9087
const props: Data = {}
9188
const attrs = (instance.attrs = shallowReactive<Data>({}))
9289
const [options] = instance.propsOptions
@@ -166,6 +163,30 @@ function registerProp(
166163
}
167164
}
168165

166+
export function normalizeRawProps(rawProps: RawProps) {
167+
if (!rawProps) return []
168+
if (!isArray(rawProps)) return [rawProps]
169+
return rawProps
170+
}
171+
172+
export function walkRawProps(
173+
rawProps: NormalizedRawProps,
174+
cb: (key: string, value: any, getter?: boolean) => void,
175+
) {
176+
for (const props of Array.from(rawProps).reverse()) {
177+
if (isFunction(props)) {
178+
const resolved = props()
179+
for (const rawKey in resolved) {
180+
cb(rawKey, resolved[rawKey])
181+
}
182+
} else {
183+
for (const rawKey in props) {
184+
cb(rawKey, props[rawKey], true)
185+
}
186+
}
187+
}
188+
}
189+
169190
function getRawKey(obj: Data, key: string) {
170191
return Object.keys(obj).find(k => camelize(k) === key)
171192
}

0 commit comments

Comments
 (0)