Skip to content

Commit 00a80b4

Browse files
authored
fix(vue): remove empty default slots of fields (#1517)
1 parent d3dd91c commit 00a80b4

File tree

15 files changed

+338
-270
lines changed

15 files changed

+338
-270
lines changed

package.json

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -57,16 +57,17 @@
5757
"@rollup/plugin-commonjs": "^17.0.0",
5858
"@testing-library/jest-dom": "^5.0.0",
5959
"@testing-library/react": "^11.2.3",
60-
"@testing-library/vue": "^5.6.1",
61-
"@types/hoist-non-react-statics": "^3.3.1",
60+
"@testing-library/vue": "^5.6.2",
6261
"@types/fs-extra": "^8.1.0",
62+
"@types/hoist-non-react-statics": "^3.3.1",
6363
"@types/jest": "^24.0.18",
6464
"@types/node": "^12.6.8",
6565
"@types/react": "^17.0.0",
6666
"@types/react-dom": "^17.0.0",
6767
"@typescript-eslint/eslint-plugin": "^4.9.1",
6868
"@typescript-eslint/parser": "^4.8.2",
6969
"@umijs/plugin-sass": "^1.1.1",
70+
"@vue/test-utils": "^1.2.0",
7071
"antd": "^4.0.0",
7172
"chalk": "^2.4.2",
7273
"chokidar": "^2.1.2",
@@ -77,22 +78,22 @@
7778
"css-loader": "^5.0.0",
7879
"cz-conventional-changelog": "^2.1.0",
7980
"dumi": "^1.1.0-rc.8",
81+
"escape-string-regexp": "^4.0.0",
8082
"eslint": "^7.14.0",
81-
"semver": "^7.3.5",
82-
"string-similarity": "^4.0.4",
8383
"eslint-config-prettier": "^7.0.0",
8484
"eslint-plugin-import": "^2.13.0",
85+
"eslint-plugin-markdown": "^2.0.1",
8586
"eslint-plugin-node": "^11.1.0",
8687
"eslint-plugin-prettier": "^3.1.0",
8788
"eslint-plugin-promise": "^4.0.0",
8889
"eslint-plugin-react": "^7.14.2",
8990
"eslint-plugin-react-hooks": "^4.2.0",
90-
"eslint-plugin-markdown": "^2.0.1",
9191
"eslint-plugin-vue": "^7.0.1",
9292
"execa": "^5.0.0",
9393
"file-loader": "^5.0.2",
9494
"findup": "^0.1.5",
9595
"fs-extra": "^7.0.1",
96+
"gh-release": "^5.0.1",
9697
"ghooks": "^2.0.4",
9798
"glob": "^7.1.3",
9899
"html-webpack-plugin": "^3.2.0",
@@ -106,35 +107,37 @@
106107
"jest-styled-components": "6.3.3",
107108
"jest-watch-lerna-packages": "^1.1.0",
108109
"lerna": "^4.0.0",
109-
"gh-release": "^5.0.1",
110110
"less": "^4.1.1",
111111
"less-plugin-npm-import": "^2.1.0",
112-
"postcss": "^8.0.0",
113112
"lint-staged": "^8.2.1",
114113
"mfetch": "^0.2.27",
115114
"mobx": "^6.0.4",
116115
"mobx-react-lite": "^3.1.6",
117-
"escape-string-regexp": "^4.0.0",
118116
"onchange": "^5.2.0",
119117
"opencollective": "^1.0.3",
120118
"opencollective-postinstall": "^2.0.2",
121119
"param-case": "^3.0.4",
120+
"postcss": "^8.0.0",
122121
"prettier": "^2.2.1",
123122
"pretty-format": "^24.0.0",
124123
"pretty-quick": "^3.1.0",
125124
"raw-loader": "^4.0.0",
126125
"react": "^17.0.1",
127126
"react-dom": "^17.0.1",
127+
"react-mde": "^11.5.0",
128128
"react-test-renderer": "^16.11.0",
129129
"rimraf": "^3.0.0",
130130
"rollup": "^2.37.1",
131131
"rollup-plugin-external-globals": "^0.6.1",
132132
"rollup-plugin-node-resolve": "^5.2.0",
133+
"rollup-plugin-postcss": "^4.0.0",
133134
"rollup-plugin-terser": "^7.0.2",
134135
"rollup-plugin-typescript2": "^0.30.0",
135-
"rollup-plugin-postcss": "^4.0.0",
136+
"semver": "^7.3.5",
136137
"semver-regex": "^2.0.0",
138+
"showdown": "^1.9.1",
137139
"staged-git-files": "^1.1.2",
140+
"string-similarity": "^4.0.4",
138141
"style-loader": "^1.1.3",
139142
"styled-components": "^5.0.0",
140143
"ts-import-plugin": "1.6.1",
@@ -145,9 +148,7 @@
145148
"vue-eslint-parser": "^7.1.1",
146149
"webpack": "^4.41.5",
147150
"webpack-cli": "^3.3.10",
148-
"webpack-dev-server": "^3.10.1",
149-
"showdown": "^1.9.1",
150-
"react-mde": "^11.5.0"
151+
"webpack-dev-server": "^3.10.1"
151152
},
152153
"repository": {
153154
"type": "git",

packages/vue/docs/demos/api/components/recursion-field-with-component.vue

Lines changed: 54 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -15,46 +15,69 @@ import { defineComponent, h } from '@vue/composition-api'
1515
// or "import { defineComponent, h } from 'vue'" if using vue3
1616
import { Input, Button, Space } from 'ant-design-vue'
1717
import { createForm } from '@formily/core'
18-
import { FormProvider, createSchemaField, RecursionField, useField, useFieldSchema, observer } from '@formily/vue'
18+
import {
19+
FormProvider,
20+
createSchemaField,
21+
RecursionField,
22+
useField,
23+
useFieldSchema,
24+
observer,
25+
} from '@formily/vue'
1926
import 'ant-design-vue/dist/antd.css'
2027
21-
const ArrayItems = observer(defineComponent({
22-
props: ['value', 'disabled', 'readOnly'],
23-
setup (props) {
24-
const fieldRef = useField()
25-
const schemaRef = useFieldSchema()
28+
const ArrayItems = observer(
29+
defineComponent({
30+
setup() {
31+
const fieldRef = useField()
32+
const schemaRef = useFieldSchema()
2633
27-
return () => {
28-
const field = fieldRef.value
29-
const schema = schemaRef.value
30-
const items = props.value?.map((item, index) => {
31-
return h('div', { key: item.id, style: { marginBottom: '10px' } }, [
32-
h(Space, [
33-
// params of render function is different in vue3
34-
h(RecursionField, { props: { schema: schema.items, name: index } }),
35-
h(Button, { on: { click: () => field.remove(index) } }, ['Remove']),
34+
return () => {
35+
const field = fieldRef.value
36+
const schema = schemaRef.value
37+
const items = field.value?.map((item, index) => {
38+
return h('div', { key: item.id, style: { marginBottom: '10px' } }, [
39+
h(Space, [
40+
// params of render function is different in vue3
41+
h(RecursionField, {
42+
props: { schema: schema.items, name: index },
43+
}),
44+
h(Button, { on: { click: () => field.remove(index) } }, [
45+
'Remove',
46+
]),
47+
]),
3648
])
37-
])
38-
})
39-
const button = h(Button, { on: { click: () => field.push({ id: Date.now() }) } }, ['Add'])
40-
return h('div', [items, button])
41-
}
42-
},
43-
}))
49+
})
50+
const button = h(
51+
Button,
52+
{ on: { click: () => field.push({ id: Date.now() }) } },
53+
['Add']
54+
)
55+
return h('div', [items, button])
56+
}
57+
},
58+
})
59+
)
4460
45-
const { SchemaField, SchemaStringField, SchemaArrayField, SchemaObjectField } = createSchemaField({
46-
components: {
47-
ArrayItems,
48-
Input,
49-
},
50-
})
61+
const { SchemaField, SchemaStringField, SchemaArrayField, SchemaObjectField } =
62+
createSchemaField({
63+
components: {
64+
ArrayItems,
65+
Input,
66+
},
67+
})
5168
5269
export default {
53-
components: { FormProvider, SchemaField, SchemaStringField, SchemaArrayField, SchemaObjectField },
70+
components: {
71+
FormProvider,
72+
SchemaField,
73+
SchemaStringField,
74+
SchemaArrayField,
75+
SchemaObjectField,
76+
},
5477
data() {
5578
return {
56-
form: createForm()
79+
form: createForm(),
5780
}
58-
}
81+
},
5982
}
6083
</script>

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

Lines changed: 91 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
Schema,
99
} from '../index'
1010
import { render } from '@testing-library/vue'
11+
import { mount, createLocalVue } from '@vue/test-utils'
1112
import Vue, { CreateElement } from 'vue'
1213
import { defineComponent, h } from '@vue/composition-api'
1314

@@ -570,87 +571,94 @@ describe('recursion field', () => {
570571
expect(queryByTestId('input')).toBeNull()
571572
})
572573

573-
// test('schema reactions', async () => {
574-
// const form = createForm()
575-
// const components = createSchemaField({
576-
// components: {
577-
// Input,
578-
// },
579-
// })
580-
// const { queryByTestId } = render({
581-
// components: components,
582-
// data () {
583-
// return {
584-
// form,
585-
// reactions: [
586-
// {
587-
// when: '{{$form.values.aaa === "123"}}',
588-
// fulfill: {
589-
// state: {
590-
// visible: true,
591-
// },
592-
// },
593-
// otherwise: {
594-
// state: {
595-
// visible: false,
596-
// },
597-
// },
598-
// },
599-
// {
600-
// when: '{{$self.value === "123"}}',
601-
// target: 'ccc',
602-
// fulfill: {
603-
// schema: {
604-
// 'x-visible': true,
605-
// },
606-
// },
607-
// otherwise: {
608-
// schema: {
609-
// 'x-visible': false,
610-
// },
611-
// },
612-
// },
613-
// ]
614-
// }
615-
// },
616-
// template: `<FormProvider :form="form">
617-
// <SchemaField>
618-
// <SchemaStringField
619-
// name="aaa"
620-
// x-component="Input"
621-
// :x-component-props="{
622-
// 'data-testid': 'aaa',
623-
// }"
624-
// />
625-
// <SchemaStringField
626-
// name="bbb"
627-
// x-component="Input"
628-
// :x-component-props="{
629-
// 'data-testid': 'bbb',
630-
// }"
631-
// :x-reactions="reactions"
632-
// />
633-
// <SchemaStringField
634-
// name="ccc"
635-
// x-component="Input"
636-
// :x-component-props="{
637-
// 'data-testid': 'ccc',
638-
// }"
639-
// />
640-
// </SchemaField>
641-
// </FormProvider>`
642-
// })
643-
// expect(queryByTestId('bbb')).toBeNull()
644-
// fireEvent.update(queryByTestId('aaa'), '123')
645-
// expect(form.query('aaa').get('value')).toEqual('123')
646-
// await waitFor(() => {
647-
// expect(queryByTestId('bbb')).toBeVisible()
648-
// })
649-
// expect(queryByTestId('ccc')).toBeNull()
650-
// fireEvent.update(queryByTestId('bbb'), '123')
651-
// expect(form.query('bbb').get('value')).toEqual('123')
652-
// await waitFor(() => {
653-
// expect(queryByTestId('ccc')).toBeVisible()
654-
// })
655-
// })
574+
test('schema reactions', async () => {
575+
const div = document.createElement('div')
576+
document.body.appendChild(div)
577+
const form = createForm()
578+
const components = createSchemaField({
579+
components: {
580+
Input,
581+
},
582+
})
583+
const localVue = createLocalVue()
584+
localVue.component('FormProvider', FormProvider)
585+
const TestComponent = {
586+
components: components,
587+
data() {
588+
return {
589+
form,
590+
reactions: [
591+
{
592+
when: '{{$form.values.aaa === "123"}}',
593+
fulfill: {
594+
state: {
595+
visible: true,
596+
},
597+
},
598+
otherwise: {
599+
state: {
600+
visible: false,
601+
},
602+
},
603+
},
604+
{
605+
when: '{{$self.value === "123"}}',
606+
target: 'ccc',
607+
fulfill: {
608+
schema: {
609+
'x-visible': true,
610+
},
611+
},
612+
otherwise: {
613+
schema: {
614+
'x-visible': false,
615+
},
616+
},
617+
},
618+
],
619+
}
620+
},
621+
template: `<FormProvider :form="form">
622+
<SchemaField>
623+
<SchemaStringField
624+
name="aaa"
625+
x-component="Input"
626+
:x-component-props="{
627+
'class': 'aaa',
628+
}"
629+
/>
630+
<SchemaStringField
631+
name="bbb"
632+
x-component="Input"
633+
:x-component-props="{
634+
'class': 'bbb',
635+
}"
636+
:x-reactions="reactions"
637+
/>
638+
<SchemaStringField
639+
name="ccc"
640+
x-component="Input"
641+
:x-component-props="{
642+
'class': 'ccc',
643+
}"
644+
/>
645+
</SchemaField>
646+
</FormProvider>`,
647+
} as any
648+
const wrapper = mount(TestComponent, {
649+
attachTo: div,
650+
localVue,
651+
})
652+
expect(wrapper.find('.bbb').exists()).toBeFalsy()
653+
wrapper.find('.aaa').setValue('123')
654+
expect(form.query('aaa').get('value')).toEqual('123')
655+
await wrapper.vm.$forceUpdate()
656+
expect(wrapper.find('.bbb').exists()).toBeTruthy()
657+
expect(wrapper.find('.ccc').exists()).toBeFalsy()
658+
wrapper.find('.bbb').setValue('123')
659+
expect(form.query('bbb').get('value')).toEqual('123')
660+
await wrapper.vm.$forceUpdate()
661+
expect(wrapper.find('.ccc').exists()).toBeTruthy()
662+
wrapper.destroy()
663+
})
656664
})

0 commit comments

Comments
 (0)