@formvk/core 0.1.0 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +14 -0
- package/README.md +63 -0
- package/package.json +8 -4
- package/src/__tests__/array.spec.ts +0 -168
- package/src/__tests__/effects.spec.ts +0 -595
- package/src/__tests__/externals.spec.ts +0 -101
- package/src/__tests__/field.spec.ts +0 -572
- package/src/__tests__/form.spec.ts +0 -720
- package/src/__tests__/graph.spec.ts +0 -56
- package/src/__tests__/heart.spec.ts +0 -60
- package/src/__tests__/internals.spec.ts +0 -138
- package/src/__tests__/lifecycle.spec.ts +0 -51
- package/src/__tests__/object.spec.ts +0 -107
- package/src/__tests__/query.spec.ts +0 -100
- package/src/__tests__/shared.ts +0 -10
- package/src/__tests__/typed.spec.ts +0 -52
- package/src/__tests__/void.spec.ts +0 -163
- package/src/effects/index.ts +0 -2
- package/src/effects/onFieldEffects.ts +0 -97
- package/src/effects/onFormEffects.ts +0 -48
- package/src/global.d.ts +0 -8
- package/src/index.ts +0 -4
- package/src/models/ArrayField.ts +0 -133
- package/src/models/BaseField.ts +0 -359
- package/src/models/Field.ts +0 -450
- package/src/models/Form.ts +0 -586
- package/src/models/Graph.ts +0 -56
- package/src/models/Heart.ts +0 -75
- package/src/models/LifeCycle.ts +0 -42
- package/src/models/ObjectField.ts +0 -48
- package/src/models/Query.ts +0 -97
- package/src/models/VoidField.ts +0 -64
- package/src/models/index.ts +0 -9
- package/src/models/types.ts +0 -9
- package/src/shared/checkers.ts +0 -85
- package/src/shared/constants.ts +0 -60
- package/src/shared/effective.ts +0 -69
- package/src/shared/externals.ts +0 -65
- package/src/shared/internals.ts +0 -965
- package/src/types.ts +0 -401
- package/tsconfig.build.json +0 -9
- package/tsconfig.json +0 -4
- package/vite.config.ts +0 -46
package/src/shared/internals.ts
DELETED
|
@@ -1,965 +0,0 @@
|
|
|
1
|
-
import type { DataChange } from '@formvk/reactive'
|
|
2
|
-
import { autorun, batch, contains, isObservable, reaction, toJS, untracked } from '@formvk/reactive'
|
|
3
|
-
import type { FormPathPattern } from '@formvk/shared'
|
|
4
|
-
import {
|
|
5
|
-
FormPath,
|
|
6
|
-
clone,
|
|
7
|
-
each,
|
|
8
|
-
isEmpty,
|
|
9
|
-
isFn,
|
|
10
|
-
isNumberLike,
|
|
11
|
-
isPlainObj,
|
|
12
|
-
isUndef,
|
|
13
|
-
isValid,
|
|
14
|
-
pascalCase,
|
|
15
|
-
toArr,
|
|
16
|
-
} from '@formvk/shared'
|
|
17
|
-
import type { ValidatorTriggerType } from '@formvk/validator'
|
|
18
|
-
import { parseValidatorDescriptions, validate } from '@formvk/validator'
|
|
19
|
-
|
|
20
|
-
import type { ArrayField, Field, Form, ObjectField } from '../models'
|
|
21
|
-
import type { BaseField } from '../models/BaseField'
|
|
22
|
-
import type {
|
|
23
|
-
FieldFeedbackTypes,
|
|
24
|
-
FieldMatchPattern,
|
|
25
|
-
FieldValidatorContext,
|
|
26
|
-
GeneralField,
|
|
27
|
-
IExchangeArrayStateProps,
|
|
28
|
-
IFieldFeedback,
|
|
29
|
-
IFieldResetOptions,
|
|
30
|
-
IFormFeedback,
|
|
31
|
-
INodePatch,
|
|
32
|
-
ISearchFeedback,
|
|
33
|
-
ISpliceArrayStateProps,
|
|
34
|
-
} from '../types'
|
|
35
|
-
import { LifeCycleTypes } from '../types'
|
|
36
|
-
import {
|
|
37
|
-
GlobalState,
|
|
38
|
-
MutuallyExclusiveProperties,
|
|
39
|
-
NumberIndexReg,
|
|
40
|
-
RESPONSE_REQUEST_DURATION,
|
|
41
|
-
ReadOnlyProperties,
|
|
42
|
-
ReservedProperties,
|
|
43
|
-
} from './constants'
|
|
44
|
-
import { isArrayField, isDataField, isForm, isGeneralField, isObjectField, isQuery, isVoidField } from './externals'
|
|
45
|
-
|
|
46
|
-
const hasOwnProperty = Object.prototype.hasOwnProperty
|
|
47
|
-
|
|
48
|
-
function notify(target: Form | Field, formType: LifeCycleTypes, fieldType: LifeCycleTypes) {
|
|
49
|
-
if (isForm(target)) {
|
|
50
|
-
target.notify(formType)
|
|
51
|
-
} else {
|
|
52
|
-
target.notify(fieldType)
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
export function isHTMLInputEvent(event: any, stopPropagation = true) {
|
|
57
|
-
if (event?.target) {
|
|
58
|
-
if (typeof event.target === 'object' && ('value' in event.target || 'checked' in event.target)) return true
|
|
59
|
-
if (stopPropagation) event.stopPropagation?.()
|
|
60
|
-
}
|
|
61
|
-
return false
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
export function getValuesFromEvent(args: any[]) {
|
|
65
|
-
return args.map(event => {
|
|
66
|
-
if (event?.target) {
|
|
67
|
-
if (isValid(event.target.value)) return event.target.value
|
|
68
|
-
if (isValid(event.target.checked)) return event.target.checked
|
|
69
|
-
return
|
|
70
|
-
}
|
|
71
|
-
return event
|
|
72
|
-
})
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
export function getTypedDefaultValue(field: Field) {
|
|
76
|
-
if (isArrayField(field)) return []
|
|
77
|
-
if (isObjectField(field)) return {}
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
export function buildFieldPath(field: GeneralField) {
|
|
81
|
-
return buildDataPath(field.form.fields, field.address)
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
export function buildDataPath(fields: Record<string, GeneralField>, pattern: FormPath) {
|
|
85
|
-
let prevArray = false
|
|
86
|
-
const segments = pattern.segments
|
|
87
|
-
const path = segments.reduce<string[]>((path, key, index) => {
|
|
88
|
-
const currentPath = path.concat(String(key))
|
|
89
|
-
const currentAddress = segments.slice(0, index + 1)
|
|
90
|
-
const current = fields[currentAddress.join('.')]
|
|
91
|
-
if (prevArray) {
|
|
92
|
-
if (!isVoidField(current)) {
|
|
93
|
-
prevArray = false
|
|
94
|
-
}
|
|
95
|
-
return path
|
|
96
|
-
}
|
|
97
|
-
if (index >= segments.length - 1) {
|
|
98
|
-
return currentPath
|
|
99
|
-
}
|
|
100
|
-
if (isVoidField(current)) {
|
|
101
|
-
const parentAddress = segments.slice(0, index)
|
|
102
|
-
const parent = fields[parentAddress.join('.')]
|
|
103
|
-
if (isArrayField(parent) && isNumberLike(key)) {
|
|
104
|
-
prevArray = true
|
|
105
|
-
return currentPath
|
|
106
|
-
}
|
|
107
|
-
return path
|
|
108
|
-
} else {
|
|
109
|
-
prevArray = false
|
|
110
|
-
}
|
|
111
|
-
return currentPath
|
|
112
|
-
}, [])
|
|
113
|
-
return new FormPath(path)
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
export function locateNode(field: GeneralField, address: FormPathPattern) {
|
|
117
|
-
field.address = FormPath.parse(address)
|
|
118
|
-
field.path = buildFieldPath(field)
|
|
119
|
-
field.form.indexes[field.path.toString()] = field.address.toString()
|
|
120
|
-
return field
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
export function patchFieldStates(target: Record<string, GeneralField>, patches: INodePatch<GeneralField>[]) {
|
|
124
|
-
patches.forEach(({ type, address, oldAddress, payload }) => {
|
|
125
|
-
if (type === 'remove') {
|
|
126
|
-
destroy(target, address, false)
|
|
127
|
-
} else if (type === 'update') {
|
|
128
|
-
if (payload) {
|
|
129
|
-
target[address] = payload
|
|
130
|
-
if (oldAddress && target[oldAddress] === payload) {
|
|
131
|
-
delete target[oldAddress]
|
|
132
|
-
}
|
|
133
|
-
}
|
|
134
|
-
if (address && payload) {
|
|
135
|
-
locateNode(payload, address)
|
|
136
|
-
}
|
|
137
|
-
}
|
|
138
|
-
})
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
export function destroy(target: Record<string, GeneralField>, address: string, forceClear = true) {
|
|
142
|
-
const field = target[address]
|
|
143
|
-
field?.dispose()
|
|
144
|
-
if (isDataField(field) && forceClear) {
|
|
145
|
-
const form = field.form
|
|
146
|
-
const path = field.path
|
|
147
|
-
form.deleteValuesIn(path)
|
|
148
|
-
form.deleteInitialValuesIn(path)
|
|
149
|
-
}
|
|
150
|
-
delete target[address]
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
export function patchFormValues(form: Form, path: Array<string | number>, source: any) {
|
|
154
|
-
function update(path: Array<string | number>, source: any) {
|
|
155
|
-
if (path.length) {
|
|
156
|
-
form.setValuesIn(path, clone(source))
|
|
157
|
-
} else {
|
|
158
|
-
Object.assign(form.values, clone(source))
|
|
159
|
-
}
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
function patch(source: any, path: Array<string | number> = []) {
|
|
163
|
-
const targetValue = form.getValuesIn(path)
|
|
164
|
-
const targetField = form.query(path).take()
|
|
165
|
-
const isUnVoidField = targetField && !isVoidField(targetField)
|
|
166
|
-
|
|
167
|
-
if (isUnVoidField && targetField.display === 'none') {
|
|
168
|
-
targetField.caches.value = clone(source)
|
|
169
|
-
return
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
if (allowAssignDefaultValue(targetValue, source)) {
|
|
173
|
-
update(path, source)
|
|
174
|
-
} else {
|
|
175
|
-
if (isEmpty(source)) return
|
|
176
|
-
if (GlobalState.initializing) return
|
|
177
|
-
if (isPlainObj(targetValue) && isPlainObj(source)) {
|
|
178
|
-
each(source, (value, key) => {
|
|
179
|
-
patch(value, path.concat(key))
|
|
180
|
-
})
|
|
181
|
-
} else {
|
|
182
|
-
if (targetField) {
|
|
183
|
-
if (isUnVoidField && !targetField.selfModified) {
|
|
184
|
-
update(path, source)
|
|
185
|
-
}
|
|
186
|
-
} else if (form.initialized) {
|
|
187
|
-
update(path, source)
|
|
188
|
-
}
|
|
189
|
-
}
|
|
190
|
-
}
|
|
191
|
-
}
|
|
192
|
-
patch(source, path)
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
export function matchFeedback(search?: ISearchFeedback, feedback?: IFormFeedback) {
|
|
196
|
-
if (!search || !feedback) return false
|
|
197
|
-
if (search.type && search.type !== feedback.type) return false
|
|
198
|
-
if (search.code && search.code !== feedback.code) return false
|
|
199
|
-
if (search.path && feedback.path) {
|
|
200
|
-
if (!FormPath.parse(search.path).match(feedback.path)) return false
|
|
201
|
-
}
|
|
202
|
-
if (search.address && feedback.address) {
|
|
203
|
-
if (!FormPath.parse(search.address).match(feedback.address)) return false
|
|
204
|
-
}
|
|
205
|
-
if (search.triggerType && search.triggerType !== feedback.triggerType) return false
|
|
206
|
-
return true
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
export function queryFeedbacks(field: Field, search?: ISearchFeedback) {
|
|
210
|
-
return field.feedbacks.filter(feedback => {
|
|
211
|
-
if (!feedback.messages?.length) return false
|
|
212
|
-
return matchFeedback(search, {
|
|
213
|
-
...feedback,
|
|
214
|
-
address: field.address?.toString(),
|
|
215
|
-
path: field.path?.toString(),
|
|
216
|
-
})
|
|
217
|
-
})
|
|
218
|
-
}
|
|
219
|
-
|
|
220
|
-
export function queryFeedbackMessages(field: Field, search: ISearchFeedback) {
|
|
221
|
-
if (!field.feedbacks.length) return []
|
|
222
|
-
return queryFeedbacks(field, search).reduce<any[]>(
|
|
223
|
-
(buf, info) => (isEmpty(info.messages) ? buf : buf.concat(info.messages)),
|
|
224
|
-
[]
|
|
225
|
-
)
|
|
226
|
-
}
|
|
227
|
-
|
|
228
|
-
export function updateFeedback(field: Field, feedback?: IFieldFeedback) {
|
|
229
|
-
if (!feedback) return
|
|
230
|
-
return batch(() => {
|
|
231
|
-
if (!field.feedbacks.length) {
|
|
232
|
-
if (!feedback.messages?.length) {
|
|
233
|
-
return
|
|
234
|
-
}
|
|
235
|
-
field.feedbacks = [feedback]
|
|
236
|
-
} else {
|
|
237
|
-
const searched = queryFeedbacks(field, feedback)
|
|
238
|
-
if (searched.length) {
|
|
239
|
-
field.feedbacks = field.feedbacks.reduce<IFieldFeedback[]>((buf, item) => {
|
|
240
|
-
if (searched.includes(item)) {
|
|
241
|
-
if (feedback.messages?.length) {
|
|
242
|
-
item.messages = feedback.messages
|
|
243
|
-
return buf.concat(item)
|
|
244
|
-
} else {
|
|
245
|
-
return buf
|
|
246
|
-
}
|
|
247
|
-
} else {
|
|
248
|
-
return buf.concat(item)
|
|
249
|
-
}
|
|
250
|
-
}, [])
|
|
251
|
-
return
|
|
252
|
-
} else if (feedback.messages?.length) {
|
|
253
|
-
field.feedbacks = field.feedbacks.concat(feedback)
|
|
254
|
-
}
|
|
255
|
-
}
|
|
256
|
-
})
|
|
257
|
-
}
|
|
258
|
-
|
|
259
|
-
export async function validateToFeedbacks(field: Field, triggerType: ValidatorTriggerType = 'onInput') {
|
|
260
|
-
const results = await validate<FieldValidatorContext>(field.value, field.validator, {
|
|
261
|
-
triggerType,
|
|
262
|
-
validateFirst: field.props.validateFirst ?? field.form.props.validateFirst,
|
|
263
|
-
context: { field, form: field.form },
|
|
264
|
-
})
|
|
265
|
-
|
|
266
|
-
batch(() => {
|
|
267
|
-
each(results, (messages, type) => {
|
|
268
|
-
field.setFeedback({
|
|
269
|
-
triggerType,
|
|
270
|
-
type: type as FieldFeedbackTypes,
|
|
271
|
-
code: pascalCase(`validate-${type}`),
|
|
272
|
-
messages: messages,
|
|
273
|
-
})
|
|
274
|
-
})
|
|
275
|
-
})
|
|
276
|
-
return results
|
|
277
|
-
}
|
|
278
|
-
|
|
279
|
-
export function setValidatorRule(field: Field, name: string, value: any) {
|
|
280
|
-
if (!isValid(value)) return
|
|
281
|
-
const validators = parseValidatorDescriptions(field.validator)
|
|
282
|
-
const hasRule = validators.some(desc => name in desc)
|
|
283
|
-
const rule = {
|
|
284
|
-
[name]: value,
|
|
285
|
-
}
|
|
286
|
-
if (hasRule) {
|
|
287
|
-
field.validator = validators.map((desc: any) => {
|
|
288
|
-
if (isPlainObj(desc) && hasOwnProperty.call(desc, name)) {
|
|
289
|
-
;(desc as Record<string, any>)[name] = value
|
|
290
|
-
return desc
|
|
291
|
-
}
|
|
292
|
-
return desc
|
|
293
|
-
})
|
|
294
|
-
} else {
|
|
295
|
-
if (name === 'required') {
|
|
296
|
-
field.validator = [rule].concat(validators)
|
|
297
|
-
} else {
|
|
298
|
-
field.validator = validators.concat(rule)
|
|
299
|
-
}
|
|
300
|
-
}
|
|
301
|
-
}
|
|
302
|
-
|
|
303
|
-
export function spliceArrayState(field: ArrayField<any>, props?: ISpliceArrayStateProps) {
|
|
304
|
-
const { startIndex, deleteCount, insertCount } = {
|
|
305
|
-
startIndex: 0,
|
|
306
|
-
deleteCount: 0,
|
|
307
|
-
insertCount: 0,
|
|
308
|
-
...props,
|
|
309
|
-
}
|
|
310
|
-
const address = field.address.toString()
|
|
311
|
-
const addrLength = address.length
|
|
312
|
-
const form = field.form
|
|
313
|
-
const fields = form.fields
|
|
314
|
-
const fieldPatches: INodePatch<GeneralField>[] = []
|
|
315
|
-
const offset = insertCount - deleteCount
|
|
316
|
-
function isArrayChildren(identifier: string) {
|
|
317
|
-
return identifier.indexOf(address) === 0 && identifier.length > addrLength
|
|
318
|
-
}
|
|
319
|
-
function isAfterNode(identifier: string) {
|
|
320
|
-
const afterStr = identifier.substring(addrLength)
|
|
321
|
-
const number = afterStr.match(NumberIndexReg)?.[1]
|
|
322
|
-
if (number === undefined) return false
|
|
323
|
-
const index = Number(number)
|
|
324
|
-
return index > startIndex + deleteCount - 1
|
|
325
|
-
}
|
|
326
|
-
function isInsertNode(identifier: string) {
|
|
327
|
-
const afterStr = identifier.substring(addrLength)
|
|
328
|
-
const number = afterStr.match(NumberIndexReg)?.[1]
|
|
329
|
-
if (number === undefined) return false
|
|
330
|
-
const index = Number(number)
|
|
331
|
-
return index >= startIndex && index < startIndex + insertCount
|
|
332
|
-
}
|
|
333
|
-
function isDeleteNode(identifier: string) {
|
|
334
|
-
const preStr = identifier.substring(0, addrLength)
|
|
335
|
-
const afterStr = identifier.substring(addrLength)
|
|
336
|
-
const number = afterStr.match(NumberIndexReg)?.[1]
|
|
337
|
-
if (number === undefined) return false
|
|
338
|
-
const index = Number(number)
|
|
339
|
-
return (
|
|
340
|
-
(index > startIndex && !fields[`${preStr}${afterStr.replace(/^\.\d+/, `.${index + deleteCount}`)}`]) ||
|
|
341
|
-
index === startIndex
|
|
342
|
-
)
|
|
343
|
-
}
|
|
344
|
-
function moveIndex(identifier: string) {
|
|
345
|
-
if (offset === 0) return identifier
|
|
346
|
-
const preStr = identifier.substring(0, addrLength)
|
|
347
|
-
const afterStr = identifier.substring(addrLength)
|
|
348
|
-
const number = afterStr.match(NumberIndexReg)?.[1]
|
|
349
|
-
if (number === undefined) return identifier
|
|
350
|
-
const index = Number(number) + offset
|
|
351
|
-
return `${preStr}${afterStr.replace(/^\.\d+/, `.${index}`)}`
|
|
352
|
-
}
|
|
353
|
-
|
|
354
|
-
batch(() => {
|
|
355
|
-
each(fields, (field, identifier) => {
|
|
356
|
-
if (isArrayChildren(identifier)) {
|
|
357
|
-
if (isAfterNode(identifier)) {
|
|
358
|
-
const newIdentifier = moveIndex(identifier)
|
|
359
|
-
fieldPatches.push({
|
|
360
|
-
type: 'update',
|
|
361
|
-
address: newIdentifier,
|
|
362
|
-
oldAddress: identifier,
|
|
363
|
-
payload: field,
|
|
364
|
-
})
|
|
365
|
-
}
|
|
366
|
-
if (isInsertNode(identifier) || isDeleteNode(identifier)) {
|
|
367
|
-
fieldPatches.push({ type: 'remove', address: identifier })
|
|
368
|
-
}
|
|
369
|
-
}
|
|
370
|
-
})
|
|
371
|
-
patchFieldStates(fields, fieldPatches)
|
|
372
|
-
})
|
|
373
|
-
field.form.notify(LifeCycleTypes.ON_FORM_GRAPH_CHANGE)
|
|
374
|
-
}
|
|
375
|
-
|
|
376
|
-
export function exchangeArrayState(field: ArrayField<any>, props: IExchangeArrayStateProps) {
|
|
377
|
-
const { fromIndex, toIndex } = {
|
|
378
|
-
fromIndex: 0,
|
|
379
|
-
toIndex: 0,
|
|
380
|
-
...props,
|
|
381
|
-
}
|
|
382
|
-
const address = field.address.toString()
|
|
383
|
-
const fields = field.form.fields
|
|
384
|
-
const addrLength = address.length
|
|
385
|
-
const fieldPatches: INodePatch<GeneralField>[] = []
|
|
386
|
-
function isArrayChildren(identifier: string) {
|
|
387
|
-
return identifier.indexOf(address) === 0 && identifier.length > addrLength
|
|
388
|
-
}
|
|
389
|
-
|
|
390
|
-
const isDown = fromIndex < toIndex
|
|
391
|
-
|
|
392
|
-
function isMoveNode(identifier: string) {
|
|
393
|
-
const afterStr = identifier.slice(address.length)
|
|
394
|
-
const number = afterStr.match(NumberIndexReg)?.[1]
|
|
395
|
-
if (number === undefined) return false
|
|
396
|
-
const index = Number(number)
|
|
397
|
-
return isDown ? index > fromIndex && index <= toIndex : index < fromIndex && index >= toIndex
|
|
398
|
-
}
|
|
399
|
-
|
|
400
|
-
function isFromNode(identifier: string) {
|
|
401
|
-
const afterStr = identifier.substring(addrLength)
|
|
402
|
-
const number = afterStr.match(NumberIndexReg)?.[1]
|
|
403
|
-
if (number === undefined) return false
|
|
404
|
-
const index = Number(number)
|
|
405
|
-
return index === fromIndex
|
|
406
|
-
}
|
|
407
|
-
|
|
408
|
-
function moveIndex(identifier: string) {
|
|
409
|
-
const preStr = identifier.substring(0, addrLength)
|
|
410
|
-
const afterStr = identifier.substring(addrLength)
|
|
411
|
-
const number = afterStr.match(NumberIndexReg)?.[1]
|
|
412
|
-
const current = Number(number)
|
|
413
|
-
let index = current
|
|
414
|
-
if (index === fromIndex) {
|
|
415
|
-
index = toIndex
|
|
416
|
-
} else {
|
|
417
|
-
index += isDown ? -1 : 1
|
|
418
|
-
}
|
|
419
|
-
|
|
420
|
-
return `${preStr}${afterStr.replace(/^\.\d+/, `.${index}`)}`
|
|
421
|
-
}
|
|
422
|
-
|
|
423
|
-
batch(() => {
|
|
424
|
-
each(fields, (field, identifier) => {
|
|
425
|
-
if (isArrayChildren(identifier)) {
|
|
426
|
-
if (isMoveNode(identifier) || isFromNode(identifier)) {
|
|
427
|
-
const newIdentifier = moveIndex(identifier)
|
|
428
|
-
fieldPatches.push({
|
|
429
|
-
type: 'update',
|
|
430
|
-
address: newIdentifier,
|
|
431
|
-
oldAddress: identifier,
|
|
432
|
-
payload: field,
|
|
433
|
-
})
|
|
434
|
-
if (!fields[newIdentifier]) {
|
|
435
|
-
fieldPatches.push({
|
|
436
|
-
type: 'remove',
|
|
437
|
-
address: identifier,
|
|
438
|
-
})
|
|
439
|
-
}
|
|
440
|
-
}
|
|
441
|
-
}
|
|
442
|
-
})
|
|
443
|
-
patchFieldStates(fields, fieldPatches)
|
|
444
|
-
})
|
|
445
|
-
field.form.notify(LifeCycleTypes.ON_FORM_GRAPH_CHANGE)
|
|
446
|
-
}
|
|
447
|
-
|
|
448
|
-
export function cleanupArrayChildren(field: ArrayField<any>, start: number) {
|
|
449
|
-
const address = field.address.toString()
|
|
450
|
-
const fields = field.form.fields
|
|
451
|
-
|
|
452
|
-
function isArrayChildren(identifier: string) {
|
|
453
|
-
return identifier.indexOf(address) === 0 && identifier.length > address.length
|
|
454
|
-
}
|
|
455
|
-
|
|
456
|
-
function isNeedCleanup(identifier: string) {
|
|
457
|
-
const afterStr = identifier.slice(address.length)
|
|
458
|
-
const numStr = afterStr.match(NumberIndexReg)?.[1]
|
|
459
|
-
if (numStr === undefined) return false
|
|
460
|
-
const index = Number(numStr)
|
|
461
|
-
return index >= start
|
|
462
|
-
}
|
|
463
|
-
|
|
464
|
-
batch(() => {
|
|
465
|
-
each(fields, (field, identifier) => {
|
|
466
|
-
if (isArrayChildren(identifier) && isNeedCleanup(identifier)) {
|
|
467
|
-
field.destroy()
|
|
468
|
-
}
|
|
469
|
-
})
|
|
470
|
-
})
|
|
471
|
-
}
|
|
472
|
-
|
|
473
|
-
export function cleanupObjectChildren(field: ObjectField<any>, keys: string[]) {
|
|
474
|
-
if (keys.length === 0) return
|
|
475
|
-
const address = field.address.toString()
|
|
476
|
-
const fields = field.form.fields
|
|
477
|
-
|
|
478
|
-
function isObjectChildren(identifier: string) {
|
|
479
|
-
return identifier.indexOf(address) === 0 && identifier.length > address.length
|
|
480
|
-
}
|
|
481
|
-
|
|
482
|
-
function isNeedCleanup(identifier: string) {
|
|
483
|
-
const afterStr = identifier.slice(address.length)
|
|
484
|
-
const key = afterStr.match(/^\.([^.]+)/)?.[1]
|
|
485
|
-
if (key === undefined) return false
|
|
486
|
-
return keys.includes(key)
|
|
487
|
-
}
|
|
488
|
-
|
|
489
|
-
batch(() => {
|
|
490
|
-
each(fields, (field, identifier) => {
|
|
491
|
-
if (isObjectChildren(identifier) && isNeedCleanup(identifier)) {
|
|
492
|
-
field.destroy()
|
|
493
|
-
}
|
|
494
|
-
})
|
|
495
|
-
})
|
|
496
|
-
}
|
|
497
|
-
|
|
498
|
-
export const initFieldUpdate = batch.scope.bound((field: GeneralField) => {
|
|
499
|
-
const form = field.form
|
|
500
|
-
const updates = FormPath.ensureIn(form, 'requests.updates', [])
|
|
501
|
-
const indexes = FormPath.ensureIn(form, 'requests.updateIndexes', {})
|
|
502
|
-
for (let index = 0; index < updates.length; index++) {
|
|
503
|
-
const { pattern, callbacks } = updates[index]
|
|
504
|
-
let removed = false
|
|
505
|
-
if (field.match(pattern)) {
|
|
506
|
-
callbacks.forEach((callback: any) => {
|
|
507
|
-
field.setState(callback)
|
|
508
|
-
})
|
|
509
|
-
if (!pattern.isWildMatchPattern && !pattern.isMatchPattern) {
|
|
510
|
-
updates.splice(index--, 1)
|
|
511
|
-
removed = true
|
|
512
|
-
}
|
|
513
|
-
}
|
|
514
|
-
if (!removed) {
|
|
515
|
-
indexes[pattern.toString()] = index
|
|
516
|
-
} else {
|
|
517
|
-
delete indexes[pattern.toString()]
|
|
518
|
-
}
|
|
519
|
-
}
|
|
520
|
-
})
|
|
521
|
-
|
|
522
|
-
export function subscribeUpdate(form: Form, pattern: FormPath, callback: (...args: any[]) => void) {
|
|
523
|
-
const updates = FormPath.ensureIn(form, 'requests.updates', [])
|
|
524
|
-
const indexes = FormPath.ensureIn(form, 'requests.updateIndexes', {})
|
|
525
|
-
const id = pattern.toString()
|
|
526
|
-
const current = indexes[id]
|
|
527
|
-
if (isValid(current)) {
|
|
528
|
-
if (
|
|
529
|
-
updates[current] &&
|
|
530
|
-
!updates[current].callbacks.some((fn: any) => (fn.toString() === callback.toString() ? fn === callback : false))
|
|
531
|
-
) {
|
|
532
|
-
updates[current].callbacks.push(callback)
|
|
533
|
-
}
|
|
534
|
-
} else {
|
|
535
|
-
indexes[id] = updates.length
|
|
536
|
-
updates.push({
|
|
537
|
-
pattern,
|
|
538
|
-
callbacks: [callback],
|
|
539
|
-
})
|
|
540
|
-
}
|
|
541
|
-
}
|
|
542
|
-
|
|
543
|
-
export function deserialize(model: any, setter: any) {
|
|
544
|
-
if (!model) return
|
|
545
|
-
if (isFn(setter)) {
|
|
546
|
-
setter(model)
|
|
547
|
-
} else {
|
|
548
|
-
for (const key in setter) {
|
|
549
|
-
if (!hasOwnProperty.call(setter, key)) continue
|
|
550
|
-
if (ReadOnlyProperties[key] || ReservedProperties[key]) continue
|
|
551
|
-
const MutuallyExclusiveKey = MutuallyExclusiveProperties[key]
|
|
552
|
-
if (
|
|
553
|
-
MutuallyExclusiveKey &&
|
|
554
|
-
hasOwnProperty.call(setter, MutuallyExclusiveKey) &&
|
|
555
|
-
!isValid(setter[MutuallyExclusiveKey])
|
|
556
|
-
)
|
|
557
|
-
continue
|
|
558
|
-
const value = setter[key]
|
|
559
|
-
if (isFn(value)) continue
|
|
560
|
-
model[key] = value
|
|
561
|
-
}
|
|
562
|
-
}
|
|
563
|
-
return model
|
|
564
|
-
}
|
|
565
|
-
|
|
566
|
-
/** 收集实例自身属性与原型链上的访问器属性(装饰器生成的 accessor 不在实例自身上) */
|
|
567
|
-
function getSerializableKeys(model: any) {
|
|
568
|
-
const keys = Object.keys(model)
|
|
569
|
-
for (
|
|
570
|
-
let proto = Object.getPrototypeOf(model);
|
|
571
|
-
proto && proto !== Object.prototype;
|
|
572
|
-
proto = Object.getPrototypeOf(proto)
|
|
573
|
-
) {
|
|
574
|
-
for (const key of Object.getOwnPropertyNames(proto)) {
|
|
575
|
-
if (keys.indexOf(key) > -1) continue
|
|
576
|
-
const descriptor = Object.getOwnPropertyDescriptor(proto, key)
|
|
577
|
-
if (descriptor && (descriptor.get || descriptor.set)) keys.push(key)
|
|
578
|
-
}
|
|
579
|
-
}
|
|
580
|
-
return keys
|
|
581
|
-
}
|
|
582
|
-
|
|
583
|
-
export function serialize(model: any, getter?: any) {
|
|
584
|
-
if (isFn(getter)) {
|
|
585
|
-
return getter(model)
|
|
586
|
-
} else {
|
|
587
|
-
const results: Record<string, any> = {}
|
|
588
|
-
for (const key of getSerializableKeys(model)) {
|
|
589
|
-
if (ReservedProperties[key]) continue
|
|
590
|
-
if (key === 'address' || key === 'path') {
|
|
591
|
-
results[key] = model[key].toString()
|
|
592
|
-
continue
|
|
593
|
-
}
|
|
594
|
-
const value = model[key]
|
|
595
|
-
if (isFn(value)) continue
|
|
596
|
-
results[key] = toJS(value)
|
|
597
|
-
}
|
|
598
|
-
return results
|
|
599
|
-
}
|
|
600
|
-
}
|
|
601
|
-
|
|
602
|
-
export function createChildrenFeedbackFilter(field: Field) {
|
|
603
|
-
const identifier = field.address?.toString()
|
|
604
|
-
return ({ address }: IFormFeedback) => {
|
|
605
|
-
if (!address) return false
|
|
606
|
-
return address === identifier || address.indexOf(identifier + '.') === 0
|
|
607
|
-
}
|
|
608
|
-
}
|
|
609
|
-
|
|
610
|
-
export function createStateSetter(model: any) {
|
|
611
|
-
return batch.bound((setter?: any) => deserialize(model, setter))
|
|
612
|
-
}
|
|
613
|
-
|
|
614
|
-
export function createStateGetter(model: any) {
|
|
615
|
-
return (getter?: any) => serialize(model, getter)
|
|
616
|
-
}
|
|
617
|
-
|
|
618
|
-
export function createBatchStateSetter(form: Form) {
|
|
619
|
-
return batch.bound((pattern: FieldMatchPattern, payload?: any) => {
|
|
620
|
-
if (isQuery(pattern)) {
|
|
621
|
-
pattern.forEach(field => {
|
|
622
|
-
field.setState(payload)
|
|
623
|
-
})
|
|
624
|
-
} else if (isGeneralField(pattern)) {
|
|
625
|
-
pattern.setState(payload)
|
|
626
|
-
} else {
|
|
627
|
-
let matchCount = 0,
|
|
628
|
-
path = FormPath.parse(pattern)
|
|
629
|
-
form.query(path).forEach(field => {
|
|
630
|
-
field.setState(payload)
|
|
631
|
-
matchCount++
|
|
632
|
-
})
|
|
633
|
-
|
|
634
|
-
if (matchCount === 0 || path.isWildMatchPattern) {
|
|
635
|
-
subscribeUpdate(form, path, payload)
|
|
636
|
-
}
|
|
637
|
-
}
|
|
638
|
-
})
|
|
639
|
-
}
|
|
640
|
-
|
|
641
|
-
export function createBatchStateGetter(form: Form) {
|
|
642
|
-
return (pattern: FieldMatchPattern, payload?: any) => {
|
|
643
|
-
if (isQuery(pattern)) {
|
|
644
|
-
return pattern.take(payload)
|
|
645
|
-
} else if (isGeneralField(pattern)) {
|
|
646
|
-
return (pattern as any).getState(payload)
|
|
647
|
-
} else {
|
|
648
|
-
return form.query(pattern).take((field: any) => {
|
|
649
|
-
return field.getState(payload)
|
|
650
|
-
})
|
|
651
|
-
}
|
|
652
|
-
}
|
|
653
|
-
}
|
|
654
|
-
|
|
655
|
-
export function triggerFormInitialValuesChange(form: Form, change: DataChange) {
|
|
656
|
-
if (Array.isArray(change.target) && change.key === 'length') return
|
|
657
|
-
if (contains(form.initialValues, change.target) || form.initialValues === change.value) {
|
|
658
|
-
if (change.type === 'add' || change.type === 'set') {
|
|
659
|
-
// 直接观察 initialValues 根对象,path 不含变更键,需要拼上 key 才是完整数据路径
|
|
660
|
-
patchFormValues(form, [...change.path, change.key] as (string | number)[], change.value)
|
|
661
|
-
}
|
|
662
|
-
if (form.initialized) {
|
|
663
|
-
form.notify(LifeCycleTypes.ON_FORM_INITIAL_VALUES_CHANGE)
|
|
664
|
-
}
|
|
665
|
-
}
|
|
666
|
-
}
|
|
667
|
-
|
|
668
|
-
export function triggerFormValuesChange(form: Form, change: DataChange) {
|
|
669
|
-
if (Array.isArray(change.target) && change.key === 'length') return
|
|
670
|
-
if ((contains(form.values, change.target) || form.values === change.value) && form.initialized) {
|
|
671
|
-
form.notify(LifeCycleTypes.ON_FORM_VALUES_CHANGE)
|
|
672
|
-
}
|
|
673
|
-
}
|
|
674
|
-
|
|
675
|
-
export function setValidating(target: Form | Field, validating?: boolean) {
|
|
676
|
-
clearTimeout(target.requests.validate)
|
|
677
|
-
if (validating) {
|
|
678
|
-
target.requests.validate = setTimeout(() => {
|
|
679
|
-
batch(() => {
|
|
680
|
-
target.validating = validating
|
|
681
|
-
notify(target, LifeCycleTypes.ON_FORM_VALIDATING, LifeCycleTypes.ON_FIELD_VALIDATING)
|
|
682
|
-
})
|
|
683
|
-
}, RESPONSE_REQUEST_DURATION)
|
|
684
|
-
notify(target, LifeCycleTypes.ON_FORM_VALIDATE_START, LifeCycleTypes.ON_FIELD_VALIDATE_START)
|
|
685
|
-
} else {
|
|
686
|
-
if (target.validating !== validating) {
|
|
687
|
-
target.validating = validating
|
|
688
|
-
}
|
|
689
|
-
notify(target, LifeCycleTypes.ON_FORM_VALIDATE_END, LifeCycleTypes.ON_FIELD_VALIDATE_END)
|
|
690
|
-
}
|
|
691
|
-
}
|
|
692
|
-
|
|
693
|
-
export function setSubmitting(target: Form | Field, submitting?: boolean) {
|
|
694
|
-
clearTimeout(target.requests.submit)
|
|
695
|
-
if (submitting) {
|
|
696
|
-
target.requests.submit = setTimeout(() => {
|
|
697
|
-
batch(() => {
|
|
698
|
-
target.submitting = submitting
|
|
699
|
-
notify(target, LifeCycleTypes.ON_FORM_SUBMITTING, LifeCycleTypes.ON_FIELD_SUBMITTING)
|
|
700
|
-
})
|
|
701
|
-
}, RESPONSE_REQUEST_DURATION)
|
|
702
|
-
notify(target, LifeCycleTypes.ON_FORM_SUBMIT_START, LifeCycleTypes.ON_FIELD_SUBMIT_START)
|
|
703
|
-
} else {
|
|
704
|
-
if (target.submitting !== submitting) {
|
|
705
|
-
target.submitting = submitting
|
|
706
|
-
}
|
|
707
|
-
notify(target, LifeCycleTypes.ON_FORM_SUBMIT_END, LifeCycleTypes.ON_FIELD_SUBMIT_END)
|
|
708
|
-
}
|
|
709
|
-
}
|
|
710
|
-
|
|
711
|
-
export function setLoading(target: Form | Field, loading?: boolean) {
|
|
712
|
-
clearTimeout(target.requests.loading)
|
|
713
|
-
if (loading) {
|
|
714
|
-
target.requests.loading = setTimeout(() => {
|
|
715
|
-
batch(() => {
|
|
716
|
-
target.loading = loading
|
|
717
|
-
notify(target, LifeCycleTypes.ON_FORM_LOADING, LifeCycleTypes.ON_FIELD_LOADING)
|
|
718
|
-
})
|
|
719
|
-
}, RESPONSE_REQUEST_DURATION)
|
|
720
|
-
} else if (target.loading !== loading) {
|
|
721
|
-
target.loading = loading
|
|
722
|
-
}
|
|
723
|
-
}
|
|
724
|
-
|
|
725
|
-
export async function batchSubmit<T>(target: Form | Field, onSubmit?: (values: any) => Promise<T> | void): Promise<T> {
|
|
726
|
-
function getValues(target: Form | Field) {
|
|
727
|
-
if (isForm(target)) {
|
|
728
|
-
return toJS(target.values)
|
|
729
|
-
}
|
|
730
|
-
return toJS(target.value)
|
|
731
|
-
}
|
|
732
|
-
target.setSubmitting(true)
|
|
733
|
-
try {
|
|
734
|
-
notify(target, LifeCycleTypes.ON_FORM_SUBMIT_VALIDATE_START, LifeCycleTypes.ON_FIELD_SUBMIT_VALIDATE_START)
|
|
735
|
-
await target.validate()
|
|
736
|
-
notify(target, LifeCycleTypes.ON_FORM_SUBMIT_VALIDATE_SUCCESS, LifeCycleTypes.ON_FIELD_SUBMIT_VALIDATE_SUCCESS)
|
|
737
|
-
} catch (e) {
|
|
738
|
-
notify(target, LifeCycleTypes.ON_FORM_SUBMIT_VALIDATE_FAILED, LifeCycleTypes.ON_FIELD_SUBMIT_VALIDATE_FAILED)
|
|
739
|
-
}
|
|
740
|
-
notify(target, LifeCycleTypes.ON_FORM_SUBMIT_VALIDATE_END, LifeCycleTypes.ON_FIELD_SUBMIT_VALIDATE_END)
|
|
741
|
-
let results: any
|
|
742
|
-
try {
|
|
743
|
-
if (target.invalid) {
|
|
744
|
-
throw target.errors
|
|
745
|
-
}
|
|
746
|
-
if (isFn(onSubmit)) {
|
|
747
|
-
results = await onSubmit(getValues(target))
|
|
748
|
-
} else {
|
|
749
|
-
results = getValues(target)
|
|
750
|
-
}
|
|
751
|
-
notify(target, LifeCycleTypes.ON_FORM_SUBMIT_SUCCESS, LifeCycleTypes.ON_FIELD_SUBMIT_SUCCESS)
|
|
752
|
-
} catch (e) {
|
|
753
|
-
target.setSubmitting(false)
|
|
754
|
-
notify(target, LifeCycleTypes.ON_FORM_SUBMIT_FAILED, LifeCycleTypes.ON_FIELD_SUBMIT_FAILED)
|
|
755
|
-
notify(target, LifeCycleTypes.ON_FORM_SUBMIT, LifeCycleTypes.ON_FIELD_SUBMIT)
|
|
756
|
-
throw e
|
|
757
|
-
}
|
|
758
|
-
target.setSubmitting(false)
|
|
759
|
-
notify(target, LifeCycleTypes.ON_FORM_SUBMIT, LifeCycleTypes.ON_FIELD_SUBMIT)
|
|
760
|
-
return results
|
|
761
|
-
}
|
|
762
|
-
|
|
763
|
-
function shouldValidate(field: Field) {
|
|
764
|
-
const validatePattern = field.props.validatePattern ?? field.form.props.validatePattern ?? ['editable']
|
|
765
|
-
const validateDisplay = field.props.validateDisplay ?? field.form.props.validateDisplay ?? ['visible']
|
|
766
|
-
return validatePattern.includes(field.pattern) && validateDisplay.includes(field.display)
|
|
767
|
-
}
|
|
768
|
-
|
|
769
|
-
export async function batchValidate(
|
|
770
|
-
target: Form | Field,
|
|
771
|
-
pattern: FormPathPattern,
|
|
772
|
-
triggerType?: ValidatorTriggerType
|
|
773
|
-
) {
|
|
774
|
-
if (isForm(target)) target.setValidating(true)
|
|
775
|
-
else {
|
|
776
|
-
if (!shouldValidate(target)) return
|
|
777
|
-
}
|
|
778
|
-
const tasks: any[] = []
|
|
779
|
-
target.query(pattern).forEach(field => {
|
|
780
|
-
if (!isVoidField(field)) {
|
|
781
|
-
tasks.push(validateSelf(field, triggerType, field === target))
|
|
782
|
-
}
|
|
783
|
-
})
|
|
784
|
-
await Promise.all(tasks)
|
|
785
|
-
if (isForm(target)) target.setValidating(false)
|
|
786
|
-
if (target.invalid) {
|
|
787
|
-
notify(target, LifeCycleTypes.ON_FORM_VALIDATE_FAILED, LifeCycleTypes.ON_FIELD_VALIDATE_FAILED)
|
|
788
|
-
throw target.errors
|
|
789
|
-
}
|
|
790
|
-
notify(target, LifeCycleTypes.ON_FORM_VALIDATE_SUCCESS, LifeCycleTypes.ON_FIELD_VALIDATE_SUCCESS)
|
|
791
|
-
}
|
|
792
|
-
|
|
793
|
-
export async function batchReset(target: Form | Field, pattern: FormPathPattern, options?: IFieldResetOptions) {
|
|
794
|
-
const tasks: Promise<any>[] = []
|
|
795
|
-
target.query(pattern).forEach(field => {
|
|
796
|
-
if (!isVoidField(field)) {
|
|
797
|
-
tasks.push(resetSelf(field, options, target === field))
|
|
798
|
-
}
|
|
799
|
-
})
|
|
800
|
-
if (isForm(target)) {
|
|
801
|
-
target.modified = false
|
|
802
|
-
}
|
|
803
|
-
notify(target, LifeCycleTypes.ON_FORM_RESET, LifeCycleTypes.ON_FIELD_RESET)
|
|
804
|
-
await Promise.all(tasks)
|
|
805
|
-
}
|
|
806
|
-
|
|
807
|
-
export const validateSelf = batch.bound(async (target: Field, triggerType?: ValidatorTriggerType, noEmit = false) => {
|
|
808
|
-
function start() {
|
|
809
|
-
setValidating(target, true)
|
|
810
|
-
}
|
|
811
|
-
function end() {
|
|
812
|
-
setValidating(target, false)
|
|
813
|
-
if (noEmit) return
|
|
814
|
-
if (target.selfValid) {
|
|
815
|
-
target.notify(LifeCycleTypes.ON_FIELD_VALIDATE_SUCCESS)
|
|
816
|
-
} else {
|
|
817
|
-
target.notify(LifeCycleTypes.ON_FIELD_VALIDATE_FAILED)
|
|
818
|
-
}
|
|
819
|
-
}
|
|
820
|
-
|
|
821
|
-
if (!shouldValidate(target)) return {}
|
|
822
|
-
start()
|
|
823
|
-
if (!triggerType) {
|
|
824
|
-
const allTriggerTypes = parseValidatorDescriptions(target.validator).reduce(
|
|
825
|
-
(types, desc) => (types.indexOf(desc.triggerType) > -1 ? types : types.concat(desc.triggerType)),
|
|
826
|
-
[]
|
|
827
|
-
)
|
|
828
|
-
const results: Record<string, any> = {}
|
|
829
|
-
for (let i = 0; i < allTriggerTypes.length; i++) {
|
|
830
|
-
const payload = await validateToFeedbacks(target, allTriggerTypes[i])
|
|
831
|
-
each(payload, (result, key) => {
|
|
832
|
-
results[key] = results[key] || []
|
|
833
|
-
results[key] = results[key].concat(result)
|
|
834
|
-
})
|
|
835
|
-
}
|
|
836
|
-
end()
|
|
837
|
-
return results
|
|
838
|
-
}
|
|
839
|
-
const results = await validateToFeedbacks(target, triggerType)
|
|
840
|
-
end()
|
|
841
|
-
return results
|
|
842
|
-
})
|
|
843
|
-
|
|
844
|
-
export const resetSelf = batch.bound(async (target: Field, options?: IFieldResetOptions, noEmit = false) => {
|
|
845
|
-
const typedDefaultValue = getTypedDefaultValue(target)
|
|
846
|
-
target.modified = false
|
|
847
|
-
target.selfModified = false
|
|
848
|
-
target.visited = false
|
|
849
|
-
target.feedbacks = []
|
|
850
|
-
target.inputValue = typedDefaultValue
|
|
851
|
-
target.inputValues = []
|
|
852
|
-
target.caches = {}
|
|
853
|
-
if (!isUndef(target.value)) {
|
|
854
|
-
if (options?.forceClear) {
|
|
855
|
-
target.value = typedDefaultValue
|
|
856
|
-
} else {
|
|
857
|
-
const initialValue = target.initialValue
|
|
858
|
-
target.value = toJS(!isUndef(initialValue) ? initialValue : typedDefaultValue)
|
|
859
|
-
}
|
|
860
|
-
}
|
|
861
|
-
if (!noEmit) {
|
|
862
|
-
target.notify(LifeCycleTypes.ON_FIELD_RESET)
|
|
863
|
-
}
|
|
864
|
-
if (options?.validate) {
|
|
865
|
-
return await validateSelf(target)
|
|
866
|
-
}
|
|
867
|
-
})
|
|
868
|
-
|
|
869
|
-
export function modifySelf(target: Field) {
|
|
870
|
-
if (target.selfModified) return
|
|
871
|
-
target.selfModified = true
|
|
872
|
-
target.modified = true
|
|
873
|
-
let parent = target.parent
|
|
874
|
-
while (parent) {
|
|
875
|
-
if (isDataField(parent)) {
|
|
876
|
-
if (parent.modified) return
|
|
877
|
-
parent.modified = true
|
|
878
|
-
}
|
|
879
|
-
parent = parent.parent
|
|
880
|
-
}
|
|
881
|
-
target.form.modified = true
|
|
882
|
-
}
|
|
883
|
-
|
|
884
|
-
export function getValidFormValues(values: any) {
|
|
885
|
-
if (isObservable(values)) return values
|
|
886
|
-
return clone(values || {})
|
|
887
|
-
}
|
|
888
|
-
|
|
889
|
-
export function getValidFieldDefaultValue(value: any, initialValue: any) {
|
|
890
|
-
if (allowAssignDefaultValue(value, initialValue)) return clone(initialValue)
|
|
891
|
-
return value
|
|
892
|
-
}
|
|
893
|
-
|
|
894
|
-
export function allowAssignDefaultValue(target: any, source: any) {
|
|
895
|
-
const isValidTarget = !isUndef(target)
|
|
896
|
-
const isValidSource = !isUndef(source)
|
|
897
|
-
if (!isValidTarget) {
|
|
898
|
-
return isValidSource
|
|
899
|
-
}
|
|
900
|
-
|
|
901
|
-
if (typeof target === typeof source) {
|
|
902
|
-
if (target === '') return false
|
|
903
|
-
if (target === 0) return false
|
|
904
|
-
}
|
|
905
|
-
|
|
906
|
-
const isEmptyTarget = target !== null && isEmpty(target, true)
|
|
907
|
-
const isEmptySource = source !== null && isEmpty(source, true)
|
|
908
|
-
if (isEmptyTarget) {
|
|
909
|
-
return !isEmptySource
|
|
910
|
-
}
|
|
911
|
-
return false
|
|
912
|
-
}
|
|
913
|
-
|
|
914
|
-
export function createReactions(field: GeneralField) {
|
|
915
|
-
const reactions = toArr(field.props.reactions)
|
|
916
|
-
field.form.addEffects(field, () => {
|
|
917
|
-
reactions.forEach(reaction => {
|
|
918
|
-
if (isFn(reaction)) {
|
|
919
|
-
field.disposers.push(
|
|
920
|
-
autorun(
|
|
921
|
-
batch.scope.bound(() => {
|
|
922
|
-
if (field.destroyed) return
|
|
923
|
-
reaction(field)
|
|
924
|
-
})
|
|
925
|
-
)
|
|
926
|
-
)
|
|
927
|
-
}
|
|
928
|
-
})
|
|
929
|
-
})
|
|
930
|
-
}
|
|
931
|
-
|
|
932
|
-
export function createReaction<T>(tracker: () => T, scheduler?: (value: T) => void) {
|
|
933
|
-
return reaction(tracker, untracked.bound(scheduler))
|
|
934
|
-
}
|
|
935
|
-
|
|
936
|
-
export function initializeStart() {
|
|
937
|
-
GlobalState.initializing = true
|
|
938
|
-
}
|
|
939
|
-
|
|
940
|
-
export function initializeEnd() {
|
|
941
|
-
batch.endpoint(() => {
|
|
942
|
-
GlobalState.initializing = false
|
|
943
|
-
})
|
|
944
|
-
}
|
|
945
|
-
|
|
946
|
-
export function getArrayParent(field: BaseField): ArrayField | undefined {
|
|
947
|
-
const index = field.index
|
|
948
|
-
if (index === -1) return
|
|
949
|
-
let parent: any = field.parent
|
|
950
|
-
while (parent) {
|
|
951
|
-
if (isArrayField(parent)) return parent
|
|
952
|
-
if (parent === field.form) return
|
|
953
|
-
parent = parent.parent
|
|
954
|
-
}
|
|
955
|
-
}
|
|
956
|
-
|
|
957
|
-
export function getObjectParent(field: BaseField): ObjectField | undefined {
|
|
958
|
-
let parent: any = field.parent
|
|
959
|
-
while (parent) {
|
|
960
|
-
if (isArrayField(parent)) return
|
|
961
|
-
if (isObjectField(parent)) return parent
|
|
962
|
-
if (parent === field.form) return
|
|
963
|
-
parent = parent.parent
|
|
964
|
-
}
|
|
965
|
-
}
|