@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.
Files changed (43) hide show
  1. package/CHANGELOG.md +14 -0
  2. package/README.md +63 -0
  3. package/package.json +8 -4
  4. package/src/__tests__/array.spec.ts +0 -168
  5. package/src/__tests__/effects.spec.ts +0 -595
  6. package/src/__tests__/externals.spec.ts +0 -101
  7. package/src/__tests__/field.spec.ts +0 -572
  8. package/src/__tests__/form.spec.ts +0 -720
  9. package/src/__tests__/graph.spec.ts +0 -56
  10. package/src/__tests__/heart.spec.ts +0 -60
  11. package/src/__tests__/internals.spec.ts +0 -138
  12. package/src/__tests__/lifecycle.spec.ts +0 -51
  13. package/src/__tests__/object.spec.ts +0 -107
  14. package/src/__tests__/query.spec.ts +0 -100
  15. package/src/__tests__/shared.ts +0 -10
  16. package/src/__tests__/typed.spec.ts +0 -52
  17. package/src/__tests__/void.spec.ts +0 -163
  18. package/src/effects/index.ts +0 -2
  19. package/src/effects/onFieldEffects.ts +0 -97
  20. package/src/effects/onFormEffects.ts +0 -48
  21. package/src/global.d.ts +0 -8
  22. package/src/index.ts +0 -4
  23. package/src/models/ArrayField.ts +0 -133
  24. package/src/models/BaseField.ts +0 -359
  25. package/src/models/Field.ts +0 -450
  26. package/src/models/Form.ts +0 -586
  27. package/src/models/Graph.ts +0 -56
  28. package/src/models/Heart.ts +0 -75
  29. package/src/models/LifeCycle.ts +0 -42
  30. package/src/models/ObjectField.ts +0 -48
  31. package/src/models/Query.ts +0 -97
  32. package/src/models/VoidField.ts +0 -64
  33. package/src/models/index.ts +0 -9
  34. package/src/models/types.ts +0 -9
  35. package/src/shared/checkers.ts +0 -85
  36. package/src/shared/constants.ts +0 -60
  37. package/src/shared/effective.ts +0 -69
  38. package/src/shared/externals.ts +0 -65
  39. package/src/shared/internals.ts +0 -965
  40. package/src/types.ts +0 -401
  41. package/tsconfig.build.json +0 -9
  42. package/tsconfig.json +0 -4
  43. package/vite.config.ts +0 -46
@@ -1,450 +0,0 @@
1
- import { action, batch, computed, shallow, shallowRef, toJS } from '@formvk/reactive'
2
- import type { FormPathPattern } from '@formvk/shared'
3
- import { isArr, isEmpty, isValid, toArr } from '@formvk/shared'
4
- import type { ValidatorTriggerType } from '@formvk/validator'
5
- import { parseValidatorDescriptions } from '@formvk/validator'
6
-
7
- import {
8
- allowAssignDefaultValue,
9
- batchReset,
10
- batchSubmit,
11
- batchValidate,
12
- createChildrenFeedbackFilter,
13
- createReaction,
14
- createReactions,
15
- createStateGetter,
16
- createStateSetter,
17
- getValidFieldDefaultValue,
18
- getValuesFromEvent,
19
- initializeEnd,
20
- initializeStart,
21
- isHTMLInputEvent,
22
- modifySelf,
23
- queryFeedbackMessages,
24
- queryFeedbacks,
25
- setLoading,
26
- setSubmitting,
27
- setValidating,
28
- setValidatorRule,
29
- updateFeedback,
30
- validateSelf,
31
- } from '../shared/internals'
32
- import type {
33
- FeedbackMessage,
34
- FieldDataSource,
35
- FieldValidator,
36
- IFieldCaches,
37
- IFieldFeedback,
38
- IFieldProps,
39
- IFieldRequests,
40
- IFieldResetOptions,
41
- IFieldState,
42
- IFormFeedback,
43
- IModelGetter,
44
- IModelSetter,
45
- ISearchFeedback,
46
- } from '../types'
47
- import { LifeCycleTypes } from '../types'
48
- import { BaseField } from './BaseField'
49
- import type { Form } from './Form'
50
- export class Field<
51
- Value = any,
52
- ComponentProps = any,
53
- DecoratorProps = any,
54
- IRecord extends object = any,
55
- > extends BaseField<ComponentProps, DecoratorProps, IRecord> {
56
- displayName = 'Field'
57
-
58
- props: IFieldProps<any, any, any, Value>
59
-
60
- @shallowRef accessor loading: boolean | undefined = false
61
- @shallowRef accessor validating: boolean | undefined = false
62
- @shallowRef accessor submitting: boolean | undefined = false
63
- @shallowRef accessor active = false
64
- @shallowRef accessor visited = false
65
- @shallowRef accessor selfModified = false
66
- @shallowRef accessor modified = false
67
- @shallowRef accessor inputValue: Value = undefined as any
68
- @shallowRef accessor inputValues: any[] = []
69
- @shallowRef accessor dataSource: FieldDataSource | undefined = undefined
70
- @shallow accessor validator: FieldValidator | undefined = undefined
71
- @shallowRef accessor feedbacks: IFieldFeedback[] = []
72
- caches: IFieldCaches = {}
73
- requests: IFieldRequests = {}
74
- constructor(address: FormPathPattern, props: IFieldProps<any, any, any, Value>, form: Form, designable?: boolean) {
75
- super()
76
- this.form = form
77
- this.props = props
78
- this.designable = designable
79
- initializeStart()
80
- this.locate(address)
81
- this.initialize()
82
- this.makeReactive()
83
- this.onInit()
84
- initializeEnd()
85
- }
86
-
87
- protected initialize() {
88
- this.initialized = false
89
- this.loading = false
90
- this.validating = false
91
- this.submitting = false
92
- this.selfModified = false
93
- this.active = false
94
- this.visited = false
95
- this.mounted = false
96
- this.unmounted = false
97
- this.inputValues = []
98
- this.inputValue = null!
99
- this.feedbacks = []
100
- this.title = this.props.title!
101
- this.description = this.props.description!
102
- this.display = this.props.display!
103
- this.pattern = this.props.pattern!
104
- this.editable = this.props.editable!
105
- this.disabled = this.props.disabled!
106
- this.readOnly = this.props.readOnly!
107
- this.readPretty = this.props.readPretty!
108
- this.visible = this.props.visible!
109
- this.hidden = this.props.hidden!
110
- this.dataSource = this.props.dataSource!
111
- this.validator = this.props.validator!
112
- this.required = this.props.required!
113
- this.content = this.props.content!
114
- this.initialValue = this.props.initialValue!
115
- this.value = this.props.value!
116
- this.data = this.props.data!
117
- this.decorator = toArr(this.props.decorator)
118
- this.component = toArr(this.props.component)
119
- }
120
-
121
- protected makeReactive() {
122
- if (this.designable) return
123
- this.disposers.push(
124
- createReaction(
125
- () => this.value,
126
- value => {
127
- this.notify(LifeCycleTypes.ON_FIELD_VALUE_CHANGE)
128
- if (isValid(value)) {
129
- if (this.selfModified && !this.caches.inputting) {
130
- validateSelf(this)
131
- }
132
- if (!isEmpty(value) && this.display === 'none') {
133
- this.caches.value = toJS(value)
134
- this.form.deleteValuesIn(this.path)
135
- }
136
- }
137
- }
138
- ),
139
- createReaction(
140
- () => this.initialValue,
141
- () => {
142
- this.notify(LifeCycleTypes.ON_FIELD_INITIAL_VALUE_CHANGE)
143
- }
144
- ),
145
- createReaction(
146
- () => this.display,
147
- display => {
148
- const value = this.value
149
- if (display !== 'none') {
150
- if (value === undefined && this.caches.value !== undefined) {
151
- this.setValue(this.caches.value)
152
- this.caches.value = undefined
153
- }
154
- } else {
155
- this.caches.value = toJS(value) ?? toJS(this.initialValue)
156
- this.form.deleteValuesIn(this.path)
157
- }
158
- if (display === 'none' || display === 'hidden') {
159
- this.setFeedback({
160
- type: 'error',
161
- messages: [],
162
- })
163
- }
164
- }
165
- ),
166
- createReaction(
167
- () => this.pattern,
168
- pattern => {
169
- if (pattern !== 'editable') {
170
- this.setFeedback({
171
- type: 'error',
172
- messages: [],
173
- })
174
- }
175
- }
176
- )
177
- )
178
- createReactions(this)
179
- }
180
-
181
- @computed
182
- get selfErrors(): FeedbackMessage {
183
- return queryFeedbackMessages(this, {
184
- type: 'error',
185
- })
186
- }
187
-
188
- @computed
189
- get errors(): IFormFeedback[] {
190
- return this.form.errors.filter(createChildrenFeedbackFilter(this))
191
- }
192
-
193
- @computed
194
- get selfWarnings(): FeedbackMessage {
195
- return queryFeedbackMessages(this, {
196
- type: 'warning',
197
- })
198
- }
199
-
200
- @computed
201
- get warnings(): IFormFeedback[] {
202
- return this.form.warnings.filter(createChildrenFeedbackFilter(this))
203
- }
204
-
205
- @computed
206
- get selfSuccesses(): FeedbackMessage {
207
- return queryFeedbackMessages(this, {
208
- type: 'success',
209
- })
210
- }
211
-
212
- @computed
213
- get successes(): IFormFeedback[] {
214
- return this.form.successes.filter(createChildrenFeedbackFilter(this))
215
- }
216
-
217
- @computed
218
- get selfValid() {
219
- return !this.selfErrors.length
220
- }
221
-
222
- @computed
223
- get valid() {
224
- return !this.errors.length
225
- }
226
-
227
- @computed
228
- get selfInvalid() {
229
- return !this.selfValid
230
- }
231
-
232
- @computed
233
- get invalid() {
234
- return !this.valid
235
- }
236
-
237
- @computed
238
- get value(): Value {
239
- return this.form.getValuesIn(this.path)
240
- }
241
-
242
- @computed
243
- get initialValue(): Value {
244
- return this.form.getInitialValuesIn(this.path)
245
- }
246
-
247
- @computed
248
- get required() {
249
- const validators = isArr(this.validator) ? this.validator : parseValidatorDescriptions(this.validator)
250
- return validators.some(desc => !!(desc as Record<string, any>)?.['required'])
251
- }
252
-
253
- @computed
254
- get validateStatus() {
255
- if (this.validating) return 'validating'
256
- if (this.selfInvalid) return 'error'
257
- if (this.selfWarnings.length) return 'warning'
258
- if (this.selfSuccesses.length) return 'success'
259
- }
260
-
261
- set required(required: boolean | undefined) {
262
- if (this.required === required) return
263
- this.setValidatorRule('required', required)
264
- }
265
-
266
- set value(value: Value | undefined) {
267
- this.setValue(value)
268
- }
269
-
270
- set initialValue(initialValue: Value) {
271
- this.setInitialValue(initialValue)
272
- }
273
-
274
- set selfErrors(messages: FeedbackMessage | undefined) {
275
- this.setFeedback({
276
- type: 'error',
277
- code: 'EffectError',
278
- messages,
279
- })
280
- }
281
-
282
- set selfWarnings(messages: FeedbackMessage | undefined) {
283
- this.setFeedback({
284
- type: 'warning',
285
- code: 'EffectWarning',
286
- messages,
287
- })
288
- }
289
-
290
- set selfSuccesses(messages: FeedbackMessage | undefined) {
291
- this.setFeedback({
292
- type: 'success',
293
- code: 'EffectSuccess',
294
- messages,
295
- })
296
- }
297
-
298
- @action
299
- setDataSource = (dataSource?: FieldDataSource) => {
300
- this.dataSource = dataSource
301
- }
302
-
303
- @action
304
- setFeedback = (feedback?: IFieldFeedback) => {
305
- updateFeedback(this, feedback)
306
- }
307
-
308
- @action
309
- setSelfErrors = (messages?: FeedbackMessage) => {
310
- this.selfErrors = messages
311
- }
312
-
313
- @action
314
- setSelfWarnings = (messages?: FeedbackMessage) => {
315
- this.selfWarnings = messages
316
- }
317
-
318
- @action
319
- setSelfSuccesses = (messages?: FeedbackMessage) => {
320
- this.selfSuccesses = messages
321
- }
322
-
323
- @action
324
- setValidator = (validator?: FieldValidator) => {
325
- this.validator = validator
326
- }
327
-
328
- @action
329
- setValidatorRule = (name: string, value: any) => {
330
- setValidatorRule(this, name, value)
331
- }
332
-
333
- @action
334
- setRequired = (required?: boolean) => {
335
- this.required = required
336
- }
337
-
338
- @action
339
- setValue = (value?: Value) => {
340
- if (this.destroyed) return
341
- if (!this.initialized) {
342
- if (this.display === 'none') {
343
- this.caches.value = value
344
- return
345
- }
346
- value = getValidFieldDefaultValue(value, this.initialValue)
347
- if (!allowAssignDefaultValue(this.value, value) && !this.designable) {
348
- return
349
- }
350
- }
351
- this.form.setValuesIn(this.path, value)
352
- }
353
-
354
- @action
355
- setInitialValue = (initialValue?: Value) => {
356
- if (this.destroyed) return
357
- if (!this.initialized) {
358
- if (!allowAssignDefaultValue(this.initialValue, initialValue) && !this.designable) {
359
- return
360
- }
361
- }
362
- this.form.setInitialValuesIn(this.path, initialValue)
363
- }
364
-
365
- @action
366
- setLoading = (loading?: boolean) => {
367
- setLoading(this, loading)
368
- }
369
-
370
- @action
371
- setValidating = (validating?: boolean) => {
372
- setValidating(this, validating)
373
- }
374
-
375
- @action
376
- setSubmitting = (submitting?: boolean) => {
377
- setSubmitting(this, submitting)
378
- }
379
-
380
- setState: IModelSetter<IFieldState> = createStateSetter(this)
381
-
382
- getState: IModelGetter<IFieldState> = createStateGetter(this)
383
-
384
- @batch
385
- onInput = async (...args: any[]) => {
386
- function isHTMLInputEventFromSelf(args: any[]) {
387
- return isHTMLInputEvent(args[0]) && 'currentTarget' in args[0] ? args[0]?.target === args[0]?.currentTarget : true
388
- }
389
- function getValues(args: any[]) {
390
- if (args[0]?.target) {
391
- if (!isHTMLInputEvent(args[0])) return args
392
- }
393
- return getValuesFromEvent(args)
394
- }
395
-
396
- if (!isHTMLInputEventFromSelf(args)) return
397
-
398
- const values = getValues(args)
399
- const value = values[0]
400
- this.caches.inputting = true
401
- this.inputValue = value
402
- this.inputValues = values
403
- this.value = value
404
- this.modify()
405
- this.notify(LifeCycleTypes.ON_FIELD_INPUT_VALUE_CHANGE)
406
- this.notify(LifeCycleTypes.ON_FORM_INPUT_CHANGE, this.form)
407
- await validateSelf(this, 'onInput')
408
- this.caches.inputting = false
409
- }
410
-
411
- @batch
412
- onFocus = async (...args: any[]) => {
413
- if (args[0]?.target) {
414
- if (!isHTMLInputEvent(args[0], false)) return
415
- }
416
- this.active = true
417
- this.visited = true
418
- await validateSelf(this, 'onFocus')
419
- }
420
-
421
- @batch
422
- onBlur = async (...args: any[]) => {
423
- if (args[0]?.target) {
424
- if (!isHTMLInputEvent(args[0], false)) return
425
- }
426
- this.active = false
427
- await validateSelf(this, 'onBlur')
428
- }
429
-
430
- @action
431
- validate = (triggerType?: ValidatorTriggerType) => {
432
- return batchValidate(this, `${this.address}.**`, triggerType)
433
- }
434
-
435
- submit = <T>(onSubmit?: (values: any) => Promise<T> | void): Promise<T> => {
436
- return batchSubmit(this, onSubmit)
437
- }
438
-
439
- @action
440
- reset = (options?: IFieldResetOptions) => {
441
- return batchReset(this, `${this.address}.**`, options)
442
- }
443
-
444
- queryFeedbacks = (search?: ISearchFeedback): IFieldFeedback[] => {
445
- return queryFeedbacks(this, search)
446
- }
447
-
448
- @action
449
- modify = () => modifySelf(this)
450
- }