-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
VoidField.ts
388 lines (336 loc) 路 9.01 KB
/
VoidField.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
import { FormPath, FormPathPattern, isValid, toArr } from '@formily/shared'
import { define, observable, batch, action } from '@formily/reactive'
import {
JSXComponent,
LifeCycleTypes,
FieldDisplayTypes,
FieldPatternTypes,
FieldDecorator,
FieldComponent,
IVoidFieldProps,
FormPatternTypes,
IVoidFieldState,
IModelSetter,
IModelGetter,
} from '../types'
import {
buildNodeIndexes,
createReactions,
createStateGetter,
createStateSetter,
initFieldUpdate,
initializeStart,
initializeEnd,
} from '../shared/internals'
import { Form } from './Form'
import { Query } from './Query'
export class VoidField<Decorator = any, Component = any, TextType = any> {
displayName = 'VoidField'
title: TextType
description: TextType
selfDisplay: FieldDisplayTypes
selfPattern: FieldPatternTypes
initialized: boolean
mounted: boolean
unmounted: boolean
content: any
data: any
decoratorType: Decorator
decoratorProps: Record<string, any>
componentType: Component
componentProps: Record<string, any>
designable: boolean
address: FormPath
path: FormPath
form: Form
props: IVoidFieldProps<Decorator, Component>
disposers: (() => void)[] = []
constructor(
address: FormPathPattern,
props: IVoidFieldProps<Decorator, Component>,
form: Form,
designable: boolean
) {
this.form = form
this.props = props
this.designable = designable
initializeStart()
this.makeIndexes(address)
this.initialize()
this.makeObservable()
this.makeReactive()
this.onInit()
initializeEnd()
}
protected makeIndexes(address: FormPathPattern) {
this.form.fields[address.toString()] = this
buildNodeIndexes(this, address)
}
protected initialize() {
this.mounted = false
this.unmounted = false
this.initialized = false
this.title = this.props.title
this.description = this.props.description
this.pattern = this.props.pattern
this.display = this.props.display
this.hidden = this.props.hidden
this.editable = this.props.editable
this.disabled = this.props.disabled
this.readOnly = this.props.readOnly
this.readPretty = this.props.readPretty
this.visible = this.props.visible
this.content = this.props.content
this.data = this.props.data
this.decorator = toArr(this.props.decorator)
this.component = toArr(this.props.component)
}
protected makeObservable() {
if (this.designable) return
define(this, {
title: observable.ref,
description: observable.ref,
selfDisplay: observable.ref,
selfPattern: observable.ref,
initialized: observable.ref,
mounted: observable.ref,
unmounted: observable.ref,
decoratorType: observable.ref,
componentType: observable.ref,
content: observable.ref,
data: observable.shallow,
decoratorProps: observable,
componentProps: observable,
display: observable.computed,
pattern: observable.computed,
hidden: observable.computed,
visible: observable.computed,
disabled: observable.computed,
readOnly: observable.computed,
readPretty: observable.computed,
editable: observable.computed,
component: observable.computed,
decorator: observable.computed,
setTitle: action,
setDescription: action,
setDisplay: action,
setPattern: action,
setComponent: action,
setComponentProps: action,
setDecorator: action,
setDecoratorProps: action,
onInit: batch,
onMount: batch,
onUnmount: batch,
})
}
protected makeReactive() {
if (this.designable) return
createReactions(this)
}
get parent() {
let parent = this.address.parent()
let identifier = parent.toString()
while (!this.form.fields[identifier]) {
parent = parent.parent()
identifier = parent.toString()
if (!identifier) return
}
return this.form.fields[identifier]
}
get component() {
return [this.componentType, this.componentProps]
}
set component(value: FieldComponent<Component>) {
const component = toArr(value)
this.componentType = component[0]
this.componentProps = component[1] || {}
}
get decorator() {
return [this.decoratorType, this.decoratorProps]
}
set decorator(value: FieldDecorator<Decorator>) {
const decorator = toArr(value)
this.decoratorType = decorator[0]
this.decoratorProps = decorator[1] || {}
}
get display(): FieldDisplayTypes {
const parentDisplay = this.parent?.display
if (parentDisplay && parentDisplay !== 'visible') {
if (this.selfDisplay && this.selfDisplay !== 'visible')
return this.selfDisplay
return parentDisplay
}
if (isValid(this.selfDisplay)) return this.selfDisplay
return parentDisplay || this.form.display || 'visible'
}
get pattern(): FormPatternTypes {
const parentPattern = this.parent?.pattern
if (isValid(this.selfPattern)) return this.selfPattern
return parentPattern || this.form.pattern || 'editable'
}
get editable() {
return this.pattern === 'editable'
}
get disabled() {
return this.pattern === 'disabled'
}
get readOnly() {
return this.pattern === 'readOnly'
}
get readPretty() {
return this.pattern === 'readPretty'
}
get hidden() {
return this.display === 'hidden'
}
get visible() {
return this.display === 'visible'
}
set hidden(hidden: boolean) {
if (!isValid(hidden)) return
if (hidden) {
this.display = 'hidden'
} else {
this.display = 'visible'
}
}
set visible(visible: boolean) {
if (!isValid(visible)) return
if (visible) {
this.display = 'visible'
} else {
this.display = 'none'
}
}
set editable(editable: boolean) {
if (!isValid(editable)) return
if (editable) {
this.pattern = 'editable'
} else {
this.pattern = 'readPretty'
}
}
set readOnly(readOnly: boolean) {
if (!isValid(readOnly)) return
if (readOnly) {
this.pattern = 'readOnly'
} else {
this.pattern = 'editable'
}
}
set disabled(disabled: boolean) {
if (!isValid(disabled)) return
if (disabled) {
this.pattern = 'disabled'
} else {
this.pattern = 'editable'
}
}
set readPretty(readPretty: boolean) {
if (!isValid(readPretty)) return
if (readPretty) {
this.pattern = 'readPretty'
} else {
this.pattern = 'editable'
}
}
set pattern(pattern: FieldPatternTypes) {
this.selfPattern = pattern
}
set display(display: FieldDisplayTypes) {
this.selfDisplay = display
}
setTitle = (title?: TextType) => {
this.title = title
}
setDescription = (description?: TextType) => {
this.description = description
}
setDisplay = (type?: FieldDisplayTypes) => {
this.display = type
}
setPattern = (type?: FieldPatternTypes) => {
this.pattern = type
}
setComponent = <C extends JSXComponent, ComponentProps extends object = {}>(
component?: C,
props?: ComponentProps
) => {
if (component) {
this.componentType = component as any
}
if (props) {
this.componentProps = this.componentProps || {}
Object.assign(this.componentProps, props)
}
}
setComponentProps = <ComponentProps extends object = {}>(
props?: ComponentProps
) => {
if (props) {
this.componentProps = this.componentProps || {}
Object.assign(this.componentProps, props)
}
}
setDecorator = <D extends JSXComponent, ComponentProps extends object = {}>(
component?: D,
props?: ComponentProps
) => {
if (component) {
this.decoratorType = component as any
}
if (props) {
this.decoratorProps = this.decoratorProps || {}
Object.assign(this.decoratorProps, props)
}
}
setDecoratorProps = <ComponentProps extends object = {}>(
props?: ComponentProps
) => {
if (props) {
this.decoratorProps = this.decoratorProps || {}
Object.assign(this.decoratorProps, props)
}
}
setState: IModelSetter<IVoidFieldState> = createStateSetter(this)
getState: IModelGetter<IVoidFieldState> = createStateGetter(this)
onInit = () => {
this.initialized = true
initFieldUpdate(this)
this.notify(LifeCycleTypes.ON_FIELD_INIT)
}
onMount = () => {
this.mounted = true
this.unmounted = false
this.notify(LifeCycleTypes.ON_FIELD_MOUNT)
}
onUnmount = () => {
this.mounted = false
this.unmounted = true
this.notify(LifeCycleTypes.ON_FIELD_UNMOUNT)
}
query = (pattern: FormPathPattern | RegExp) => {
return new Query({
pattern,
base: this.address,
form: this.form,
})
}
notify = (type: LifeCycleTypes, payload?: any) => {
return this.form.notify(type, payload ?? this)
}
dispose = () => {
this.disposers.forEach((dispose) => {
dispose()
})
this.form.removeEffects(this)
}
destroy = () => {
this.dispose()
delete this.form.fields[this.address.toString()]
}
match = (pattern: FormPathPattern) => {
return FormPath.parse(pattern).matchAliasGroup(this.address, this.path)
}
}