@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.
- package/CHANGELOG.md +16 -0
- package/README.md +60 -0
- package/dist/index.js +91 -295
- package/dist/index.js.map +1 -1
- package/package.json +10 -6
- package/src/__tests__/expression.spec.tsx +0 -87
- package/src/__tests__/form.spec.tsx +0 -164
- package/src/__tests__/hooks.spec.tsx +0 -198
- package/src/__tests__/render.spec.tsx +0 -704
- package/src/__tests__/schema.spec.tsx +0 -598
- package/src/__tests__/shared.tsx +0 -44
- package/src/components/ExpressionScope.tsx +0 -16
- package/src/components/FormConsumer.tsx +0 -20
- package/src/components/FormProvider.tsx +0 -35
- package/src/components/ReactiveField.tsx +0 -161
- package/src/components/RecursionField.tsx +0 -113
- package/src/components/SchemaField.tsx +0 -195
- package/src/components/index.ts +0 -7
- package/src/hooks/index.ts +0 -6
- package/src/hooks/useAttach.ts +0 -20
- package/src/hooks/useField.ts +0 -8
- package/src/hooks/useFieldSchema.ts +0 -8
- package/src/hooks/useForm.ts +0 -8
- package/src/hooks/useFormEffects.ts +0 -19
- package/src/hooks/useParentForm.ts +0 -18
- package/src/index.ts +0 -5
- package/src/shared/connect.tsx +0 -72
- package/src/shared/context.ts +0 -15
- package/src/shared/createForm.ts +0 -10
- package/src/shared/index.ts +0 -2
- package/src/types/index.ts +0 -144
- package/src/utils/getRawComponent.ts +0 -12
- package/src/utils/slots.ts +0 -9
- package/tsconfig.build.json +0 -9
- package/tsconfig.json +0 -7
- package/vite.config.ts +0 -32
|
@@ -1,198 +0,0 @@
|
|
|
1
|
-
import type { Form } from '@formvk/core'
|
|
2
|
-
import { onFieldValueChange } from '@formvk/core'
|
|
3
|
-
import { act, cleanup, render } from '@testing-library/react'
|
|
4
|
-
import { afterEach, describe, expect, test, vi } from 'vitest'
|
|
5
|
-
|
|
6
|
-
import {
|
|
7
|
-
FormProvider,
|
|
8
|
-
SchemaField,
|
|
9
|
-
createForm,
|
|
10
|
-
useAttach,
|
|
11
|
-
useField,
|
|
12
|
-
useFieldSchema,
|
|
13
|
-
useForm,
|
|
14
|
-
useFormEffects,
|
|
15
|
-
useParentForm,
|
|
16
|
-
} from '..'
|
|
17
|
-
import { FieldContext, FormContext } from '../shared/context'
|
|
18
|
-
import { Input, mountWithForm } from './shared'
|
|
19
|
-
|
|
20
|
-
afterEach(cleanup)
|
|
21
|
-
|
|
22
|
-
describe('hooks', () => {
|
|
23
|
-
test('无 Provider 时 useForm / useField / useFieldSchema 返回 undefined 且不报错', () => {
|
|
24
|
-
function EmptyProbe() {
|
|
25
|
-
const form = useForm()
|
|
26
|
-
const field = useField()
|
|
27
|
-
const schema = useFieldSchema()
|
|
28
|
-
return <div className="empty-probe">{`${String(form)}-${String(field)}-${String(schema)}`}</div>
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
const view = render(<EmptyProbe />)
|
|
32
|
-
expect(view.container.querySelector('.empty-probe')!.textContent).toBe('undefined-undefined-undefined')
|
|
33
|
-
})
|
|
34
|
-
|
|
35
|
-
test('useFieldSchema 在 RecursionField 内拿到当前节点 schema', () => {
|
|
36
|
-
function SchemaProbe() {
|
|
37
|
-
const schema = useFieldSchema()
|
|
38
|
-
return <div className="schema-probe">{`${String(schema?.type)}:${String(schema?.name)}`}</div>
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
const form = createForm()
|
|
42
|
-
const view = mountWithForm(
|
|
43
|
-
form,
|
|
44
|
-
<SchemaField
|
|
45
|
-
components={{ SchemaProbe }}
|
|
46
|
-
schema={{
|
|
47
|
-
type: 'object',
|
|
48
|
-
properties: {
|
|
49
|
-
a: { type: 'string', component: 'SchemaProbe' },
|
|
50
|
-
},
|
|
51
|
-
}}
|
|
52
|
-
/>
|
|
53
|
-
)
|
|
54
|
-
|
|
55
|
-
expect(view.container.querySelector('.schema-probe')!.textContent).toBe('string:a')
|
|
56
|
-
})
|
|
57
|
-
|
|
58
|
-
test('useFormEffects 注册 effects,卸载后移除生命周期', () => {
|
|
59
|
-
let count = 0
|
|
60
|
-
function EffectsProbe() {
|
|
61
|
-
useFormEffects(() => {
|
|
62
|
-
onFieldValueChange('*', () => {
|
|
63
|
-
count++
|
|
64
|
-
})
|
|
65
|
-
})
|
|
66
|
-
return <div className="effects-probe" />
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
const form = createForm()
|
|
70
|
-
const view = mountWithForm(
|
|
71
|
-
form,
|
|
72
|
-
<>
|
|
73
|
-
<EffectsProbe />
|
|
74
|
-
<SchemaField
|
|
75
|
-
components={{ Input }}
|
|
76
|
-
schema={{
|
|
77
|
-
type: 'object',
|
|
78
|
-
properties: {
|
|
79
|
-
a: { type: 'string', component: 'Input' },
|
|
80
|
-
},
|
|
81
|
-
}}
|
|
82
|
-
/>
|
|
83
|
-
</>
|
|
84
|
-
)
|
|
85
|
-
|
|
86
|
-
act(() => {
|
|
87
|
-
form.setValuesIn('a', 'first')
|
|
88
|
-
})
|
|
89
|
-
expect(count).toBe(1)
|
|
90
|
-
|
|
91
|
-
view.unmount()
|
|
92
|
-
form.setValuesIn('a', 'second')
|
|
93
|
-
expect(count).toBe(1)
|
|
94
|
-
})
|
|
95
|
-
|
|
96
|
-
test('form 实例替换后 effects 重新注册到新表单', () => {
|
|
97
|
-
const formA = createForm()
|
|
98
|
-
const formB = createForm()
|
|
99
|
-
const addA = vi.spyOn(formA, 'addEffects')
|
|
100
|
-
const removeA = vi.spyOn(formA, 'removeEffects')
|
|
101
|
-
const addB = vi.spyOn(formB, 'addEffects')
|
|
102
|
-
|
|
103
|
-
function EffectsProbe() {
|
|
104
|
-
useFormEffects(() => {
|
|
105
|
-
onFieldValueChange('*', () => {})
|
|
106
|
-
})
|
|
107
|
-
return <div className="effects-probe" />
|
|
108
|
-
}
|
|
109
|
-
function Harness(props: { form: Form }) {
|
|
110
|
-
return (
|
|
111
|
-
<FormProvider form={props.form}>
|
|
112
|
-
<EffectsProbe />
|
|
113
|
-
</FormProvider>
|
|
114
|
-
)
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
const view = render(<Harness form={formA} />)
|
|
118
|
-
expect(addA).toHaveBeenCalledTimes(1)
|
|
119
|
-
|
|
120
|
-
view.rerender(<Harness form={formB} />)
|
|
121
|
-
expect(removeA).toHaveBeenCalled()
|
|
122
|
-
expect(addB).toHaveBeenCalledTimes(1)
|
|
123
|
-
})
|
|
124
|
-
|
|
125
|
-
test('useParentForm 响应字段引用替换', () => {
|
|
126
|
-
const form = createForm()
|
|
127
|
-
const objectField = form.createObjectField({ name: 'obj' })
|
|
128
|
-
const plainField = form.createField({ name: 'plain' })
|
|
129
|
-
|
|
130
|
-
function ParentProbe() {
|
|
131
|
-
const parent = useParentForm()
|
|
132
|
-
return <div className="parent-probe">{parent?.displayName}</div>
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
const view = render(
|
|
136
|
-
<FormContext.Provider value={form}>
|
|
137
|
-
<FieldContext.Provider value={objectField as any}>
|
|
138
|
-
<ParentProbe />
|
|
139
|
-
</FieldContext.Provider>
|
|
140
|
-
</FormContext.Provider>
|
|
141
|
-
)
|
|
142
|
-
expect(view.container.querySelector('.parent-probe')!.textContent).toBe('ObjectField')
|
|
143
|
-
|
|
144
|
-
view.rerender(
|
|
145
|
-
<FormContext.Provider value={form}>
|
|
146
|
-
<FieldContext.Provider value={plainField as any}>
|
|
147
|
-
<ParentProbe />
|
|
148
|
-
</FieldContext.Provider>
|
|
149
|
-
</FormContext.Provider>
|
|
150
|
-
)
|
|
151
|
-
expect(view.container.querySelector('.parent-probe')!.textContent).toBe('Form')
|
|
152
|
-
})
|
|
153
|
-
|
|
154
|
-
test('useAttach 切换 target 时旧值 onUnmount、新值 onMount', () => {
|
|
155
|
-
const targetA = { onMount: vi.fn(), onUnmount: vi.fn() }
|
|
156
|
-
const targetB = { onMount: vi.fn(), onUnmount: vi.fn() }
|
|
157
|
-
|
|
158
|
-
function AttachProbe(props: { target: { onMount: () => void; onUnmount: () => void } | undefined }) {
|
|
159
|
-
useAttach(props.target)
|
|
160
|
-
return <div className="attach-probe" />
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
const view = render(<AttachProbe target={targetA} />)
|
|
164
|
-
expect(targetA.onMount).toHaveBeenCalledTimes(1)
|
|
165
|
-
|
|
166
|
-
view.rerender(<AttachProbe target={targetB} />)
|
|
167
|
-
expect(targetA.onUnmount).toHaveBeenCalledTimes(1)
|
|
168
|
-
expect(targetB.onMount).toHaveBeenCalledTimes(1)
|
|
169
|
-
|
|
170
|
-
view.unmount()
|
|
171
|
-
expect(targetB.onUnmount).toHaveBeenCalled()
|
|
172
|
-
})
|
|
173
|
-
|
|
174
|
-
test('useAttach 目标置空时不报错', () => {
|
|
175
|
-
const target = { onMount: vi.fn(), onUnmount: vi.fn() }
|
|
176
|
-
|
|
177
|
-
function DetachProbe(props: { target: { onMount: () => void; onUnmount: () => void } | undefined }) {
|
|
178
|
-
useAttach(props.target)
|
|
179
|
-
return <div className="detach-probe" />
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
const view = render(<DetachProbe target={target} />)
|
|
183
|
-
view.rerender(<DetachProbe target={undefined} />)
|
|
184
|
-
|
|
185
|
-
expect(target.onUnmount).toHaveBeenCalledTimes(1)
|
|
186
|
-
view.unmount()
|
|
187
|
-
})
|
|
188
|
-
|
|
189
|
-
test('useFormEffects 无表单上下文时安全跳过', () => {
|
|
190
|
-
function EmptyEffectsProbe() {
|
|
191
|
-
useFormEffects(() => {})
|
|
192
|
-
return <div className="empty-effects" />
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
const view = render(<EmptyEffectsProbe />)
|
|
196
|
-
expect(view.container.querySelector('.empty-effects')).toBeTruthy()
|
|
197
|
-
})
|
|
198
|
-
})
|