From c924f4ca82d5e9f7d48191e3cd62498a2b850ccf Mon Sep 17 00:00:00 2001 From: tangjinzhou <415800467@qq.com> Date: Mon, 14 Jan 2019 22:42:04 +0800 Subject: [PATCH] perf: form --- components/form/Form.jsx | 5 ++ components/form/__tests__/message.test.js | 14 ++--- components/vc-form/demo/dynamic-fields.js | 70 +++++++++++++++++++++++ components/vc-form/src/createBaseForm.jsx | 16 +++--- package.json | 2 +- 5 files changed, 92 insertions(+), 15 deletions(-) diff --git a/components/form/Form.jsx b/components/form/Form.jsx index 91fd6cd16..f6dd4567b 100755 --- a/components/form/Form.jsx +++ b/components/form/Form.jsx @@ -149,6 +149,11 @@ const Form = { this.$forceUpdate(); }, }, + updated(){ + if(this.autoFormCreate || this.form) { + this.form.cleanUpUselessFields(); + } + }, methods: { onSubmit(e) { const { $listeners } = this; diff --git a/components/form/__tests__/message.test.js b/components/form/__tests__/message.test.js index 917927af5..dd819ae0b 100644 --- a/components/form/__tests__/message.test.js +++ b/components/form/__tests__/message.test.js @@ -1,8 +1,9 @@ import { mount } from '@vue/test-utils'; +import { asyncExpect } from '@/tests/utils'; import Form from '..'; describe('Form', () => { - it('should display two message', () => { + it('should display two message', async () => { const rules = [ { pattern: /^\w+$/, @@ -16,6 +17,7 @@ describe('Form', () => { let myForm; const Form1 = Form.create()({ render() { + myForm = this.form; return (
@@ -27,13 +29,11 @@ describe('Form', () => { }); const wrapper = mount(Form1, { - propsData: { - wrappedComponentRef: inst => { - myForm = inst.form; - }, - }, + sync: false, }); - myForm.validateFields(); + await asyncExpect(()=>{ + myForm.validateFields(); + }); wrapper.vm.$forceUpdate(); expect(wrapper.html()).toMatchSnapshot(); diff --git a/components/vc-form/demo/dynamic-fields.js b/components/vc-form/demo/dynamic-fields.js index fc7925a7e..140c2dc63 100644 --- a/components/vc-form/demo/dynamic-fields.js +++ b/components/vc-form/demo/dynamic-fields.js @@ -200,9 +200,78 @@ const Form3 = { }, }; +const Form4 = { + mixins: [BaseMixin], + props: { + form: Object, + }, + data() { + return { + useInput: true, + }; + }, + methods: { + onSubmit(e) { + e.preventDefault(); + this.form.validateFields((error, values) => { + if (!error) { + console.log('ok', values); + } else { + console.log('error', error, values); + } + }); + }, + changeUseInput(e) { + this.setState({ + useInput: e.target.checked, + }); + }, + }, + + render() { + const { getFieldError, getFieldDecorator } = this.form; + return ( + +

situation 4

+ {this.useInput + ? getFieldDecorator('name', { + initialValue: '', + trigger: 'input', + rules: [ + { + required: true, + message: "What's your name 1?", + }, + ], + })() + : getFieldDecorator('name2', { + initialValue: '', + trigger: 'input', + rules: [ + { + required: true, + message: "What's your name 2?", + }, + ], + })()} +
+ + {(getFieldError('name') || []).join(', ')} + {(getFieldError('name2') || []).join(', ')} +
+ + + ); + }, +}; + const WrappedForm1 = createForm()(Form1); const WrappedForm2 = createForm()(Form2); const WrappedForm3 = createForm()(Form3); +const WrappedForm4 = createForm()(Form4); export default { render() { @@ -211,6 +280,7 @@ export default { + ); }, diff --git a/components/vc-form/src/createBaseForm.jsx b/components/vc-form/src/createBaseForm.jsx index d1d7298b1..310b7ccae 100644 --- a/components/vc-form/src/createBaseForm.jsx +++ b/components/vc-form/src/createBaseForm.jsx @@ -98,16 +98,12 @@ function createBaseForm(option = {}, mixins = []) { }, }, mounted() { - this.wrappedComponentRef(this.$refs.WrappedComponent); this.cleanUpUselessFields(); }, updated() { - this.wrappedComponentRef(this.$refs.WrappedComponent); + // form updated add for template v-decorator this.cleanUpUselessFields(); }, - destroyed() { - this.wrappedComponentRef(null); - }, methods: { updateFields(fields = {}) { this.fieldsStore.updateFields(mapPropsToFields(fields)); @@ -636,14 +632,20 @@ function createBaseForm(option = {}, mixins = []) { const formProps = { [formPropName]: this.getForm(), }; - const props = getOptionProps(this); + const {wrappedComponentRef, ...restProps} = getOptionProps(this); const wrappedComponentProps = { props: mapProps.call(this, { ...formProps, - ...props, + ...restProps, }), on: $listeners, ref: 'WrappedComponent', + directives: [ + { + name: 'ant-ref', + value: wrappedComponentRef, + }, + ], }; return WrappedComponent ? ( diff --git a/package.json b/package.json index 269bea858..61d295a4f 100644 --- a/package.json +++ b/package.json @@ -188,7 +188,7 @@ "resize-observer-polyfill": "^1.5.1", "shallow-equal": "^1.0.0", "shallowequal": "^1.0.2", - "vue-ref": "^1.0.3", + "vue-ref": "^1.0.4", "warning": "^3.0.0" } }