@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,359 +0,0 @@
1
- import { action, batch, computed, reactive, shallow, shallowRef } from '@formvk/reactive'
2
- import type { FormPathPattern } from '@formvk/shared'
3
- import { each, FormPath, isFn, isValid, toArr } from '@formvk/shared'
4
-
5
- import { destroy, getArrayParent, getObjectParent, initFieldUpdate, locateNode } from '../shared/internals'
6
- import type {
7
- FieldComponent,
8
- FieldDecorator,
9
- FieldDisplayTypes,
10
- FieldPatternTypes,
11
- GeneralField,
12
- IFieldActions,
13
- JSXComponent,
14
- } from '../types'
15
- import { LifeCycleTypes } from '../types'
16
- import type { Form } from './Form'
17
- import { Query } from './Query'
18
-
19
- export class BaseField<ComponentProps = any, DecoratorProps = any, IRecord extends object = any> {
20
- @shallowRef accessor title: any = undefined
21
- @shallowRef accessor description: any = undefined
22
-
23
- @shallowRef accessor selfDisplay: FieldDisplayTypes | undefined = undefined
24
- @shallowRef accessor selfPattern: FieldPatternTypes | undefined = undefined
25
- @shallowRef accessor initialized = false
26
- @shallowRef accessor mounted = false
27
- @shallowRef accessor unmounted = false
28
-
29
- @shallowRef accessor content: any = undefined
30
-
31
- @shallow accessor data: any = undefined
32
-
33
- @shallowRef accessor decoratorType: any = undefined
34
- @reactive accessor decoratorProps: DecoratorProps = {} as DecoratorProps
35
- @shallowRef accessor componentType: any = undefined
36
- @reactive accessor componentProps: ComponentProps = {} as ComponentProps
37
-
38
- designable?: boolean
39
- address!: FormPath
40
- @shallowRef accessor path: FormPath = undefined as any
41
- form!: Form
42
-
43
- disposers: (() => void)[] = []
44
-
45
- actions: IFieldActions = {}
46
-
47
- locate(address: FormPathPattern) {
48
- this.form.fields[address.toString()] = this as any
49
- locateNode(this as any, address)
50
- }
51
-
52
- @computed
53
- get indexes(): number[] {
54
- return this.path.transform(/^\d+$/, (...args) => args.map(index => Number(index))) as number[]
55
- }
56
-
57
- get index() {
58
- return this.indexes[this.indexes.length - 1] ?? -1
59
- }
60
-
61
- get records(): IRecord[] {
62
- const array = getArrayParent(this)
63
- return array?.value as IRecord[]
64
- }
65
-
66
- get record(): IRecord {
67
- const obj = getObjectParent(this)
68
- if (obj) {
69
- return obj.value as IRecord
70
- }
71
- const index = this.index
72
- const array = getArrayParent(this)
73
- if (array) {
74
- return array.value?.[index] as IRecord
75
- }
76
- return this.form.values as IRecord
77
- }
78
-
79
- @computed
80
- get component() {
81
- return [this.componentType, this.componentProps]
82
- }
83
-
84
- set component(value: FieldComponent<any, ComponentProps>) {
85
- const component = toArr(value)
86
- this.componentType = component[0]
87
- this.componentProps = component[1] || {}
88
- }
89
-
90
- @computed
91
- get decorator() {
92
- return [this.decoratorType, this.decoratorProps]
93
- }
94
-
95
- set decorator(value: FieldDecorator<any, DecoratorProps>) {
96
- const decorator = toArr(value)
97
- this.decoratorType = decorator[0]
98
- this.decoratorProps = decorator[1] || {}
99
- }
100
-
101
- get parent(): GeneralField | undefined {
102
- let parent = this.address.parent()
103
- let identifier = parent.toString()
104
- while (!this.form.fields[identifier]) {
105
- parent = parent.parent()
106
- identifier = parent.toString()
107
- if (!identifier) return
108
- }
109
- return this.form.fields[identifier]
110
- }
111
-
112
- @computed
113
- get display(): FieldDisplayTypes {
114
- const parentDisplay = (this.parent as any)?.display
115
- if (parentDisplay && parentDisplay !== 'visible') {
116
- if (this.selfDisplay && this.selfDisplay !== 'visible') return this.selfDisplay
117
- return parentDisplay
118
- }
119
- if (isValid(this.selfDisplay)) return this.selfDisplay
120
- return parentDisplay || this.form.display || 'visible'
121
- }
122
-
123
- @computed
124
- get pattern(): FieldPatternTypes {
125
- const parentPattern: FieldPatternTypes = (this.parent as any)?.pattern || this.form.pattern || 'editable'
126
- const selfPattern = this.selfPattern
127
- if (isValid(selfPattern)) {
128
- if (parentPattern === 'readPretty' && selfPattern !== 'editable') {
129
- return parentPattern
130
- }
131
- return selfPattern
132
- }
133
- return parentPattern
134
- }
135
-
136
- @computed
137
- get editable() {
138
- return this.pattern === 'editable'
139
- }
140
-
141
- @computed
142
- get disabled() {
143
- return this.pattern === 'disabled'
144
- }
145
-
146
- @computed
147
- get readOnly() {
148
- return this.pattern === 'readOnly'
149
- }
150
-
151
- @computed
152
- get readPretty() {
153
- return this.pattern === 'readPretty'
154
- }
155
-
156
- @computed
157
- get hidden() {
158
- return this.display === 'hidden'
159
- }
160
-
161
- @computed
162
- get visible() {
163
- return this.display === 'visible'
164
- }
165
-
166
- get destroyed() {
167
- return !this.form.fields[this.address.toString()]
168
- }
169
-
170
- set hidden(hidden: boolean | undefined) {
171
- if (!isValid(hidden)) return
172
- if (hidden) {
173
- this.display = 'hidden'
174
- } else {
175
- this.display = 'visible'
176
- }
177
- }
178
-
179
- set visible(visible: boolean | undefined) {
180
- if (!isValid(visible)) return
181
- if (visible) {
182
- this.display = 'visible'
183
- } else {
184
- this.display = 'none'
185
- }
186
- }
187
-
188
- set editable(editable: boolean | undefined) {
189
- if (!isValid(editable)) return
190
- if (editable) {
191
- this.pattern = 'editable'
192
- } else {
193
- this.pattern = 'readPretty'
194
- }
195
- }
196
-
197
- set readOnly(readOnly: boolean | undefined) {
198
- if (!isValid(readOnly)) return
199
- if (readOnly) {
200
- this.pattern = 'readOnly'
201
- } else {
202
- this.pattern = 'editable'
203
- }
204
- }
205
-
206
- set disabled(disabled: boolean | undefined) {
207
- if (!isValid(disabled)) return
208
- if (disabled) {
209
- this.pattern = 'disabled'
210
- } else {
211
- this.pattern = 'editable'
212
- }
213
- }
214
-
215
- set readPretty(readPretty: boolean | undefined) {
216
- if (!isValid(readPretty)) return
217
- if (readPretty) {
218
- this.pattern = 'readPretty'
219
- } else {
220
- this.pattern = 'editable'
221
- }
222
- }
223
-
224
- set pattern(pattern: FieldPatternTypes | undefined) {
225
- this.selfPattern = pattern
226
- }
227
-
228
- set display(display: FieldDisplayTypes | undefined) {
229
- this.selfDisplay = display
230
- }
231
-
232
- @action
233
- setTitle = (title?: any) => {
234
- this.title = title
235
- }
236
-
237
- @action
238
- setDescription = (description?: any) => {
239
- this.description = description
240
- }
241
-
242
- @action
243
- setDisplay = (type?: FieldDisplayTypes) => {
244
- this.display = type
245
- }
246
-
247
- @action
248
- setPattern = (type?: FieldPatternTypes) => {
249
- this.pattern = type
250
- }
251
-
252
- @action
253
- setComponent = <C extends JSXComponent>(component?: C, props?: Partial<ComponentProps>) => {
254
- if (component) {
255
- this.componentType = component as any
256
- }
257
- if (props) {
258
- this.componentProps = this.componentProps || ({} as ComponentProps)
259
- Object.assign(this.componentProps as any, props)
260
- }
261
- }
262
-
263
- @action
264
- setComponentProps = (props?: Partial<ComponentProps>) => {
265
- if (props) {
266
- this.componentProps = this.componentProps || ({} as ComponentProps)
267
- Object.assign(this.componentProps as any, props)
268
- }
269
- }
270
-
271
- @action
272
- setDecorator = <D extends JSXComponent>(component?: D, props?: Partial<DecoratorProps>) => {
273
- if (component) {
274
- this.decoratorType = component as any
275
- }
276
- if (props) {
277
- this.decoratorProps = this.decoratorProps || ({} as DecoratorProps)
278
- Object.assign(this.decoratorProps as any, props)
279
- }
280
- }
281
-
282
- @action
283
- setDecoratorProps = (props?: Partial<DecoratorProps>) => {
284
- if (props) {
285
- this.decoratorProps = this.decoratorProps || ({} as DecoratorProps)
286
- Object.assign(this.decoratorProps as any, props)
287
- }
288
- }
289
-
290
- @action
291
- setData = (data: any) => {
292
- this.data = data
293
- }
294
-
295
- @action
296
- setContent = (content: any) => {
297
- this.content = content
298
- }
299
-
300
- @batch
301
- onInit = () => {
302
- this.initialized = true
303
- initFieldUpdate(this as any)
304
- this.notify(LifeCycleTypes.ON_FIELD_INIT)
305
- }
306
-
307
- @batch
308
- onMount = () => {
309
- this.mounted = true
310
- this.unmounted = false
311
- this.notify(LifeCycleTypes.ON_FIELD_MOUNT)
312
- }
313
-
314
- @batch
315
- onUnmount = () => {
316
- this.mounted = false
317
- this.unmounted = true
318
- this.notify(LifeCycleTypes.ON_FIELD_UNMOUNT)
319
- }
320
-
321
- query = (pattern: FormPathPattern | RegExp) => {
322
- return new Query({
323
- pattern,
324
- base: this.address,
325
- form: this.form,
326
- })
327
- }
328
-
329
- notify = (type: LifeCycleTypes, payload?: any) => {
330
- return this.form.notify(type, payload ?? this)
331
- }
332
-
333
- dispose = () => {
334
- this.disposers.forEach(dispose => {
335
- dispose()
336
- })
337
- this.form.removeEffects(this)
338
- }
339
-
340
- destroy = (forceClear = true) => {
341
- destroy(this.form.fields, this.address.toString(), forceClear)
342
- }
343
-
344
- match = (pattern: FormPathPattern) => {
345
- return FormPath.parse(pattern).matchAliasGroup(this.address, this.path)
346
- }
347
-
348
- inject = (actions: IFieldActions) => {
349
- each(actions, (action, key) => {
350
- if (isFn(action)) {
351
- this.actions[key] = action
352
- }
353
- })
354
- }
355
-
356
- invoke = (name: string, ...args: any[]) => {
357
- return this.actions[name]?.(...args)
358
- }
359
- }