@formvk/vue 0.1.0 → 1.1.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 +27 -0
- package/README.md +65 -0
- package/README.zh-CN.md +65 -0
- package/dist/index.js +451 -322
- package/dist/index.js.map +1 -1
- package/package.json +13 -9
- package/src/__tests__/expression.spec.tsx +0 -79
- package/src/__tests__/form.spec.tsx +0 -185
- package/src/__tests__/hooks.spec.tsx +0 -230
- package/src/__tests__/render.spec.tsx +0 -694
- package/src/__tests__/schema.spec.tsx +0 -587
- package/src/__tests__/shared.tsx +0 -52
- package/src/components/ExpressionScope.tsx +0 -17
- package/src/components/FormConsumer.tsx +0 -17
- package/src/components/FormProvider.tsx +0 -24
- package/src/components/ReactiveField.tsx +0 -196
- package/src/components/RecursionField.tsx +0 -166
- package/src/components/SchemaField.tsx +0 -240
- package/src/components/index.ts +0 -5
- package/src/hooks/index.ts +0 -7
- package/src/hooks/useAttach.ts +0 -24
- package/src/hooks/useField.ts +0 -9
- package/src/hooks/useFieldSchema.ts +0 -9
- package/src/hooks/useForm.ts +0 -9
- package/src/hooks/useFormEffects.ts +0 -23
- package/src/hooks/useInjectionCleaner.ts +0 -6
- package/src/hooks/useParentForm.ts +0 -20
- package/src/index.ts +0 -5
- package/src/shared/connect.tsx +0 -75
- package/src/shared/context.ts +0 -15
- package/src/shared/createForm.ts +0 -7
- package/src/shared/index.ts +0 -2
- package/src/types/index.ts +0 -141
- package/src/utils/getRawComponent.ts +0 -11
- package/tsconfig.build.json +0 -9
- package/tsconfig.json +0 -8
- package/vite.config.ts +0 -23
|
@@ -1,587 +0,0 @@
|
|
|
1
|
-
import type { SchemaKey } from '@formvk/schema'
|
|
2
|
-
import { describe, expect, expectTypeOf, test } from 'vitest'
|
|
3
|
-
import { defineComponent, nextTick } from 'vue'
|
|
4
|
-
|
|
5
|
-
import { RecursionField, Schema, SchemaField, createForm, createSchemaField, useField, useFieldSchema } from '..'
|
|
6
|
-
import { Input, mountWithForm } from './shared'
|
|
7
|
-
|
|
8
|
-
const SlotContainer = defineComponent({
|
|
9
|
-
name: 'SlotContainer',
|
|
10
|
-
setup(_, { slots }) {
|
|
11
|
-
return () => (
|
|
12
|
-
<div class="slot-container">
|
|
13
|
-
<div class="header">{slots.header?.()}</div>
|
|
14
|
-
<div class="default">{slots.default?.()}</div>
|
|
15
|
-
</div>
|
|
16
|
-
)
|
|
17
|
-
},
|
|
18
|
-
})
|
|
19
|
-
|
|
20
|
-
const ArrayList = defineComponent({
|
|
21
|
-
name: 'ArrayList',
|
|
22
|
-
setup() {
|
|
23
|
-
const fieldRef = useField()
|
|
24
|
-
const schemaRef = useFieldSchema()
|
|
25
|
-
return () => {
|
|
26
|
-
const field = fieldRef.value as any
|
|
27
|
-
const items = (schemaRef.value as any)?.items
|
|
28
|
-
const list = Array.isArray(field?.value) ? field.value : []
|
|
29
|
-
return (
|
|
30
|
-
<div class="array-list">
|
|
31
|
-
{list.map((_: any, index: number) => (
|
|
32
|
-
<RecursionField key={index} schema={items} name={index} basePath={field.address} />
|
|
33
|
-
))}
|
|
34
|
-
</div>
|
|
35
|
-
)
|
|
36
|
-
}
|
|
37
|
-
},
|
|
38
|
-
})
|
|
39
|
-
|
|
40
|
-
describe('SchemaField / RecursionField', () => {
|
|
41
|
-
test('JSON schema 基础渲染:object 下的 string / boolean / number 子字段', () => {
|
|
42
|
-
const form = createForm({ values: { s: 'text', b: true, n: 3 } })
|
|
43
|
-
const wrapper = mountWithForm(form, () => (
|
|
44
|
-
<SchemaField
|
|
45
|
-
components={{ Input }}
|
|
46
|
-
schema={{
|
|
47
|
-
type: 'object',
|
|
48
|
-
properties: {
|
|
49
|
-
s: { type: 'string', component: 'Input' },
|
|
50
|
-
b: { type: 'boolean', component: 'Input' },
|
|
51
|
-
n: { type: 'number', component: 'Input' },
|
|
52
|
-
},
|
|
53
|
-
}}
|
|
54
|
-
/>
|
|
55
|
-
))
|
|
56
|
-
|
|
57
|
-
const inputs = wrapper.findAll('input')
|
|
58
|
-
expect(inputs.map(input => input.element.value)).toEqual(['text', 'true', '3'])
|
|
59
|
-
expect(inputs.map(input => input.attributes('data-testid'))).toEqual(['s', 'b', 'n'])
|
|
60
|
-
expect((form.fields['b'] as any).displayName).toBe('Field')
|
|
61
|
-
})
|
|
62
|
-
|
|
63
|
-
test('properties 的 index 控制渲染顺序', () => {
|
|
64
|
-
const form = createForm()
|
|
65
|
-
const wrapper = mountWithForm(form, () => (
|
|
66
|
-
<SchemaField
|
|
67
|
-
components={{ Input }}
|
|
68
|
-
schema={{
|
|
69
|
-
type: 'object',
|
|
70
|
-
properties: {
|
|
71
|
-
a: { type: 'string', component: 'Input', index: 2 },
|
|
72
|
-
b: { type: 'string', component: 'Input', index: 0 },
|
|
73
|
-
c: { type: 'string', component: 'Input', index: 1 },
|
|
74
|
-
},
|
|
75
|
-
}}
|
|
76
|
-
/>
|
|
77
|
-
))
|
|
78
|
-
|
|
79
|
-
expect(wrapper.findAll('input').map(input => input.attributes('data-testid'))).toEqual(['b', 'c', 'a'])
|
|
80
|
-
})
|
|
81
|
-
|
|
82
|
-
test('onlyRenderSelf 只创建自身字段,onlyRenderProperties 只渲染子属性', () => {
|
|
83
|
-
const selfForm = createForm()
|
|
84
|
-
const selfWrapper = mountWithForm(selfForm, () => (
|
|
85
|
-
<SchemaField
|
|
86
|
-
name="obj"
|
|
87
|
-
onlyRenderSelf
|
|
88
|
-
components={{ Input }}
|
|
89
|
-
schema={{
|
|
90
|
-
type: 'object',
|
|
91
|
-
properties: {
|
|
92
|
-
a: { type: 'string', component: 'Input' },
|
|
93
|
-
},
|
|
94
|
-
}}
|
|
95
|
-
/>
|
|
96
|
-
))
|
|
97
|
-
|
|
98
|
-
expect(selfForm.fields['obj']).toBeTruthy()
|
|
99
|
-
expect(selfForm.fields['obj.a']).toBeUndefined()
|
|
100
|
-
expect(selfWrapper.find('input').exists()).toBe(false)
|
|
101
|
-
|
|
102
|
-
const propsForm = createForm()
|
|
103
|
-
const propsWrapper = mountWithForm(propsForm, () => (
|
|
104
|
-
<SchemaField
|
|
105
|
-
name="obj"
|
|
106
|
-
onlyRenderProperties
|
|
107
|
-
components={{ Input }}
|
|
108
|
-
schema={{
|
|
109
|
-
type: 'object',
|
|
110
|
-
properties: {
|
|
111
|
-
a: { type: 'string', component: 'Input' },
|
|
112
|
-
},
|
|
113
|
-
}}
|
|
114
|
-
/>
|
|
115
|
-
))
|
|
116
|
-
|
|
117
|
-
expect(propsForm.fields['obj']).toBeUndefined()
|
|
118
|
-
expect(propsForm.fields['obj.a']).toBeTruthy()
|
|
119
|
-
expect(propsWrapper.find('input').exists()).toBe(true)
|
|
120
|
-
})
|
|
121
|
-
|
|
122
|
-
test('mapProperties 替换子 schema,filterProperties 过滤子节点', () => {
|
|
123
|
-
const form = createForm()
|
|
124
|
-
const wrapper = mountWithForm(form, () => (
|
|
125
|
-
<SchemaField
|
|
126
|
-
components={{ Input }}
|
|
127
|
-
mapProperties={(schema: any, name: SchemaKey) =>
|
|
128
|
-
name === 'a' ? { type: 'string', component: 'Input', componentProps: { placeholder: 'mapped' } } : undefined
|
|
129
|
-
}
|
|
130
|
-
filterProperties={(_schema: any, name: SchemaKey) => name !== 'b'}
|
|
131
|
-
schema={{
|
|
132
|
-
type: 'object',
|
|
133
|
-
properties: {
|
|
134
|
-
a: { type: 'string', component: 'Input' },
|
|
135
|
-
b: { type: 'string', component: 'Input' },
|
|
136
|
-
c: { type: 'string', component: 'Input' },
|
|
137
|
-
},
|
|
138
|
-
}}
|
|
139
|
-
/>
|
|
140
|
-
))
|
|
141
|
-
|
|
142
|
-
expect(wrapper.findAll('input')).toHaveLength(2)
|
|
143
|
-
expect(wrapper.find('[data-testid="a"]').attributes('placeholder')).toBe('mapped')
|
|
144
|
-
expect(wrapper.find('[data-testid="b"]').exists()).toBe(false)
|
|
145
|
-
expect(wrapper.find('[data-testid="c"]').exists()).toBe(true)
|
|
146
|
-
})
|
|
147
|
-
|
|
148
|
-
test('void 字段通过 slot 把子节点渲染到具名插槽', () => {
|
|
149
|
-
const form = createForm()
|
|
150
|
-
const wrapper = mountWithForm(form, () => (
|
|
151
|
-
<SchemaField
|
|
152
|
-
components={{ Input, SlotContainer }}
|
|
153
|
-
schema={{
|
|
154
|
-
type: 'object',
|
|
155
|
-
properties: {
|
|
156
|
-
v: {
|
|
157
|
-
type: 'void',
|
|
158
|
-
component: 'SlotContainer',
|
|
159
|
-
properties: {
|
|
160
|
-
a: { type: 'string', component: 'Input', slot: 'header' },
|
|
161
|
-
},
|
|
162
|
-
},
|
|
163
|
-
},
|
|
164
|
-
}}
|
|
165
|
-
/>
|
|
166
|
-
))
|
|
167
|
-
|
|
168
|
-
expect(wrapper.find('.slot-container .header input').exists()).toBe(true)
|
|
169
|
-
expect(wrapper.find('.slot-container .default').text()).toBe('')
|
|
170
|
-
expect((form.fields['v'] as any).displayName).toBe('VoidField')
|
|
171
|
-
})
|
|
172
|
-
|
|
173
|
-
test('array 字段按 items 渲染子节点', () => {
|
|
174
|
-
const form = createForm({ values: { arr: ['x', 'y'] } })
|
|
175
|
-
const wrapper = mountWithForm(form, () => (
|
|
176
|
-
<SchemaField
|
|
177
|
-
components={{ Input, ArrayList }}
|
|
178
|
-
schema={{
|
|
179
|
-
type: 'object',
|
|
180
|
-
properties: {
|
|
181
|
-
arr: {
|
|
182
|
-
type: 'array',
|
|
183
|
-
component: 'ArrayList',
|
|
184
|
-
items: { type: 'string', component: 'Input' },
|
|
185
|
-
},
|
|
186
|
-
},
|
|
187
|
-
}}
|
|
188
|
-
/>
|
|
189
|
-
))
|
|
190
|
-
|
|
191
|
-
const inputs = wrapper.findAll('input')
|
|
192
|
-
expect(inputs[0].attributes('data-testid')).toBe('arr.0')
|
|
193
|
-
expect(inputs[1].attributes('data-testid')).toBe('arr.1')
|
|
194
|
-
expect(inputs.map(input => input.element.value)).toEqual(['x', 'y'])
|
|
195
|
-
expect((form.fields['arr'] as any).displayName).toBe('ArrayField')
|
|
196
|
-
})
|
|
197
|
-
|
|
198
|
-
test('scope 表达式读取 $values 并在依赖值变化后重新编译', async () => {
|
|
199
|
-
const form = createForm({ values: { source: 'abc' } })
|
|
200
|
-
const wrapper = mountWithForm(form, () => (
|
|
201
|
-
<SchemaField
|
|
202
|
-
components={{ Input }}
|
|
203
|
-
schema={{
|
|
204
|
-
type: 'object',
|
|
205
|
-
properties: {
|
|
206
|
-
source: { type: 'string', component: 'Input' },
|
|
207
|
-
target: {
|
|
208
|
-
type: 'string',
|
|
209
|
-
component: 'Input',
|
|
210
|
-
componentProps: { placeholder: '{{$values.source}}' },
|
|
211
|
-
},
|
|
212
|
-
},
|
|
213
|
-
}}
|
|
214
|
-
/>
|
|
215
|
-
))
|
|
216
|
-
|
|
217
|
-
expect(wrapper.find('[data-testid="target"]').attributes('placeholder')).toBe('abc')
|
|
218
|
-
|
|
219
|
-
form.setValuesIn('source', 'changed')
|
|
220
|
-
await nextTick()
|
|
221
|
-
|
|
222
|
-
expect(wrapper.find('[data-testid="target"]').attributes('placeholder')).toBe('changed')
|
|
223
|
-
})
|
|
224
|
-
|
|
225
|
-
test('reactions 的 dependencies / when 驱动 visible 变化', async () => {
|
|
226
|
-
const form = createForm({ values: { hide: false } })
|
|
227
|
-
const wrapper = mountWithForm(form, () => (
|
|
228
|
-
<SchemaField
|
|
229
|
-
components={{ Input }}
|
|
230
|
-
schema={{
|
|
231
|
-
type: 'object',
|
|
232
|
-
properties: {
|
|
233
|
-
hide: { type: 'boolean', component: 'Input' },
|
|
234
|
-
target: {
|
|
235
|
-
type: 'string',
|
|
236
|
-
component: 'Input',
|
|
237
|
-
reactions: [
|
|
238
|
-
{
|
|
239
|
-
dependencies: ['hide'],
|
|
240
|
-
when: '{{$values.hide === true}}',
|
|
241
|
-
fulfill: { state: { visible: false } },
|
|
242
|
-
otherwise: { state: { visible: true } },
|
|
243
|
-
},
|
|
244
|
-
],
|
|
245
|
-
},
|
|
246
|
-
},
|
|
247
|
-
}}
|
|
248
|
-
/>
|
|
249
|
-
))
|
|
250
|
-
|
|
251
|
-
expect(wrapper.find('[data-testid="target"]').exists()).toBe(true)
|
|
252
|
-
|
|
253
|
-
form.setValuesIn('hide', true)
|
|
254
|
-
await nextTick()
|
|
255
|
-
expect(wrapper.find('[data-testid="target"]').exists()).toBe(false)
|
|
256
|
-
|
|
257
|
-
form.setValuesIn('hide', false)
|
|
258
|
-
await nextTick()
|
|
259
|
-
expect(wrapper.find('[data-testid="target"]').exists()).toBe(true)
|
|
260
|
-
})
|
|
261
|
-
|
|
262
|
-
test('SchemaField 接受扁平 components,同名组件可在 props 层覆盖', () => {
|
|
263
|
-
const InputA = defineComponent({
|
|
264
|
-
name: 'InputA',
|
|
265
|
-
props: ['value'],
|
|
266
|
-
setup(props) {
|
|
267
|
-
return () => <input class="input-a" value={props.value} />
|
|
268
|
-
},
|
|
269
|
-
})
|
|
270
|
-
const InputB = defineComponent({
|
|
271
|
-
name: 'InputB',
|
|
272
|
-
props: ['value'],
|
|
273
|
-
setup(props) {
|
|
274
|
-
return () => <input class="input-b" value={props.value} />
|
|
275
|
-
},
|
|
276
|
-
})
|
|
277
|
-
|
|
278
|
-
const schema = {
|
|
279
|
-
type: 'object',
|
|
280
|
-
properties: {
|
|
281
|
-
a: { type: 'string', component: 'Input' },
|
|
282
|
-
},
|
|
283
|
-
}
|
|
284
|
-
|
|
285
|
-
const formA = createForm()
|
|
286
|
-
const wrapperA = mountWithForm(formA, () => <SchemaField schema={schema} components={{ Input: InputA }} />)
|
|
287
|
-
expect(wrapperA.find('.input-a').exists()).toBe(true)
|
|
288
|
-
expect(wrapperA.find('.input-b').exists()).toBe(false)
|
|
289
|
-
|
|
290
|
-
const formB = createForm()
|
|
291
|
-
const wrapperB = mountWithForm(formB, () => <SchemaField components={{ Input: InputB }} schema={schema} />)
|
|
292
|
-
expect(wrapperB.find('.input-b').exists()).toBe(true)
|
|
293
|
-
expect(wrapperB.find('.input-a').exists()).toBe(false)
|
|
294
|
-
})
|
|
295
|
-
|
|
296
|
-
test('schema 引用变化后重建字段并渲染新节点', async () => {
|
|
297
|
-
const form = createForm()
|
|
298
|
-
let schema: any = {
|
|
299
|
-
type: 'object',
|
|
300
|
-
properties: {
|
|
301
|
-
a: { type: 'string', component: 'Input' },
|
|
302
|
-
},
|
|
303
|
-
}
|
|
304
|
-
const wrapper = mountWithForm(form, () => <SchemaField components={{ Input }} schema={schema} />)
|
|
305
|
-
|
|
306
|
-
const first = form.fields['a'] as any
|
|
307
|
-
expect(first.mounted).toBe(true)
|
|
308
|
-
|
|
309
|
-
schema = {
|
|
310
|
-
type: 'object',
|
|
311
|
-
properties: {
|
|
312
|
-
b: { type: 'string', component: 'Input' },
|
|
313
|
-
},
|
|
314
|
-
}
|
|
315
|
-
wrapper.vm.$forceUpdate()
|
|
316
|
-
await nextTick()
|
|
317
|
-
await nextTick()
|
|
318
|
-
|
|
319
|
-
expect(first.unmounted).toBe(true)
|
|
320
|
-
expect(form.fields['b']).toBeTruthy()
|
|
321
|
-
expect(wrapper.find('input').attributes('data-testid')).toBe('b')
|
|
322
|
-
})
|
|
323
|
-
|
|
324
|
-
test('hotReload:schema 引用变化后按新 schema 重建字段并保留值', async () => {
|
|
325
|
-
const form = createForm({ values: { a: 'keep', b: 'stale' } })
|
|
326
|
-
let schema: any = {
|
|
327
|
-
type: 'object',
|
|
328
|
-
properties: {
|
|
329
|
-
a: { type: 'string', component: 'Input', title: 'old-title' },
|
|
330
|
-
b: { type: 'string', component: 'Input' },
|
|
331
|
-
},
|
|
332
|
-
}
|
|
333
|
-
const wrapper = mountWithForm(form, () => <SchemaField hotReload components={{ Input }} schema={schema} />)
|
|
334
|
-
|
|
335
|
-
const firstA = form.fields['a'] as any
|
|
336
|
-
expect(firstA.title).toBe('old-title')
|
|
337
|
-
|
|
338
|
-
schema = {
|
|
339
|
-
type: 'object',
|
|
340
|
-
properties: {
|
|
341
|
-
a: { type: 'string', component: 'Input', title: 'new-title' },
|
|
342
|
-
c: { type: 'string', component: 'Input' },
|
|
343
|
-
},
|
|
344
|
-
}
|
|
345
|
-
wrapper.vm.$forceUpdate()
|
|
346
|
-
await nextTick()
|
|
347
|
-
await nextTick()
|
|
348
|
-
|
|
349
|
-
expect(form.fields['a']).not.toBe(firstA)
|
|
350
|
-
expect((form.fields['a'] as any).title).toBe('new-title')
|
|
351
|
-
expect(form.values.a).toBe('keep')
|
|
352
|
-
expect(form.values.b).toBeUndefined()
|
|
353
|
-
expect(form.fields['b']).toBeUndefined()
|
|
354
|
-
expect(form.fields['c']).toBeTruthy()
|
|
355
|
-
expect(wrapper.findAll('input').map(input => input.attributes('data-testid'))).toEqual(['a', 'c'])
|
|
356
|
-
})
|
|
357
|
-
|
|
358
|
-
test('schema prop 支持直接传入 Schema 实例', () => {
|
|
359
|
-
const form = createForm({ values: { a: 'instance' } })
|
|
360
|
-
const schema = new Schema({
|
|
361
|
-
type: 'object',
|
|
362
|
-
properties: {
|
|
363
|
-
a: { type: 'string', component: 'Input' },
|
|
364
|
-
},
|
|
365
|
-
})
|
|
366
|
-
const wrapper = mountWithForm(form, () => <SchemaField components={{ Input }} schema={schema} />)
|
|
367
|
-
|
|
368
|
-
expect(wrapper.find('input').element.value).toBe('instance')
|
|
369
|
-
})
|
|
370
|
-
|
|
371
|
-
test('无 properties 的 object schema 渲染为空', () => {
|
|
372
|
-
const form = createForm()
|
|
373
|
-
const wrapper = mountWithForm(form, () => <SchemaField components={{ Input }} schema={{ type: 'object' }} />)
|
|
374
|
-
|
|
375
|
-
expect(wrapper.find('input').exists()).toBe(false)
|
|
376
|
-
expect(Object.keys(form.fields)).toHaveLength(0)
|
|
377
|
-
})
|
|
378
|
-
|
|
379
|
-
test('onlyRenderProperties 无 name 时渲染当前 schema 的子属性', () => {
|
|
380
|
-
const form = createForm()
|
|
381
|
-
const wrapper = mountWithForm(form, () => (
|
|
382
|
-
<SchemaField
|
|
383
|
-
onlyRenderProperties
|
|
384
|
-
components={{ Input }}
|
|
385
|
-
schema={{
|
|
386
|
-
type: 'object',
|
|
387
|
-
properties: {
|
|
388
|
-
a: { type: 'string', component: 'Input' },
|
|
389
|
-
},
|
|
390
|
-
}}
|
|
391
|
-
/>
|
|
392
|
-
))
|
|
393
|
-
|
|
394
|
-
expect(wrapper.find('input').attributes('data-testid')).toBe('a')
|
|
395
|
-
})
|
|
396
|
-
|
|
397
|
-
test('void + onlyRenderProperties 只渲染子属性且不创建自身字段', () => {
|
|
398
|
-
const form = createForm()
|
|
399
|
-
const wrapper = mountWithForm(form, () => (
|
|
400
|
-
<SchemaField
|
|
401
|
-
name="v"
|
|
402
|
-
onlyRenderProperties
|
|
403
|
-
components={{ Input }}
|
|
404
|
-
schema={{
|
|
405
|
-
type: 'void',
|
|
406
|
-
properties: {
|
|
407
|
-
a: { type: 'string', component: 'Input' },
|
|
408
|
-
},
|
|
409
|
-
}}
|
|
410
|
-
/>
|
|
411
|
-
))
|
|
412
|
-
|
|
413
|
-
expect(form.fields['v']).toBeUndefined()
|
|
414
|
-
expect(form.fields['v.a']).toBeTruthy()
|
|
415
|
-
expect(wrapper.find('input').attributes('data-testid')).toBe('v.a')
|
|
416
|
-
})
|
|
417
|
-
})
|
|
418
|
-
|
|
419
|
-
const TypedInput = defineComponent({
|
|
420
|
-
name: 'TypedInput',
|
|
421
|
-
props: { maxLength: Number, placeholder: String },
|
|
422
|
-
setup: () => () => <input />,
|
|
423
|
-
})
|
|
424
|
-
|
|
425
|
-
const TypedArray = defineComponent({
|
|
426
|
-
name: 'TypedArray',
|
|
427
|
-
props: { maxItems: Number },
|
|
428
|
-
setup: () => () => <div />,
|
|
429
|
-
})
|
|
430
|
-
|
|
431
|
-
const TypedLayout = defineComponent({
|
|
432
|
-
name: 'TypedLayout',
|
|
433
|
-
props: { colon: Boolean },
|
|
434
|
-
setup: () => () => <div />,
|
|
435
|
-
})
|
|
436
|
-
|
|
437
|
-
const demoComponents = {
|
|
438
|
-
Input: TypedInput,
|
|
439
|
-
}
|
|
440
|
-
|
|
441
|
-
describe('createSchemaField 返回 SchemaField + fragment', () => {
|
|
442
|
-
test('运行时:工厂返回 { SchemaField, fragment } 双 key', () => {
|
|
443
|
-
const built = createSchemaField({ components: { Input: TypedInput } })
|
|
444
|
-
|
|
445
|
-
expect(Object.keys(built).sort()).toEqual(['SchemaField', 'fragment'])
|
|
446
|
-
expect(built.SchemaField).toBeTruthy()
|
|
447
|
-
expect(typeof built.fragment.Field).toBe('function')
|
|
448
|
-
})
|
|
449
|
-
|
|
450
|
-
test('运行时:分类入参传组件 map,动态构造器按分类产出节点', () => {
|
|
451
|
-
const { fragment: f } = createSchemaField({
|
|
452
|
-
components: { Input: TypedInput },
|
|
453
|
-
voidComponents: { Layout: TypedLayout },
|
|
454
|
-
arrayComponents: { ArrayList: TypedArray },
|
|
455
|
-
defaultDecorator: 'Layout',
|
|
456
|
-
})
|
|
457
|
-
|
|
458
|
-
expect(f.Field()()).toMatchObject({ type: 'string' })
|
|
459
|
-
expect(f.Input({ title: '姓名' })()).toMatchObject({
|
|
460
|
-
type: 'string',
|
|
461
|
-
component: 'Input',
|
|
462
|
-
title: '姓名',
|
|
463
|
-
decorator: 'Layout',
|
|
464
|
-
})
|
|
465
|
-
expect(f.ArrayList(f.Input())()).toMatchObject({
|
|
466
|
-
type: 'array',
|
|
467
|
-
component: 'ArrayList',
|
|
468
|
-
items: { type: 'string', component: 'Input' },
|
|
469
|
-
})
|
|
470
|
-
expect(f.Layout({})()).toMatchObject({ type: 'void', component: 'Layout' })
|
|
471
|
-
expect(
|
|
472
|
-
f.Object({
|
|
473
|
-
name: f.Input({ required: true }),
|
|
474
|
-
})()
|
|
475
|
-
).toMatchObject({ type: 'object', properties: { name: { type: 'string', component: 'Input' } } })
|
|
476
|
-
})
|
|
477
|
-
|
|
478
|
-
test('运行时:预绑定 SchemaField 无需再传 components 即可渲染 fragment 产出的 schema', () => {
|
|
479
|
-
const { SchemaField: BoundSchemaField, fragment: f } = createSchemaField({
|
|
480
|
-
components: { Input: TypedInput },
|
|
481
|
-
arrayComponents: { ArrayList: TypedArray },
|
|
482
|
-
})
|
|
483
|
-
const schema = f.Object({ name: f.Input({ required: true }) })()
|
|
484
|
-
const form = createForm()
|
|
485
|
-
|
|
486
|
-
const wrapper = mountWithForm(form, () => <BoundSchemaField schema={schema} />)
|
|
487
|
-
|
|
488
|
-
expect(wrapper.find('input').exists()).toBe(true)
|
|
489
|
-
expect(form.fields['name']).toBeTruthy()
|
|
490
|
-
})
|
|
491
|
-
|
|
492
|
-
test('运行时:预绑定 SchemaField 支持 props 传扁平 map 覆盖同名组件', () => {
|
|
493
|
-
const OverrideInput = defineComponent({
|
|
494
|
-
name: 'OverrideInput',
|
|
495
|
-
props: ['value'],
|
|
496
|
-
setup: () => () => <input class="override-input" />,
|
|
497
|
-
})
|
|
498
|
-
const { SchemaField: BoundSchemaField, fragment: f } = createSchemaField({
|
|
499
|
-
components: { Input: TypedInput },
|
|
500
|
-
})
|
|
501
|
-
const schema = f.Object({ name: f.Input() })()
|
|
502
|
-
const form = createForm()
|
|
503
|
-
|
|
504
|
-
const wrapper = mountWithForm(form, () => (
|
|
505
|
-
<BoundSchemaField schema={schema} components={{ Input: OverrideInput }} />
|
|
506
|
-
))
|
|
507
|
-
|
|
508
|
-
expect(wrapper.find('.override-input').exists()).toBe(true)
|
|
509
|
-
})
|
|
510
|
-
|
|
511
|
-
test('运行时:无参调用仍可用,动态构造器运行时保持宽松', () => {
|
|
512
|
-
const { fragment: f } = createSchemaField()
|
|
513
|
-
expect(f.Field()()).toMatchObject({ type: 'string' })
|
|
514
|
-
expect((f as any).Anything()()).toMatchObject({ type: 'string', component: 'Anything' })
|
|
515
|
-
})
|
|
516
|
-
|
|
517
|
-
test('类型:组件 props、父子值类型与 scope 全部由扁平 components 推断,无需手写泛型', () => {
|
|
518
|
-
const { fragment: f } = createSchemaField({
|
|
519
|
-
components: demoComponents,
|
|
520
|
-
voidComponents: { Layout: TypedLayout },
|
|
521
|
-
arrayComponents: { ArrayList: TypedArray },
|
|
522
|
-
scope: { $moment: (value: string) => value },
|
|
523
|
-
})
|
|
524
|
-
|
|
525
|
-
// @ts-expect-error 分类入参只接收组件 map,不再接收名字数组
|
|
526
|
-
createSchemaField({ components: demoComponents, arrayComponents: ['ArrayList'] })
|
|
527
|
-
|
|
528
|
-
f.Input({ componentProps: { maxLength: 10, placeholder: 'name' } })
|
|
529
|
-
// @ts-expect-error componentProps 由组件 props 推导,maxLength 必须是 number
|
|
530
|
-
f.Input({ componentProps: { maxLength: '10' } })
|
|
531
|
-
// @ts-expect-error 未在 components 中声明的组件名不在类型表面
|
|
532
|
-
void f.NotExist
|
|
533
|
-
|
|
534
|
-
f.Object<{ name: string; age: number }>({
|
|
535
|
-
name: f.Field({
|
|
536
|
-
required: true,
|
|
537
|
-
reactions: (field, scope) => {
|
|
538
|
-
expectTypeOf(field.value).toEqualTypeOf<string>()
|
|
539
|
-
expectTypeOf(scope.$moment).toEqualTypeOf<(value: string) => string>()
|
|
540
|
-
expectTypeOf(scope.$record).toEqualTypeOf<{ name: string; age: number } & { $lookup: any; $index: number }>()
|
|
541
|
-
},
|
|
542
|
-
}),
|
|
543
|
-
age: f.Field({
|
|
544
|
-
required: true,
|
|
545
|
-
reactions: field => {
|
|
546
|
-
expectTypeOf(field.value).toEqualTypeOf<number>()
|
|
547
|
-
},
|
|
548
|
-
}),
|
|
549
|
-
layout: f.Layout({}),
|
|
550
|
-
})
|
|
551
|
-
|
|
552
|
-
// @ts-expect-error 未在父级 record 中声明的 key 不允许产生值的字段,错误落在调用表达式上
|
|
553
|
-
f.Object<{ name: string }>({
|
|
554
|
-
name: f.Field({ required: true }),
|
|
555
|
-
extra: f.Field(),
|
|
556
|
-
})
|
|
557
|
-
|
|
558
|
-
f.ArrayList(
|
|
559
|
-
f.Object<{ name: string }>({
|
|
560
|
-
name: f.Field({
|
|
561
|
-
required: true,
|
|
562
|
-
reactions: (_field, scope) => {
|
|
563
|
-
expectTypeOf(scope.$record).toEqualTypeOf<{ name: string } & { $lookup: any; $index: number }>()
|
|
564
|
-
},
|
|
565
|
-
}),
|
|
566
|
-
}),
|
|
567
|
-
{
|
|
568
|
-
reactions: (field, scope) => {
|
|
569
|
-
expectTypeOf(field.value).toEqualTypeOf<{ name: string }[]>()
|
|
570
|
-
expectTypeOf(scope.$moment).toEqualTypeOf<(value: string) => string>()
|
|
571
|
-
},
|
|
572
|
-
}
|
|
573
|
-
)
|
|
574
|
-
})
|
|
575
|
-
|
|
576
|
-
test('类型:defaultDecorator 推断后收窄 decoratorProps', () => {
|
|
577
|
-
const { fragment: f } = createSchemaField({
|
|
578
|
-
components: { Input: TypedInput },
|
|
579
|
-
voidComponents: { Layout: TypedLayout },
|
|
580
|
-
defaultDecorator: 'Layout',
|
|
581
|
-
})
|
|
582
|
-
|
|
583
|
-
f.Input({ title: '姓名', decoratorProps: { colon: true } })
|
|
584
|
-
// @ts-expect-error decoratorProps 按 Layout 的 props 推导
|
|
585
|
-
f.Input({ title: '姓名', decoratorProps: { unknown: 1 } })
|
|
586
|
-
})
|
|
587
|
-
})
|
package/src/__tests__/shared.tsx
DELETED
|
@@ -1,52 +0,0 @@
|
|
|
1
|
-
import type { Form } from '@formvk/core'
|
|
2
|
-
import { mount } from '@vue/test-utils'
|
|
3
|
-
import { defineComponent } from 'vue'
|
|
4
|
-
|
|
5
|
-
import { FormProvider, useField } from '..'
|
|
6
|
-
|
|
7
|
-
export const Input = defineComponent({
|
|
8
|
-
name: 'Input',
|
|
9
|
-
props: ['value'],
|
|
10
|
-
setup(props, { attrs }) {
|
|
11
|
-
const fieldRef = useField()
|
|
12
|
-
return () => (
|
|
13
|
-
<input
|
|
14
|
-
class="input"
|
|
15
|
-
data-testid={fieldRef.value?.path?.toString()}
|
|
16
|
-
value={props.value}
|
|
17
|
-
onInput={(event: Event) => (attrs.onChange as any)?.(event)}
|
|
18
|
-
onFocus={(event: Event) => (attrs.onFocus as any)?.(event)}
|
|
19
|
-
onBlur={(event: Event) => (attrs.onBlur as any)?.(event)}
|
|
20
|
-
/>
|
|
21
|
-
)
|
|
22
|
-
},
|
|
23
|
-
})
|
|
24
|
-
|
|
25
|
-
export const Decorator = defineComponent({
|
|
26
|
-
name: 'Decorator',
|
|
27
|
-
setup(_, { slots }) {
|
|
28
|
-
return () => (
|
|
29
|
-
<div class="decorator">
|
|
30
|
-
{slots.label?.()}
|
|
31
|
-
{slots.default?.()}
|
|
32
|
-
</div>
|
|
33
|
-
)
|
|
34
|
-
},
|
|
35
|
-
})
|
|
36
|
-
|
|
37
|
-
export const Container = defineComponent({
|
|
38
|
-
name: 'Container',
|
|
39
|
-
setup(_, { slots }) {
|
|
40
|
-
return () => <div class="container">{slots.default?.()}</div>
|
|
41
|
-
},
|
|
42
|
-
})
|
|
43
|
-
|
|
44
|
-
export function mountWithForm(form: Form, render: () => any) {
|
|
45
|
-
return mount(
|
|
46
|
-
defineComponent({
|
|
47
|
-
setup() {
|
|
48
|
-
return () => <FormProvider form={form}>{render()}</FormProvider>
|
|
49
|
-
},
|
|
50
|
-
})
|
|
51
|
-
)
|
|
52
|
-
}
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
import { lazyMerge } from '@formvk/shared'
|
|
2
|
-
import { computed, defineComponent, inject, provide, ref } from 'vue'
|
|
3
|
-
|
|
4
|
-
import { SchemaExpressionScopeSymbol } from '../shared/context'
|
|
5
|
-
|
|
6
|
-
export const ExpressionScope = defineComponent({
|
|
7
|
-
name: 'ExpressionScope',
|
|
8
|
-
inheritAttrs: false,
|
|
9
|
-
props: ['value'],
|
|
10
|
-
setup(props, { slots }) {
|
|
11
|
-
const scopeRef = inject(SchemaExpressionScopeSymbol, ref({}))
|
|
12
|
-
const expressionScopeRef = computed(() => lazyMerge(scopeRef.value, props.value))
|
|
13
|
-
provide(SchemaExpressionScopeSymbol, expressionScopeRef)
|
|
14
|
-
|
|
15
|
-
return () => slots.default?.()
|
|
16
|
-
},
|
|
17
|
-
})
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
import type { SlotsType } from 'vue'
|
|
2
|
-
import { defineComponent } from 'vue'
|
|
3
|
-
|
|
4
|
-
import { useForm } from '../hooks/useForm'
|
|
5
|
-
import type { IFormConsumerSlotProps } from '../types'
|
|
6
|
-
|
|
7
|
-
export const FormConsumer = defineComponent({
|
|
8
|
-
name: 'FormConsumer',
|
|
9
|
-
inheritAttrs: false,
|
|
10
|
-
slots: Object as SlotsType<{
|
|
11
|
-
default: IFormConsumerSlotProps
|
|
12
|
-
}>,
|
|
13
|
-
setup(_, { slots }) {
|
|
14
|
-
const formRef = useForm()
|
|
15
|
-
return () => slots.default?.({ form: formRef.value }) ?? null
|
|
16
|
-
},
|
|
17
|
-
})
|
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
import { defineComponent, provide, toRef } from 'vue'
|
|
2
|
-
|
|
3
|
-
import { useAttach } from '../hooks/useAttach'
|
|
4
|
-
import { useInjectionCleaner } from '../hooks/useInjectionCleaner'
|
|
5
|
-
import {
|
|
6
|
-
FieldSymbol,
|
|
7
|
-
FormSymbol,
|
|
8
|
-
SchemaExpressionScopeSymbol,
|
|
9
|
-
SchemaOptionsSymbol,
|
|
10
|
-
SchemaSymbol,
|
|
11
|
-
} from '../shared/context'
|
|
12
|
-
|
|
13
|
-
export const FormProvider = defineComponent({
|
|
14
|
-
name: 'FormProvider',
|
|
15
|
-
inheritAttrs: false,
|
|
16
|
-
props: ['form'],
|
|
17
|
-
setup(props, { slots }) {
|
|
18
|
-
const formRef = useAttach(toRef(props, 'form'))
|
|
19
|
-
provide(FormSymbol, formRef)
|
|
20
|
-
useInjectionCleaner([FieldSymbol, SchemaSymbol, SchemaExpressionScopeSymbol, SchemaOptionsSymbol])
|
|
21
|
-
|
|
22
|
-
return () => slots.default?.()
|
|
23
|
-
},
|
|
24
|
-
})
|