@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,572 +0,0 @@
1
- import { expect, test } from 'vitest'
2
-
3
- import { createForm, setValidateLanguage } from '..'
4
- import { attach, sleep } from './shared'
5
-
6
- setValidateLanguage('en')
7
-
8
- test('创建字段并读取配置', () => {
9
- const form = attach(createForm())
10
- const field1 = attach(
11
- form.createField({
12
- name: 'field1',
13
- title: '字段一',
14
- description: '字段一描述',
15
- required: true,
16
- })!
17
- )
18
- expect(field1.title).toEqual('字段一')
19
- expect(field1.description).toEqual('字段一描述')
20
- expect(field1.required).toBeTruthy()
21
- expect(field1.validator).toEqual([{ required: true }])
22
-
23
- const field2 = attach(form.createField({ name: 'field2', disabled: true, hidden: true })!)
24
- expect(field2.pattern).toEqual('disabled')
25
- expect(field2.disabled).toBeTruthy()
26
- expect(field2.display).toEqual('hidden')
27
- expect(field2.hidden).toBeTruthy()
28
-
29
- const field3 = attach(form.createField({ name: 'field3', readOnly: true, visible: false })!)
30
- expect(field3.pattern).toEqual('readOnly')
31
- expect(field3.readOnly).toBeTruthy()
32
- expect(field3.display).toEqual('none')
33
- expect(field3.visible).toBeFalsy()
34
-
35
- const field4 = attach(form.createField({ name: 'field4', value: 123 })!)
36
- expect(field4.value).toEqual(123)
37
- expect(field4.initialValue).toBeUndefined()
38
-
39
- const field5 = attach(form.createField({ name: 'field5', initialValue: 123 })!)
40
- expect(field5.value).toEqual(123)
41
- expect(field5.initialValue).toEqual(123)
42
- })
43
-
44
- test('字段值与表单值保持同步', () => {
45
- const form = attach(createForm())
46
- const field = attach(form.createField({ name: 'aa' })!)
47
-
48
- field.value = '123'
49
- expect(form.values.aa).toEqual('123')
50
- expect(form.getValuesIn('aa')).toEqual('123')
51
-
52
- form.setValuesIn('aa', '321')
53
- expect(field.value).toEqual('321')
54
-
55
- field.setInitialValue('init')
56
- expect(field.initialValue).toEqual('init')
57
- expect(form.initialValues.aa).toEqual('init')
58
- })
59
-
60
- test('输入字段会更新值与修改状态', async () => {
61
- const form = attach(createForm())
62
- const field = attach(form.createField({ name: 'aa' })!)
63
-
64
- await field.onInput('123')
65
- expect(field.value).toEqual('123')
66
- expect(field.inputValue).toEqual('123')
67
- expect(field.inputValues).toEqual(['123'])
68
- expect(field.selfModified).toBeTruthy()
69
- expect(field.modified).toBeTruthy()
70
- expect(form.modified).toBeTruthy()
71
- })
72
-
73
- test('必填字段校验', async () => {
74
- const form = attach(createForm())
75
- const field = attach(form.createField({ name: 'aa', required: true })!)
76
-
77
- await expect(field.validate()).rejects.toBeTruthy()
78
- expect(field.selfErrors).toEqual(['The field value is required'])
79
- expect(field.valid).toBeFalsy()
80
- expect(field.invalid).toBeTruthy()
81
- expect(field.validateStatus).toEqual('error')
82
-
83
- await field.onInput('123')
84
- expect(field.value).toEqual('123')
85
- expect(field.selfErrors).toEqual([])
86
- expect(field.valid).toBeTruthy()
87
- expect(field.validateStatus).toBeUndefined()
88
- })
89
-
90
- test('自定义校验器与反馈', async () => {
91
- const form = attach(createForm())
92
- const field = attach(
93
- form.createField({
94
- name: 'aa',
95
- validator(value: string) {
96
- if (!value) return '不能为空'
97
- if (value.length < 3) {
98
- return {
99
- type: 'warning',
100
- message: '内容太短',
101
- }
102
- }
103
- },
104
- })!
105
- )
106
-
107
- try {
108
- await field.validate()
109
- } catch {}
110
- expect(field.selfErrors).toEqual(['不能为空'])
111
-
112
- await field.onInput('ab')
113
- expect(field.selfWarnings).toEqual(['内容太短'])
114
-
115
- await field.onInput('abc')
116
- expect(field.selfErrors).toEqual([])
117
- expect(field.selfWarnings).toEqual([])
118
- })
119
-
120
- test('设置校验规则', () => {
121
- const form = attach(createForm())
122
- const field = attach(form.createField({ name: 'aa', required: true })!)
123
-
124
- field.setValidatorRule('format', 'phone')
125
- expect(field.validator).toEqual([{ required: true }, { format: 'phone' }])
126
-
127
- field.setValidatorRule('required', false)
128
- expect(field.validator).toEqual([{ required: false }, { format: 'phone' }])
129
- expect(field.required).toBeFalsy()
130
- })
131
-
132
- test('字段显示状态切换时缓存值', () => {
133
- const form = attach(createForm())
134
- const field = attach(form.createField({ name: 'aa', value: '123' })!)
135
-
136
- expect(field.value).toEqual('123')
137
-
138
- field.setDisplay('none')
139
- expect(field.display).toEqual('none')
140
- expect(field.value).toBeUndefined()
141
- expect(form.existValuesIn('aa')).toBeFalsy()
142
-
143
- field.setDisplay('visible')
144
- expect(field.display).toEqual('visible')
145
- expect(field.value).toEqual('123')
146
- })
147
-
148
- test('切换字段交互模式', () => {
149
- const form = attach(createForm())
150
- const field = attach(form.createField({ name: 'aa', value: '123' })!)
151
-
152
- field.setPattern('readOnly')
153
- expect(field.pattern).toEqual('readOnly')
154
- expect(field.readOnly).toBeTruthy()
155
- expect(field.editable).toBeFalsy()
156
-
157
- field.setPattern('disabled')
158
- expect(field.disabled).toBeTruthy()
159
-
160
- field.setPattern('readPretty')
161
- expect(field.readPretty).toBeTruthy()
162
-
163
- field.setPattern('editable')
164
- expect(field.editable).toBeTruthy()
165
- })
166
-
167
- test('字段挂载与卸载', () => {
168
- const form = attach(createForm())
169
- const field = attach(form.createField({ name: 'aa' })!)
170
-
171
- expect(field.mounted).toBeTruthy()
172
- expect(field.initialized).toBeTruthy()
173
-
174
- field.onUnmount()
175
- expect(field.mounted).toBeFalsy()
176
- expect(field.unmounted).toBeTruthy()
177
-
178
- field.onMount()
179
- expect(field.mounted).toBeTruthy()
180
- expect(field.unmounted).toBeFalsy()
181
- })
182
-
183
- test('查询字段', () => {
184
- const form = attach(createForm())
185
- const object = attach(form.createObjectField({ name: 'object' })!)
186
- attach(form.createField({ name: 'a', basePath: 'object' }))
187
- attach(form.createField({ name: 'b', basePath: 'object' }))
188
- attach(form.createArrayField({ name: 'array', basePath: 'object' }))
189
- attach(form.createField({ name: '0', basePath: 'object.array' }))
190
-
191
- const paths = object.query('*').map(field => field.path.toString())
192
- expect(paths).toContain('object.a')
193
- expect(paths).toContain('object.b')
194
- expect(paths).toContain('object.array')
195
- expect(paths).toContain('object.array.0')
196
-
197
- expect(object.query('object.a').take()?.address.toString()).toEqual('object.a')
198
- expect(form.query('object.*').map(field => field.address.toString())).toContain('object.a')
199
- })
200
-
201
- test('销毁字段会清理值与初始值', () => {
202
- const form = attach(createForm())
203
- const field = attach(form.createField({ name: 'aa', initialValue: 456 })!)
204
-
205
- expect(form.values.aa).toEqual(456)
206
- expect(form.initialValues.aa).toEqual(456)
207
-
208
- field.destroy()
209
- expect(form.fields.aa).toBeUndefined()
210
- expect(form.existValuesIn('aa')).toBeFalsy()
211
- expect(form.existInitialValuesIn('aa')).toBeFalsy()
212
- })
213
-
214
- test('字段同时声明值与初始值时初始值优先生效', () => {
215
- const form = attach(createForm())
216
- // 与 formily 行为一致:构造期先写入 initialValue,value 因默认值保护被跳过
217
- const field = attach(form.createField({ name: 'aa', value: 123, initialValue: 456 })!)
218
- expect(field.value).toEqual(456)
219
- expect(form.initialValues.aa).toEqual(456)
220
-
221
- // 构造完成后显式赋值不再受默认值保护限制
222
- field.setValue(789)
223
- expect(field.value).toEqual(789)
224
- })
225
-
226
- test('设置组件与装饰器', () => {
227
- const form = attach(createForm())
228
- const field = attach(form.createField({ name: 'aa' })!)
229
- function Component() {
230
- return null
231
- }
232
- function Decorator() {
233
- return null
234
- }
235
-
236
- field.setComponent(Component, { a: 1 })
237
- expect(field.componentType).toBe(Component)
238
- expect(field.componentProps).toEqual({ a: 1 })
239
-
240
- field.setComponentProps({ b: 2 })
241
- expect(field.componentProps).toEqual({ a: 1, b: 2 })
242
-
243
- field.setDecorator(Decorator, { c: 3 })
244
- expect(field.decoratorType).toBe(Decorator)
245
- expect(field.decoratorProps).toEqual({ c: 3 })
246
-
247
- field.setDecoratorProps({ d: 4 })
248
- expect(field.decoratorProps).toEqual({ c: 3, d: 4 })
249
- })
250
-
251
- test('设置字段反馈信息', () => {
252
- const form = attach(createForm())
253
- const field = attach(form.createField({ name: 'aa' })!)
254
-
255
- field.setFeedback({ type: 'error', messages: ['出错了'] })
256
- expect(field.selfErrors).toEqual(['出错了'])
257
- expect(field.queryFeedbacks({ address: 'aa' })).toHaveLength(1)
258
- expect(field.queryFeedbacks({ address: 'bb' })).toHaveLength(0)
259
-
260
- field.setFeedback({ type: 'warning', messages: ['警告'] })
261
- expect(field.selfWarnings).toEqual(['警告'])
262
-
263
- field.setFeedback({ type: 'success', messages: ['成功'] })
264
- expect(field.selfSuccesses).toEqual(['成功'])
265
-
266
- field.setFeedback({ type: 'error', messages: [] })
267
- expect(field.selfErrors).toEqual([])
268
- })
269
-
270
- test('设置与读取字段状态', () => {
271
- const form = attach(createForm())
272
- const field = attach(form.createField({ name: 'aa' })!)
273
-
274
- field.setState({ title: '标题', description: '描述' })
275
- expect(field.title).toEqual('标题')
276
- expect(field.description).toEqual('描述')
277
- expect(field.getState(state => state.title)).toEqual('标题')
278
-
279
- field.setState(state => {
280
- state.value = '123'
281
- })
282
- expect(field.value).toEqual('123')
283
-
284
- const state: any = field.getState()
285
- expect(state.address).toEqual('aa')
286
- expect(state.path).toEqual('aa')
287
- expect(state.value).toEqual('123')
288
- })
289
-
290
- test('设置字段数据与内容', () => {
291
- const form = attach(createForm())
292
- const field = attach(form.createField({ name: 'aa' })!)
293
-
294
- field.setTitle('新标题')
295
- field.setDescription('新描述')
296
- field.setData({ extra: 1 })
297
- field.setContent('内容')
298
- field.setDataSource([{ label: '选项一', value: 1 }])
299
-
300
- expect(field.title).toEqual('新标题')
301
- expect(field.description).toEqual('新描述')
302
- expect(field.data).toEqual({ extra: 1 })
303
- expect(field.content).toEqual('内容')
304
- expect(field.dataSource).toEqual([{ label: '选项一', value: 1 }])
305
- })
306
-
307
- test('按校验模式与显示状态控制字段校验', async () => {
308
- const form = attach(
309
- createForm({
310
- validatePattern: ['editable'],
311
- validateDisplay: ['visible'],
312
- })
313
- )
314
- const field1 = attach(form.createField({ name: 'a', required: true })!)
315
- const field2 = attach(
316
- form.createField({
317
- name: 'b',
318
- required: true,
319
- validatePattern: ['readOnly'],
320
- validateDisplay: ['hidden'],
321
- })!
322
- )
323
- const field3 = attach(
324
- form.createField({
325
- name: 'c',
326
- required: true,
327
- validatePattern: ['readOnly', 'editable'],
328
- validateDisplay: ['hidden', 'visible'],
329
- })!
330
- )
331
-
332
- try {
333
- await form.validate()
334
- } catch {}
335
- expect(field1.selfErrors).toHaveLength(1)
336
- expect(field2.selfErrors).toHaveLength(0)
337
- expect(field3.selfErrors).toHaveLength(1)
338
-
339
- form.setPattern('readOnly')
340
- form.setDisplay('hidden')
341
- try {
342
- await form.validate()
343
- } catch {}
344
- // 切到 readOnly + hidden 后:field1 被跳过且错误被 reaction 清空,field2/field3 命中各自配置
345
- expect(field1.selfErrors).toHaveLength(0)
346
- expect(field2.selfErrors).toHaveLength(1)
347
- expect(field3.selfErrors).toHaveLength(1)
348
- })
349
-
350
- test('字段级校验配置覆盖表单级配置', async () => {
351
- const form = attach(
352
- createForm({
353
- validatePattern: ['editable'],
354
- validateDisplay: ['visible'],
355
- })
356
- )
357
- // 表单默认只校验 editable + visible,该字段自行放宽到 readOnly
358
- const field = attach(
359
- form.createField({
360
- name: 'aa',
361
- required: true,
362
- validatePattern: ['editable', 'readOnly'],
363
- })!
364
- )
365
-
366
- form.setPattern('readOnly')
367
- try {
368
- await form.validate()
369
- } catch {}
370
- expect(field.selfErrors).toEqual(['The field value is required'])
371
-
372
- // 未配置放宽的字段仍被跳过
373
- const skipped = attach(form.createField({ name: 'bb', required: true })!)
374
- try {
375
- await form.validate()
376
- } catch {}
377
- expect(skipped.selfErrors).toEqual([])
378
- })
379
-
380
- test('setLoading/setValidating/setSubmitting 延迟生效', async () => {
381
- const form = attach(createForm())
382
- const field = attach(form.createField({ name: 'aa' })!)
383
-
384
- field.setLoading(true)
385
- field.setValidating(true)
386
- field.setSubmitting(true)
387
- // 状态延迟写入,避免请求瞬间造成闪烁
388
- expect(field.loading).toBeFalsy()
389
- expect(field.validating).toBeFalsy()
390
- expect(field.submitting).toBeFalsy()
391
- await sleep(150)
392
- expect(field.loading).toBeTruthy()
393
- expect(field.validating).toBeTruthy()
394
- expect(field.submitting).toBeTruthy()
395
-
396
- field.setLoading(false)
397
- field.setValidating(false)
398
- field.setSubmitting(false)
399
- expect(field.loading).toBeFalsy()
400
- expect(field.validating).toBeFalsy()
401
- expect(field.submitting).toBeFalsy()
402
- })
403
-
404
- test('selfErrors/selfWarnings/selfSuccesses setter 写入 Effect 反馈', () => {
405
- const form = attach(createForm())
406
- const field = attach(form.createField({ name: 'aa' })!)
407
-
408
- field.selfErrors = ['effect error']
409
- field.selfWarnings = ['effect warning']
410
- field.selfSuccesses = ['effect success']
411
- expect(field.selfErrors).toEqual(['effect error'])
412
- expect(field.selfWarnings).toEqual(['effect warning'])
413
- expect(field.selfSuccesses).toEqual(['effect success'])
414
- expect(field.queryFeedbacks({ code: 'EffectError' })).toHaveLength(1)
415
- expect(field.queryFeedbacks({ code: 'EffectWarning' })).toHaveLength(1)
416
- expect(field.queryFeedbacks({ code: 'EffectSuccess' })).toHaveLength(1)
417
-
418
- field.selfErrors = undefined
419
- field.selfWarnings = undefined
420
- field.selfSuccesses = undefined
421
- expect(field.selfErrors).toEqual([])
422
- expect(field.selfWarnings).toEqual([])
423
- expect(field.selfSuccesses).toEqual([])
424
- })
425
-
426
- test('onFocus/onBlur 切换激活状态并触发对应校验', async () => {
427
- const form = attach(createForm())
428
- // 校验规则的 triggerType 默认是 onInput,这里显式声明 onFocus
429
- const field = attach(
430
- form.createField({
431
- name: 'aa',
432
- validator: [{ triggerType: 'onFocus', validator: () => 'focus error' }],
433
- })!
434
- )
435
-
436
- await field.onFocus()
437
- expect(field.active).toBeTruthy()
438
- expect(field.visited).toBeTruthy()
439
- expect(field.selfErrors).toEqual(['focus error'])
440
-
441
- await field.onBlur()
442
- expect(field.active).toBeFalsy()
443
- })
444
-
445
- test('onInput 只接受来自自身元素的事件', async () => {
446
- const form = attach(createForm())
447
- const field = attach(form.createField({ name: 'aa' })!)
448
-
449
- const target = { value: '123' }
450
- await field.onInput({ target, currentTarget: target })
451
- expect(field.value).toEqual('123')
452
-
453
- // target 与 currentTarget 不同说明事件来自子节点冒泡,应忽略
454
- await field.onInput({ target: { value: '456' }, currentTarget: { value: '789' } })
455
- expect(field.value).toEqual('123')
456
-
457
- // 支持受控 checkbox 的 checked 值
458
- const checkedTarget = { checked: true }
459
- await field.onInput({ target: checkedTarget, currentTarget: checkedTarget })
460
- expect(field.value).toBeTruthy()
461
- })
462
-
463
- test('record/records/index 读取父级数据', () => {
464
- const form = attach(createForm({ values: { array: [{ aa: 'a' }] } }))
465
- attach(form.createArrayField({ name: 'array' }))
466
- const child = attach(form.createField({ name: '0.aa', basePath: 'array' })!)
467
-
468
- expect(child.indexes).toEqual([0])
469
- expect(child.index).toEqual(0)
470
- expect(child.records).toEqual([{ aa: 'a' }])
471
- expect(child.record).toEqual({ aa: 'a' })
472
-
473
- const rootField = attach(form.createField({ name: 'bb' })!)
474
- expect(rootField.indexes).toEqual([])
475
- expect(rootField.index).toEqual(-1)
476
- expect(rootField.records).toBeUndefined()
477
- expect(rootField.record).toEqual(form.values)
478
- })
479
-
480
- test('父级 readPretty 覆盖子字段自身的非 editable 模式', () => {
481
- const form = attach(createForm())
482
- const parent = attach(form.createVoidField({ name: 'parent' })!)
483
- const child = attach(form.createField({ name: 'child', basePath: 'parent', pattern: 'disabled' })!)
484
-
485
- parent.setPattern('readPretty')
486
- expect(child.pattern).toEqual('readPretty')
487
-
488
- // 子字段显式声明 editable 时可以解除父级 readPretty
489
- child.setPattern('editable')
490
- expect(child.pattern).toEqual('editable')
491
- })
492
-
493
- test('销毁后的字段忽略写入', () => {
494
- const form = attach(createForm())
495
- const field = attach(form.createField({ name: 'aa', value: 123 })!)
496
-
497
- field.destroy()
498
- field.setValue(456)
499
- field.setInitialValue(456)
500
- expect(form.values.aa).toBeUndefined()
501
- expect(form.initialValues.aa).toBeUndefined()
502
- })
503
-
504
- test('reactions 属性随字段值响应式执行', () => {
505
- const form = attach(createForm())
506
- const field = attach(
507
- form.createField({
508
- name: 'aa',
509
- reactions(field) {
510
- field.title = `值:${field.value ?? ''}`
511
- },
512
- })!
513
- )
514
- expect(field.title).toEqual('值:')
515
-
516
- field.setValue('123')
517
- expect(field.title).toEqual('值:123')
518
- })
519
-
520
- test('match 支持精确路径与通配符', () => {
521
- const form = attach(createForm())
522
- const field = attach(form.createField({ name: 'aa' })!)
523
- expect(field.match('aa')).toBeTruthy()
524
- expect(field.match('*')).toBeTruthy()
525
- expect(field.match('bb')).toBeFalsy()
526
-
527
- // 虚拟字段包裹后 path 与 address 不一致,两者都能命中
528
- attach(form.createVoidField({ name: 'void' }))
529
- const child = attach(form.createField({ name: 'bb', basePath: 'void' })!)
530
- expect(child.match('bb')).toBeTruthy()
531
- expect(child.match('void.bb')).toBeTruthy()
532
- })
533
-
534
- test('字段 submit 与 reset 的选项', async () => {
535
- const form = attach(createForm())
536
- const field = attach(form.createField({ name: 'aa', initialValue: 'init', required: true })!)
537
-
538
- await field.onInput('changed')
539
- const result = await field.submit()
540
- expect(result).toEqual('changed')
541
-
542
- await field.reset()
543
- expect(field.value).toEqual('init')
544
- expect(field.modified).toBeFalsy()
545
-
546
- await field.reset({ forceClear: true })
547
- expect(field.value).toBeUndefined()
548
-
549
- // reset 的 validate 选项只负责写入反馈,不会抛出错误
550
- await field.reset({ forceClear: true, validate: true })
551
- expect(field.selfErrors).toEqual(['The field value is required'])
552
- })
553
-
554
- test('required setter 与 setValidator 更新校验规则', async () => {
555
- const form = attach(createForm())
556
- const field = attach(form.createField({ name: 'aa' })!)
557
-
558
- field.required = true
559
- expect(field.required).toBeTruthy()
560
- await expect(field.validate()).rejects.toBeTruthy()
561
- expect(field.selfErrors).toEqual(['The field value is required'])
562
-
563
- field.setValidator(() => '自定义错误')
564
- await expect(field.validate()).rejects.toBeTruthy()
565
- expect(field.selfErrors).toEqual(['自定义错误'])
566
-
567
- field.setValidator([{ required: true }])
568
- expect(field.validator).toEqual([{ required: true }])
569
-
570
- field.setValidator(undefined)
571
- expect(field.validator).toBeUndefined()
572
- })