Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(vue): prop "scope" of SchemaField not work with x-reactions #1976

Merged
merged 2 commits into from
Aug 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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