@formvk/react 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.
@@ -1,704 +0,0 @@
1
- import { act, cleanup, fireEvent, render } from '@testing-library/react'
2
- import { memo } from 'react'
3
- import { afterEach, describe, expect, test, vi } from 'vitest'
4
-
5
- import { FormProvider, ReactiveField, SchemaField, connect, createForm, mapProps, mapReadPretty } from '..'
6
- import { Container, Decorator, Input, mountWithForm } from './shared'
7
-
8
- afterEach(cleanup)
9
-
10
- function Text(props: any) {
11
- return (
12
- <span className="text" data-value={props.value} data-text={props.text}>
13
- {props.title ?? props.value}
14
- </span>
15
- )
16
- }
17
- Text.displayName = 'Text'
18
-
19
- function ReadPretty() {
20
- return <span className="read-pretty">readPretty</span>
21
- }
22
-
23
- function SlotViewer(props: any) {
24
- return (
25
- <div className="slot-viewer">
26
- <div className="named">{props.slots?.label}</div>
27
- <div className="default">{props.slots?.default}</div>
28
- </div>
29
- )
30
- }
31
- SlotViewer.displayName = 'SlotViewer'
32
-
33
- const components = { Input, Decorator, Container, SlotViewer }
34
-
35
- describe('schema 驱动的字段渲染', () => {
36
- test('数据字段基础渲染:decorator + component', () => {
37
- const form = createForm({ values: { username: 'init' } })
38
- const view = mountWithForm(
39
- form,
40
- <SchemaField
41
- components={components}
42
- schema={{
43
- type: 'object',
44
- properties: {
45
- username: {
46
- type: 'string',
47
- decorator: 'Decorator',
48
- component: 'Input',
49
- },
50
- },
51
- }}
52
- />
53
- )
54
-
55
- expect(view.container.querySelector('.decorator')).toBeTruthy()
56
- expect(view.container.querySelector('input')!.value).toBe('init')
57
- // 无名 object 根节点用 display: contents 包裹子节点
58
- expect(view.container.innerHTML).toContain('display: contents')
59
- })
60
-
61
- test('component / decorator 支持字符串解析与组件对象', () => {
62
- const form = createForm()
63
- const view = mountWithForm(
64
- form,
65
- <SchemaField
66
- components={components}
67
- schema={{
68
- type: 'object',
69
- properties: {
70
- byName: { type: 'string', decorator: 'Decorator', component: 'Input' },
71
- byObject: { type: 'string', decorator: Decorator, component: Input },
72
- },
73
- }}
74
- />
75
- )
76
-
77
- expect(view.container.querySelectorAll('.decorator')).toHaveLength(2)
78
- expect(view.container.querySelectorAll('input')).toHaveLength(2)
79
- })
80
-
81
- test('无 decorator 只渲染 component;无 component 时渲染 content', () => {
82
- const form = createForm()
83
- const view = mountWithForm(
84
- form,
85
- <SchemaField
86
- components={components}
87
- schema={{
88
- type: 'object',
89
- properties: {
90
- plain: { type: 'string', component: 'Input' },
91
- contentOnly: { type: 'string', component: 'SlotViewer', content: 'hello' },
92
- },
93
- }}
94
- />
95
- )
96
-
97
- expect(view.container.querySelector('.decorator')).toBeNull()
98
- expect(view.container.querySelector('input')).toBeTruthy()
99
- expect(view.container.querySelector('.slot-viewer .default')!.textContent).toBe('hello')
100
- })
101
-
102
- test('object / array / void 节点的插槽透传', () => {
103
- const form = createForm()
104
- const view = mountWithForm(
105
- form,
106
- <SchemaField
107
- components={components}
108
- schema={{
109
- type: 'object',
110
- properties: {
111
- obj: {
112
- type: 'object',
113
- component: 'Container',
114
- properties: {
115
- a: { type: 'string', component: 'Input' },
116
- },
117
- },
118
- list: {
119
- type: 'array',
120
- component: 'Container',
121
- items: { type: 'string', component: 'Input' },
122
- },
123
- voidField: {
124
- type: 'void',
125
- component: 'Container',
126
- properties: {
127
- b: { type: 'string', component: 'Input' },
128
- },
129
- },
130
- },
131
- }}
132
- />
133
- )
134
-
135
- const containers = view.container.querySelectorAll('.container')
136
- expect(containers).toHaveLength(3)
137
- expect(containers[0].querySelector('input')).toBeTruthy()
138
- expect(containers[2].querySelector('input')).toBeTruthy()
139
- expect((form.fields['list'] as any).displayName).toBe('ArrayField')
140
- })
141
-
142
- test('value 受控:字段值与输入事件双向同步', async () => {
143
- const form = createForm<{ username?: string }>({ values: { username: 'init' } })
144
- const view = mountWithForm(
145
- form,
146
- <SchemaField
147
- components={components}
148
- schema={{
149
- type: 'object',
150
- properties: {
151
- username: { type: 'string', component: 'Input' },
152
- },
153
- }}
154
- />
155
- )
156
-
157
- const input = view.container.querySelector('input')!
158
- expect(input.value).toBe('init')
159
-
160
- fireEvent.change(input, { target: { value: 'changed' } })
161
- expect(form.values.username).toBe('changed')
162
-
163
- await act(async () => {
164
- form.setValuesIn('username', 'remote')
165
- })
166
- expect(input.value).toBe('remote')
167
- })
168
-
169
- test('componentProps 中 onChange / @change 都驱动字段,@change 优先', () => {
170
- const onChange = vi.fn()
171
- const atChange = vi.fn()
172
- const form = createForm<{ username?: string; nickname?: string }>()
173
- const view = mountWithForm(
174
- form,
175
- <SchemaField
176
- components={components}
177
- schema={{
178
- type: 'object',
179
- properties: {
180
- username: {
181
- type: 'string',
182
- component: 'Input',
183
- componentProps: { onChange },
184
- },
185
- nickname: {
186
- type: 'string',
187
- component: 'Input',
188
- componentProps: { onChange, '@change': atChange },
189
- },
190
- },
191
- }}
192
- />
193
- )
194
-
195
- const inputs = view.container.querySelectorAll('input')
196
- fireEvent.change(inputs[0], { target: { value: 'a' } })
197
- expect(form.values.username).toBe('a')
198
- expect(onChange).toHaveBeenCalled()
199
-
200
- fireEvent.change(inputs[1], { target: { value: 'b' } })
201
- expect(form.values.nickname).toBe('b')
202
- expect(atChange).toHaveBeenCalled()
203
- })
204
-
205
- test('onFocus / onBlur 触发字段 focus / blur', () => {
206
- const form = createForm()
207
- const view = mountWithForm(
208
- form,
209
- <SchemaField
210
- components={components}
211
- schema={{
212
- type: 'object',
213
- properties: {
214
- username: { type: 'string', component: 'Input' },
215
- },
216
- }}
217
- />
218
- )
219
-
220
- const field = form.fields['username'] as any
221
- const input = view.container.querySelector('input')!
222
- fireEvent.focus(input)
223
- expect(field.active).toBe(true)
224
-
225
- fireEvent.blur(input)
226
- expect(field.active).toBe(false)
227
- })
228
-
229
- test('pattern 驱动 disabled / readOnly', async () => {
230
- const form = createForm()
231
- const view = mountWithForm(
232
- form,
233
- <SchemaField
234
- components={components}
235
- schema={{
236
- type: 'object',
237
- properties: {
238
- username: { type: 'string', component: 'Input' },
239
- },
240
- }}
241
- />
242
- )
243
-
244
- const input = view.container.querySelector('input')!
245
-
246
- await act(async () => {
247
- form.setPattern('disabled')
248
- })
249
- expect(input.hasAttribute('disabled')).toBe(true)
250
-
251
- await act(async () => {
252
- form.setPattern('readOnly')
253
- })
254
- expect(input.hasAttribute('readonly')).toBe(true)
255
- expect(input.hasAttribute('disabled')).toBe(false)
256
-
257
- await act(async () => {
258
- form.setPattern('readPretty')
259
- })
260
- expect(input.hasAttribute('disabled')).toBe(true)
261
- })
262
-
263
- test('display 从 none 切到 visible 时重新渲染', async () => {
264
- const form = createForm()
265
- const view = mountWithForm(
266
- form,
267
- <SchemaField
268
- components={components}
269
- schema={{
270
- type: 'object',
271
- properties: {
272
- username: { type: 'string', component: 'Input', display: 'none' },
273
- },
274
- }}
275
- />
276
- )
277
-
278
- expect(view.container.querySelector('input')).toBeNull()
279
-
280
- await act(async () => {
281
- form.setFieldState('username', state => {
282
- state.display = 'visible'
283
- })
284
- })
285
- expect(view.container.querySelector('input')).toBeTruthy()
286
- })
287
-
288
- test('content 支持字符串、具名插槽对象、ReactElement', () => {
289
- const form = createForm()
290
- const view = mountWithForm(
291
- form,
292
- <SchemaField
293
- components={components}
294
- schema={{
295
- type: 'object',
296
- properties: {
297
- text: { type: 'string', component: 'SlotViewer', content: 'plain' },
298
- named: {
299
- type: 'string',
300
- component: 'SlotViewer',
301
- content: { label: 'LBL', default: 'DEF' },
302
- },
303
- vnode: {
304
- type: 'string',
305
- component: 'SlotViewer',
306
- content: <span className="vnode-content">V</span>,
307
- },
308
- },
309
- }}
310
- />
311
- )
312
-
313
- const viewers = view.container.querySelectorAll('.slot-viewer')
314
- expect(viewers[0].querySelector('.default')!.textContent).toBe('plain')
315
- expect(viewers[1].querySelector('.named')!.textContent).toBe('LBL')
316
- expect(viewers[1].querySelector('.default')!.textContent).toBe('DEF')
317
- expect(viewers[2].querySelector('.vnode-content')!.textContent).toBe('V')
318
- })
319
-
320
- test('connect + mapProps(含 value 删除分支)与 mapReadPretty', async () => {
321
- const Connected = connect(Text, mapProps({ value: 'value', title: 'title' }), mapReadPretty(ReadPretty))
322
- const ValueMapped = connect(Text, mapProps({ value: 'text' }))
323
-
324
- const form = createForm({ values: { mapped: 'mapped-value' } })
325
- const view = mountWithForm(
326
- form,
327
- <SchemaField
328
- components={{ Connected, ValueMapped }}
329
- schema={{
330
- type: 'object',
331
- properties: {
332
- mapped: {
333
- type: 'string',
334
- title: 'Title',
335
- component: 'Connected',
336
- },
337
- deleted: {
338
- type: 'string',
339
- component: 'ValueMapped',
340
- },
341
- },
342
- }}
343
- />
344
- )
345
-
346
- const mapped = view.container.querySelector('.text')!
347
- expect(mapped.textContent).toBe('Title')
348
- expect(mapped.getAttribute('data-value')).toBe('mapped-value')
349
-
350
- await act(async () => {
351
- form.setPattern('readPretty')
352
- })
353
- expect(view.container.querySelector('.read-pretty')).toBeTruthy()
354
-
355
- await act(async () => {
356
- form.setPattern('editable')
357
- })
358
- const valueMapped = Array.from(view.container.querySelectorAll('.text')).at(-1)!
359
- expect(valueMapped.getAttribute('data-text')).toBeNull()
360
- expect(valueMapped.getAttribute('data-value')).toBeNull()
361
- })
362
-
363
- test('schema 变化后创建新字段并卸载旧字段', async () => {
364
- const form = createForm()
365
- let schema: any = {
366
- type: 'object',
367
- properties: {
368
- a: { type: 'string', component: 'Input' },
369
- },
370
- }
371
- const view = mountWithForm(form, <SchemaField components={components} schema={schema} />)
372
-
373
- const first = form.fields['a'] as any
374
- expect(first.mounted).toBe(true)
375
-
376
- schema = {
377
- type: 'object',
378
- properties: {
379
- b: { type: 'string', component: 'Input' },
380
- },
381
- }
382
- view.rerender(
383
- <FormProvider form={form}>
384
- <SchemaField components={components} schema={schema} />
385
- </FormProvider>
386
- )
387
-
388
- expect(first.unmounted).toBe(true)
389
- expect((form.fields['b'] as any)?.displayName).toBe('Field')
390
- expect(view.container.querySelector('input')!.getAttribute('data-testid')).toBe('b')
391
- })
392
- })
393
-
394
- describe('边界与降级渲染', () => {
395
- test('content 的空值与数字内容不渲染', () => {
396
- const form = createForm()
397
- const view = mountWithForm(
398
- form,
399
- <SchemaField
400
- components={components}
401
- schema={{
402
- type: 'object',
403
- properties: {
404
- partial: {
405
- type: 'string',
406
- component: 'SlotViewer',
407
- content: { label: null, default: 5 } as any,
408
- },
409
- numeric: { type: 'string', component: 'SlotViewer', content: 123 as any },
410
- },
411
- }}
412
- />
413
- )
414
-
415
- const viewers = view.container.querySelectorAll('.slot-viewer')
416
- expect(viewers[0].querySelector('.named')!.textContent).toBe('')
417
- expect(viewers[0].querySelector('.default')!.textContent).toBe('5')
418
- expect(viewers[1].querySelector('.default')!.textContent).toBe('')
419
- })
420
-
421
- test('ReactiveField content 支持函数组件与对象组件', async () => {
422
- const FnContent = () => <span className="direct-fn">fn</span>
423
- const ObjectContent = memo(() => <span className="direct-object">object</span>)
424
- const Viewer = (props: any) => <div className="direct-viewer">{props.children}</div>
425
- const form = createForm()
426
- const view = render(
427
- <FormProvider form={form}>
428
- <ReactiveField fieldProps={{ name: 'directContent' }} />
429
- </FormProvider>
430
- )
431
-
432
- const field = form.fields['directContent'] as any
433
- await act(async () => {
434
- field.component = Viewer
435
- field.content = FnContent
436
- })
437
- expect(view.container.querySelector('.direct-viewer .direct-fn')!.textContent).toBe('fn')
438
-
439
- await act(async () => {
440
- field.content = ObjectContent
441
- })
442
- expect(view.container.querySelector('.direct-viewer .direct-object')!.textContent).toBe('object')
443
- })
444
-
445
- test('object 无 component / decorator 且多子节点时以 Fragment 渲染', () => {
446
- const form = createForm()
447
- const view = mountWithForm(
448
- form,
449
- <SchemaField
450
- components={components}
451
- schema={{
452
- type: 'object',
453
- properties: {
454
- obj: {
455
- type: 'object',
456
- properties: {
457
- a: { type: 'string', component: 'Input' },
458
- b: { type: 'string', component: 'Input' },
459
- },
460
- },
461
- },
462
- }}
463
- />
464
- )
465
-
466
- expect(view.container.querySelectorAll('input')).toHaveLength(2)
467
- expect((form.fields['obj'] as any).displayName).toBe('ObjectField')
468
- })
469
-
470
- test('object 无子属性时不渲染内容', () => {
471
- const form = createForm()
472
- const view = mountWithForm(
473
- form,
474
- <SchemaField components={components} schema={{ type: 'object', properties: { blank: { type: 'object' } } }} />
475
- )
476
-
477
- expect(view.container.querySelector('input')).toBeNull()
478
- expect((form.fields['blank'] as any).displayName).toBe('ObjectField')
479
- })
480
-
481
- test('object 无子属性且 content 默认插槽为空时渲染为空', () => {
482
- const form = createForm()
483
- const view = mountWithForm(
484
- form,
485
- <SchemaField
486
- components={components}
487
- schema={{
488
- type: 'object',
489
- properties: {
490
- blank: { type: 'object', content: { default: [] } as any },
491
- },
492
- }}
493
- />
494
- )
495
-
496
- expect(view.container.querySelector('input')).toBeNull()
497
- expect(view.container.querySelector('.slot-viewer')).toBeNull()
498
- })
499
-
500
- test('未注册的组件名按原生标签渲染', () => {
501
- const form = createForm()
502
- const view = mountWithForm(
503
- form,
504
- <SchemaField
505
- components={components}
506
- schema={{
507
- type: 'object',
508
- properties: {
509
- native: { type: 'string', component: 'section', content: 'native-text' },
510
- },
511
- }}
512
- />
513
- )
514
-
515
- expect(view.container.querySelector('section')!.textContent).toBe('native-text')
516
- })
517
-
518
- test('object 同时配置 content 与子属性时合并插槽内容', () => {
519
- const form = createForm()
520
- const view = mountWithForm(
521
- form,
522
- <SchemaField
523
- components={components}
524
- schema={{
525
- type: 'object',
526
- properties: {
527
- box: {
528
- type: 'object',
529
- component: 'Container',
530
- content: 'extra',
531
- properties: {
532
- a: { type: 'string', component: 'Input' },
533
- },
534
- },
535
- },
536
- }}
537
- />
538
- )
539
-
540
- expect(view.container.querySelector('.container input')).toBeTruthy()
541
- expect(view.container.querySelector('.container')!.textContent).toContain('extra')
542
- })
543
-
544
- test('decoratorProps / componentProps 被清空后仍可渲染', async () => {
545
- const form = createForm()
546
- const view = mountWithForm(
547
- form,
548
- <SchemaField
549
- components={components}
550
- schema={{
551
- type: 'object',
552
- properties: {
553
- a: {
554
- type: 'string',
555
- decorator: 'Decorator',
556
- decoratorProps: { label: 'L' },
557
- component: 'Input',
558
- componentProps: { placeholder: 'P' },
559
- },
560
- },
561
- }}
562
- />
563
- )
564
-
565
- expect(view.container.querySelector('input')!.getAttribute('placeholder')).toBe('P')
566
-
567
- const field = form.fields['a'] as any
568
- await act(async () => {
569
- field.decoratorProps = undefined
570
- field.componentProps = undefined
571
- })
572
-
573
- expect(view.container.querySelector('.decorator')).toBeTruthy()
574
- expect(view.container.querySelector('input')).toBeTruthy()
575
- expect(view.container.querySelector('input')!.getAttribute('placeholder')).toBeNull()
576
- })
577
-
578
- test('void 字段的 component 事件不写入字段状态', () => {
579
- const onChange = vi.fn()
580
- const onFocus = vi.fn()
581
- const onBlur = vi.fn()
582
- const form = createForm()
583
- const view = mountWithForm(
584
- form,
585
- <SchemaField
586
- components={components}
587
- schema={{
588
- type: 'object',
589
- properties: {
590
- v: {
591
- type: 'void',
592
- component: 'Input',
593
- componentProps: { onChange, onFocus, onBlur },
594
- },
595
- },
596
- }}
597
- />
598
- )
599
-
600
- const input = view.container.querySelector('input')!
601
- fireEvent.change(input, { target: { value: 'x' } })
602
- fireEvent.focus(input)
603
- fireEvent.blur(input)
604
-
605
- expect(onChange).toHaveBeenCalled()
606
- expect(onFocus).toHaveBeenCalled()
607
- expect(onBlur).toHaveBeenCalled()
608
-
609
- const field = form.fields['v'] as any
610
- expect(field.displayName).toBe('VoidField')
611
- expect(field.value).toBeUndefined()
612
- })
613
-
614
- test('同名字段 schema 更新时触发字段重建逻辑', () => {
615
- const form = createForm()
616
- let schema: any = {
617
- type: 'object',
618
- properties: {
619
- a: { type: 'string', component: 'Input' },
620
- },
621
- }
622
- const view = mountWithForm(form, <SchemaField components={components} schema={schema} />)
623
- const first = form.fields['a']
624
-
625
- schema = {
626
- type: 'object',
627
- properties: {
628
- a: { type: 'string', component: 'Input', title: 'updated' },
629
- },
630
- }
631
- view.rerender(
632
- <FormProvider form={form}>
633
- <SchemaField components={components} schema={schema} />
634
- </FormProvider>
635
- )
636
-
637
- expect(form.fields['a']).toBe(first)
638
- expect(view.container.querySelector('input')).toBeTruthy()
639
- })
640
-
641
- test('ReactiveField 无表单上下文或字段缺失时回退渲染插槽', () => {
642
- const bare = render(<ReactiveField />)
643
- expect(bare.container.querySelectorAll('.fallback')).toHaveLength(0)
644
-
645
- const fallback = render(
646
- <ReactiveField>
647
- <span className="fallback">fallback</span>
648
- </ReactiveField>
649
- )
650
- expect(fallback.container.querySelector('.fallback')).toBeTruthy()
651
-
652
- const form = createForm()
653
- render(
654
- <FormProvider form={form}>
655
- <ReactiveField fieldType={null as any} fieldProps={null as any} />
656
- </FormProvider>
657
- )
658
- expect(Object.keys(form.fields)).toHaveLength(0)
659
- })
660
-
661
- test('connect / mapProps 支持函数映射、非字符串映射与匿名组件', () => {
662
- const Anon = (props: any) => <span className="anon">{`${props.title ?? ''}|${props.placeholder ?? ''}`}</span>
663
- const FnMapped = connect(
664
- Anon,
665
- mapProps((props: any) => ({ ...props, title: 'fn-title' }))
666
- )
667
- const SkipMapped = connect(Anon, mapProps({ placeholder: 'placeholder' }))
668
- const BoolMapped = connect(Anon, mapProps({ title: false } as any))
669
- // 匿名组件的 mapReadPretty 使用降级组件名
670
- connect(
671
- Anon,
672
- mapReadPretty(() => <span className="read-anon">read</span>)
673
- )
674
-
675
- const form = createForm()
676
- const view = mountWithForm(
677
- form,
678
- <SchemaField
679
- components={{ FnMapped, SkipMapped, BoolMapped }}
680
- schema={{
681
- type: 'object',
682
- properties: {
683
- fn: { type: 'string', component: 'FnMapped' },
684
- skip: {
685
- type: 'string',
686
- component: 'SkipMapped',
687
- componentProps: { placeholder: 'from-schema' },
688
- },
689
- bool: { type: 'string', title: 'title-value', component: 'BoolMapped' },
690
- },
691
- }}
692
- />
693
- )
694
-
695
- const anons = view.container.querySelectorAll('.anon')
696
- expect(anons[0].textContent).toBe('fn-title|')
697
- expect(anons[1].textContent).toBe('|from-schema')
698
- expect(anons[2].textContent).toBe('title-value|')
699
-
700
- const DirectConnected = connect(Anon, mapProps({ title: 'title' }))
701
- const direct = render(<DirectConnected title="direct-title" />)
702
- expect(direct.container.querySelector('.anon')!.textContent).toBe('direct-title|')
703
- })
704
- })