-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvnode.ts
69 lines (60 loc) · 1.87 KB
/
vnode.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
58
59
60
61
62
63
64
65
66
67
68
69
import { Component } from '@/component';
import { normalizeChildren } from './helpers';
import {
type ComponentVNode,
type ElementVNode,
type FragmentVNode,
FragmentVNodeType,
type FunctionComponentVNode,
type TextVNode,
TextVNodeType,
type VNode,
type VNodeChildren,
type VNodeProps,
type VNodeTypes,
} from './types';
export function createVNode(
type: VNodeTypes,
props?: VNodeProps<any> | null,
children?: VNodeChildren[]
): VNode {
const { ref: rawRef, ...others } = props ?? {};
const normalizedChildren = normalizeChildren(children || []);
const ref = typeof rawRef === 'string' ? rawRef : null;
return {
_isVNode: true,
type,
props: others,
children: normalizedChildren,
ref,
el: null,
listeners: null,
component: null,
} satisfies VNode;
}
export function createTextVNode(value: string) {
return createVNode(TextVNodeType, { nodeValue: value });
}
export function createFragmentVNode(children: VNodeChildren[]): VNode {
return createVNode(FragmentVNodeType, null, children);
}
export function isVNode(value?: any): value is VNode {
return value ? value?._isVNode === true : false;
}
export function isTextVNode(value?: VNode): value is TextVNode {
return value.type === TextVNodeType;
}
export function isFragmentVNode(value?: VNode): value is FragmentVNode {
return value.type === FragmentVNodeType;
}
export function isElementVNode(value?: VNode): value is ElementVNode {
return typeof value.type === 'string';
}
export function isClassComponentVNode(value?: VNode): value is ComponentVNode {
return (
isFunctionComponentVNode(value) && Object.prototype.isPrototypeOf.call(Component, value.type)
);
}
export function isFunctionComponentVNode(value?: VNode): value is FunctionComponentVNode {
return typeof value.type === 'function';
}