Skip to content

Commit

Permalink
fix(vue): prop "scope" of SchemaField not work with x-reactions (#1976)
Browse files Browse the repository at this point in the history
  • Loading branch information
MisicDemone authored Aug 10, 2021
1 parent 6a437c8 commit 05e14ce
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 7 deletions.
119 changes: 117 additions & 2 deletions packages/vue/src/__tests__/schema.json.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { createForm } from '@formily/core'
import { createForm, isField } from '@formily/core'
import { Schema } from '@formily/json-schema'
import { render } from '@testing-library/vue'
import { render, waitFor } from '@testing-library/vue'
import Vue, { FunctionalComponentOptions } from 'vue'
import { FormProvider, createSchemaField } from '../vue2-components'

Expand All @@ -21,6 +21,21 @@ const Input: FunctionalComponentOptions = {
},
}

const Input2: FunctionalComponentOptions = {
functional: true,
render(h, context) {
return h('input', {
attrs: {
value: context.props.value,
'data-testid': 'input2',
},
on: {
input: context.listeners.change,
},
})
},
}

const Previewer: FunctionalComponentOptions = {
functional: true,
render(h, context) {
Expand Down Expand Up @@ -296,3 +311,103 @@ describe('x-content', () => {
expect(queryByTestId('previewer2').textContent).toEqual('123')
})
})

describe('scope', () => {
test('scope in <SchemaField> prop', async () => {
const form = createForm()
const { SchemaField } = createSchemaField({
components: {
Input,
Input2,
Previewer,
},
})
const { queryByTestId } = render({
components: { SchemaField },
data() {
return {
form,
schema: {
type: 'object',
properties: {
input1: {
type: 'string',
'x-component': 'Input',
'x-reactions': {
target: 'input2',
fulfill: {
state: {
value: '{{ test }}',
},
},
},
},
input2: {
type: 'string',
'x-component': 'Input2',
},
},
},
}
},
template: `<FormProvider :form="form">
<SchemaField
:schema="schema"
:scope="{
test: '123'
}"
/>
</FormProvider>`,
})
expect(queryByTestId('input2').getAttribute('value')).toEqual('123')
})

test('scope in options of createSchemaField', async () => {
const form = createForm()
const { SchemaField } = createSchemaField({
components: {
Input,
Input2,
Previewer,
},
scope: {
test: '123',
},
})
const { queryByTestId } = render({
components: { SchemaField },
data() {
return {
form,
schema: {
type: 'object',
properties: {
input1: {
type: 'string',
'x-component': 'Input',
'x-reactions': {
target: 'input2',
fulfill: {
state: {
value: '{{ test }}',
},
},
},
},
input2: {
type: 'string',
'x-component': 'Input2',
},
},
},
}
},
template: `<FormProvider :form="form">
<SchemaField
:schema="schema"
/>
</FormProvider>`,
})
expect(queryByTestId('input2').getAttribute('value')).toEqual('123')
})
})
7 changes: 2 additions & 5 deletions packages/vue/src/components/RecursionField.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,7 @@ const RecursionField = observer(
const createSchema = (schemaProp: IRecursionFieldProps['schema']) =>
new Schema(schemaProp)
const createFieldSchema = (schema: Schema) =>
schema.compile?.({
...optionsRef.value.scope,
...scopeRef.value,
})
schema.compile?.(scopeRef.value)
const schemaRef = shallowRef(createSchema(props.schema))
const fieldSchemaRef = shallowRef(createFieldSchema(schemaRef.value))
watch([() => props.schema, scopeRef, optionsRef], () => {
Expand All @@ -69,7 +66,7 @@ const RecursionField = observer(
})

const getPropsFromSchema = (schema: Schema) =>
schema?.toFieldProps?.(optionsRef.value)
schema?.toFieldProps?.({ ...optionsRef.value, scope: scopeRef.value })
const fieldPropsRef = shallowRef(getPropsFromSchema(fieldSchemaRef.value))
watch([fieldSchemaRef, optionsRef], () => {
fieldPropsRef.value = getPropsFromSchema(fieldSchemaRef.value)
Expand Down

0 comments on commit 05e14ce

Please sign in to comment.