Skip to content

Commit 05e14ce

Browse files
authored
fix(vue): prop "scope" of SchemaField not work with x-reactions (#1976)
1 parent 6a437c8 commit 05e14ce

File tree

2 files changed

+119
-7
lines changed

2 files changed

+119
-7
lines changed

packages/vue/src/__tests__/schema.json.spec.ts

Lines changed: 117 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { createForm } from '@formily/core'
1+
import { createForm, isField } from '@formily/core'
22
import { Schema } from '@formily/json-schema'
3-
import { render } from '@testing-library/vue'
3+
import { render, waitFor } from '@testing-library/vue'
44
import Vue, { FunctionalComponentOptions } from 'vue'
55
import { FormProvider, createSchemaField } from '../vue2-components'
66

@@ -21,6 +21,21 @@ const Input: FunctionalComponentOptions = {
2121
},
2222
}
2323

24+
const Input2: FunctionalComponentOptions = {
25+
functional: true,
26+
render(h, context) {
27+
return h('input', {
28+
attrs: {
29+
value: context.props.value,
30+
'data-testid': 'input2',
31+
},
32+
on: {
33+
input: context.listeners.change,
34+
},
35+
})
36+
},
37+
}
38+
2439
const Previewer: FunctionalComponentOptions = {
2540
functional: true,
2641
render(h, context) {
@@ -296,3 +311,103 @@ describe('x-content', () => {
296311
expect(queryByTestId('previewer2').textContent).toEqual('123')
297312
})
298313
})
314+
315+
describe('scope', () => {
316+
test('scope in <SchemaField> prop', async () => {
317+
const form = createForm()
318+
const { SchemaField } = createSchemaField({
319+
components: {
320+
Input,
321+
Input2,
322+
Previewer,
323+
},
324+
})
325+
const { queryByTestId } = render({
326+
components: { SchemaField },
327+
data() {
328+
return {
329+
form,
330+
schema: {
331+
type: 'object',
332+
properties: {
333+
input1: {
334+
type: 'string',
335+
'x-component': 'Input',
336+
'x-reactions': {
337+
target: 'input2',
338+
fulfill: {
339+
state: {
340+
value: '{{ test }}',
341+
},
342+
},
343+
},
344+
},
345+
input2: {
346+
type: 'string',
347+
'x-component': 'Input2',
348+
},
349+
},
350+
},
351+
}
352+
},
353+
template: `<FormProvider :form="form">
354+
<SchemaField
355+
:schema="schema"
356+
:scope="{
357+
test: '123'
358+
}"
359+
/>
360+
</FormProvider>`,
361+
})
362+
expect(queryByTestId('input2').getAttribute('value')).toEqual('123')
363+
})
364+
365+
test('scope in options of createSchemaField', async () => {
366+
const form = createForm()
367+
const { SchemaField } = createSchemaField({
368+
components: {
369+
Input,
370+
Input2,
371+
Previewer,
372+
},
373+
scope: {
374+
test: '123',
375+
},
376+
})
377+
const { queryByTestId } = render({
378+
components: { SchemaField },
379+
data() {
380+
return {
381+
form,
382+
schema: {
383+
type: 'object',
384+
properties: {
385+
input1: {
386+
type: 'string',
387+
'x-component': 'Input',
388+
'x-reactions': {
389+
target: 'input2',
390+
fulfill: {
391+
state: {
392+
value: '{{ test }}',
393+
},
394+
},
395+
},
396+
},
397+
input2: {
398+
type: 'string',
399+
'x-component': 'Input2',
400+
},
401+
},
402+
},
403+
}
404+
},
405+
template: `<FormProvider :form="form">
406+
<SchemaField
407+
:schema="schema"
408+
/>
409+
</FormProvider>`,
410+
})
411+
expect(queryByTestId('input2').getAttribute('value')).toEqual('123')
412+
})
413+
})

packages/vue/src/components/RecursionField.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,7 @@ const RecursionField = observer(
5757
const createSchema = (schemaProp: IRecursionFieldProps['schema']) =>
5858
new Schema(schemaProp)
5959
const createFieldSchema = (schema: Schema) =>
60-
schema.compile?.({
61-
...optionsRef.value.scope,
62-
...scopeRef.value,
63-
})
60+
schema.compile?.(scopeRef.value)
6461
const schemaRef = shallowRef(createSchema(props.schema))
6562
const fieldSchemaRef = shallowRef(createFieldSchema(schemaRef.value))
6663
watch([() => props.schema, scopeRef, optionsRef], () => {
@@ -69,7 +66,7 @@ const RecursionField = observer(
6966
})
7067

7168
const getPropsFromSchema = (schema: Schema) =>
72-
schema?.toFieldProps?.(optionsRef.value)
69+
schema?.toFieldProps?.({ ...optionsRef.value, scope: scopeRef.value })
7370
const fieldPropsRef = shallowRef(getPropsFromSchema(fieldSchemaRef.value))
7471
watch([fieldSchemaRef, optionsRef], () => {
7572
fieldPropsRef.value = getPropsFromSchema(fieldSchemaRef.value)

0 commit comments

Comments
 (0)