-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ts
158 lines (130 loc) · 3.47 KB
/
main.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import Kiq from './src/main'
import Async from './components/Async'
class Prefix extends Kiq.Component {
state = {
arr: this.props.start || [],
}
onComponentRender() {
setInterval(() => {
this.setState({
arr: [this.state.arr.length, ...this.state.arr],
})
}, 1000)
}
Element() {
return Kiq.createElement('ol', null, ...this.state.arr.map((item) => Kiq.createElement('li', { key: item }, item)))
}
}
class Counter extends Kiq.Component {
state = {
count: 0,
}
Element() {
return Kiq.createElement('button', { onclick: () => this.setState({ count: this.state.count + 1 }) }, 'The count is: ', this.state.count)
}
}
class NoKeysPrefix extends Kiq.Component {
state = {
arr: this.props.start || [],
}
onComponentRender() {
setInterval(() => {
this.setState({
arr: [this.state.arr.length, ...this.state.arr],
})
}, 1000)
}
Element() {
return Kiq.createElement('ol', null, ...this.state.arr.map((item) => Kiq.createElement('li', null, item)))
}
}
class Parent extends Kiq.Component {
state = {
count: 0,
}
Element() {
return Kiq.createElement(Child, { increment: () => this.setState({ count: this.state.count + 1 }), count: this.state.count })
}
}
class Child extends Kiq.Component {
Element() {
return Kiq.createElement('button', { onclick: () => this.props.increment() }, 'The count is: ', this.props.count)
}
}
class ParentList extends Kiq.Component {
state = {
arr: [],
}
onComponentRender() {
setInterval(() => {
this.setState({
arr: [...this.state.arr, this.state.arr.length],
})
}, 1000)
}
Element() {
return Kiq.createElement('ol', null, ...this.state.arr.map((item) => Kiq.createElement(ChildListItem, { key: item, count: item })))
}
}
class ChildListItem extends Kiq.Component {
state = {
count: this.props.count,
}
Element() {
return Kiq.createElement('li', null, this.state.count)
}
}
const timeout = (ms: number) => new Promise((resolve) => setTimeout(() => resolve('data'), ms))
class Test extends Kiq.Component {
Element() {
return Kiq.createElement(Async, { promise: () => timeout(1000), then: (data) => Kiq.createElement('div', null, data) }, 'Loading...')
}
}
class Condition extends Kiq.Component {
state = {
guard: false,
}
Element() {
return Kiq.createElement(
'div',
null,
Kiq.createElement('button', { onclick: () => this.setState({ guard: !this.state.guard }) }, 'Toggle'),
Kiq.createElement('div', null, this.state.guard ? Kiq.createElement('div', null, 'Guard is ', 'true') : Kiq.createElement(UnMount)),
)
}
}
class UnMount extends Kiq.Component {
onComponentRender() {
console.log('render')
}
onComponentWillUnMount() {
console.log('unmount')
}
Element() {
return Kiq.createElement('div', null, 'Guard is ', 'false')
}
}
class Remove extends Kiq.Component {
state = {
condition: false,
}
onComponentRender() {
setInterval(() => {
this.setState({
condition: !this.state.condition,
})
}, 2000)
}
Element() {
return Kiq.createElement('div', null, this.state.condition ? null : 'hello')
}
}
const app = document.getElementById('app') as HTMLElement
//Kiq.render(Kiq.createElement(Prefix), app)
//Kiq.render(Kiq.createElement(Counter), app)
//Kiq.render(Kiq.createElement(NoKeysPrefix, { start: [1, 2, 3] }), app)
//Kiq.render(Kiq.createElement(Parent), app)
//Kiq.render(Kiq.createElement(ParentList), app)
//Kiq.render(Kiq.createElement(Test), app)
//Kiq.render(Kiq.createElement(Condition), app)
Kiq.render(Kiq.createElement(Remove), app)