Skip to content

Commit c924f4c

Browse files
committed
perf: form
1 parent f686674 commit c924f4c

File tree

5 files changed

+92
-15
lines changed

5 files changed

+92
-15
lines changed

components/form/Form.jsx

+5
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,11 @@ const Form = {
149149
this.$forceUpdate();
150150
},
151151
},
152+
updated(){
153+
if(this.autoFormCreate || this.form) {
154+
this.form.cleanUpUselessFields();
155+
}
156+
},
152157
methods: {
153158
onSubmit(e) {
154159
const { $listeners } = this;

components/form/__tests__/message.test.js

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import { mount } from '@vue/test-utils';
2+
import { asyncExpect } from '@/tests/utils';
23
import Form from '..';
34

45
describe('Form', () => {
5-
it('should display two message', () => {
6+
it('should display two message', async () => {
67
const rules = [
78
{
89
pattern: /^\w+$/,
@@ -16,6 +17,7 @@ describe('Form', () => {
1617
let myForm;
1718
const Form1 = Form.create()({
1819
render() {
20+
myForm = this.form;
1921
return (
2022
<Form>
2123
<Form.Item label="Account">
@@ -27,13 +29,11 @@ describe('Form', () => {
2729
});
2830

2931
const wrapper = mount(Form1, {
30-
propsData: {
31-
wrappedComponentRef: inst => {
32-
myForm = inst.form;
33-
},
34-
},
32+
sync: false,
3533
});
36-
myForm.validateFields();
34+
await asyncExpect(()=>{
35+
myForm.validateFields();
36+
});
3737

3838
wrapper.vm.$forceUpdate();
3939
expect(wrapper.html()).toMatchSnapshot();

components/vc-form/demo/dynamic-fields.js

+70
Original file line numberDiff line numberDiff line change
@@ -200,9 +200,78 @@ const Form3 = {
200200
},
201201
};
202202

203+
const Form4 = {
204+
mixins: [BaseMixin],
205+
props: {
206+
form: Object,
207+
},
208+
data() {
209+
return {
210+
useInput: true,
211+
};
212+
},
213+
methods: {
214+
onSubmit(e) {
215+
e.preventDefault();
216+
this.form.validateFields((error, values) => {
217+
if (!error) {
218+
console.log('ok', values);
219+
} else {
220+
console.log('error', error, values);
221+
}
222+
});
223+
},
224+
changeUseInput(e) {
225+
this.setState({
226+
useInput: e.target.checked,
227+
});
228+
},
229+
},
230+
231+
render() {
232+
const { getFieldError, getFieldDecorator } = this.form;
233+
return (
234+
<form onSubmit={this.onSubmit}>
235+
<h2>situation 4</h2>
236+
{this.useInput
237+
? getFieldDecorator('name', {
238+
initialValue: '',
239+
trigger: 'input',
240+
rules: [
241+
{
242+
required: true,
243+
message: "What's your name 1?",
244+
},
245+
],
246+
})(<input />)
247+
: getFieldDecorator('name2', {
248+
initialValue: '',
249+
trigger: 'input',
250+
rules: [
251+
{
252+
required: true,
253+
message: "What's your name 2?",
254+
},
255+
],
256+
})(<input />)}
257+
<div>
258+
<label>
259+
<input type="checkbox" checked={this.useInput} onInput={this.changeUseInput} />
260+
toggle decorator name
261+
</label>
262+
{(getFieldError('name') || []).join(', ')}
263+
{(getFieldError('name2') || []).join(', ')}
264+
</div>
265+
<button>Submit</button>
266+
</form>
267+
);
268+
},
269+
};
270+
203271
const WrappedForm1 = createForm()(Form1);
204272
const WrappedForm2 = createForm()(Form2);
205273
const WrappedForm3 = createForm()(Form3);
274+
const WrappedForm4 = createForm()(Form4);
206275

207276
export default {
208277
render() {
@@ -211,6 +280,7 @@ export default {
211280
<WrappedForm1 />
212281
<WrappedForm2 />
213282
<WrappedForm3 />
283+
<WrappedForm4 />
214284
</div>
215285
);
216286
},

components/vc-form/src/createBaseForm.jsx

+9-7
Original file line numberDiff line numberDiff line change
@@ -98,16 +98,12 @@ function createBaseForm(option = {}, mixins = []) {
9898
},
9999
},
100100
mounted() {
101-
this.wrappedComponentRef(this.$refs.WrappedComponent);
102101
this.cleanUpUselessFields();
103102
},
104103
updated() {
105-
this.wrappedComponentRef(this.$refs.WrappedComponent);
104+
// form updated add for template v-decorator
106105
this.cleanUpUselessFields();
107106
},
108-
destroyed() {
109-
this.wrappedComponentRef(null);
110-
},
111107
methods: {
112108
updateFields(fields = {}) {
113109
this.fieldsStore.updateFields(mapPropsToFields(fields));
@@ -636,14 +632,20 @@ function createBaseForm(option = {}, mixins = []) {
636632
const formProps = {
637633
[formPropName]: this.getForm(),
638634
};
639-
const props = getOptionProps(this);
635+
const {wrappedComponentRef, ...restProps} = getOptionProps(this);
640636
const wrappedComponentProps = {
641637
props: mapProps.call(this, {
642638
...formProps,
643-
...props,
639+
...restProps,
644640
}),
645641
on: $listeners,
646642
ref: 'WrappedComponent',
643+
directives: [
644+
{
645+
name: 'ant-ref',
646+
value: wrappedComponentRef,
647+
},
648+
],
647649
};
648650

649651
return WrappedComponent ? (

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@
188188
"resize-observer-polyfill": "^1.5.1",
189189
"shallow-equal": "^1.0.0",
190190
"shallowequal": "^1.0.2",
191-
"vue-ref": "^1.0.3",
191+
"vue-ref": "^1.0.4",
192192
"warning": "^3.0.0"
193193
}
194194
}

0 commit comments

Comments
 (0)