fix(Form): incorrect validation order when add field dynamically

This commit is contained in:
chenjiahan 2020-04-01 21:15:31 +08:00 committed by neverland
parent 02e67fdd47
commit b8dea3c13b
5 changed files with 79 additions and 24 deletions

View File

@ -80,13 +80,13 @@ export default createComponent({
this.$nextTick(this.adjustSize);
if (this.vanForm) {
this.vanForm.fields.push(this);
this.vanForm.addField(this);
}
},
beforeDestroy() {
if (this.vanForm) {
this.vanForm.fields = this.vanForm.fields.filter(item => item !== this);
this.vanForm.removeField(this);
}
},

View File

@ -1,4 +1,5 @@
import { createNamespace } from '../utils';
import { sortChildren } from '../utils/vnodes';
const [createComponent, bem] = createNamespace('form');
@ -124,6 +125,15 @@ export default createComponent({
});
},
addField(field) {
this.fields.push(field);
sortChildren(this.fields, this);
},
removeField(field) {
this.fields = this.fields.filter(item => item !== field);
},
getValues() {
return this.fields.reduce((form, field) => {
form[field.name] = field.formValue;

View File

@ -35,3 +35,41 @@ test('dynamic add/remove fileds', async () => {
await submitForm(wrapper);
expect(onSubmit).toHaveBeenCalledWith({ B: '' });
});
test('dynamic add fileds when validate-first', async () => {
const onFailed = jest.fn();
const wrapper = mountForm({
template: `
<van-form validate-first @failed="onFailed">
<van-field
v-if="show"
name="A"
value=""
:rules="[{ required: true, message: 'A' }]"
/>
<van-field
name="B"
value=""
:rules="[{ required: true, message: 'B' }]"
/>
<van-button native-type="submit" />
</van-form>
`,
data() {
return {
show: false,
};
},
methods: {
onFailed,
},
});
await submitForm(wrapper);
expect(onFailed.mock.calls[0][0].errors[0].name).toEqual('B');
wrapper.setData({ show: true });
await submitForm(wrapper);
expect(onFailed.mock.calls[1][0].errors[0].name).toEqual('A');
});

View File

@ -1,26 +1,10 @@
import Vue, { VNode } from 'vue';
import Vue from 'vue';
import { sortChildren } from '../utils/vnodes';
type ChildrenMixinOptions = {
indexKey?: any;
};
function flattenVNodes(vnodes: VNode[]) {
const result: VNode[] = [];
function traverse(vnodes: VNode[]) {
vnodes.forEach(vnode => {
result.push(vnode);
if (vnode.children) {
traverse(vnode.children);
}
});
}
traverse(vnodes);
return result;
}
type ChildrenMixinThis = {
disableBindRelation?: boolean;
};
@ -72,10 +56,8 @@ export function ChildrenMixin(
}
const children = [...this.parent.children, this];
const vnodes = flattenVNodes(this.parent.slots());
children.sort(
(a, b) => vnodes.indexOf(a.$vnode) - vnodes.indexOf(b.$vnode)
);
sortChildren(children, this.parent);
this.parent.children = children;
},

25
src/utils/vnodes.ts Normal file
View File

@ -0,0 +1,25 @@
/* eslint-disable no-underscore-dangle */
import { VNode } from 'vue';
function flattenVNodes(vnodes: VNode[]) {
const result: VNode[] = [];
function traverse(vnodes: VNode[]) {
vnodes.forEach(vnode => {
result.push(vnode);
if (vnode.children) {
traverse(vnode.children);
}
});
}
traverse(vnodes);
return result;
}
// sort children instances by vnodes order
export function sortChildren(children: any[], parent: any) {
const vnodes = flattenVNodes(parent._vnode.children);
children.sort((a, b) => vnodes.indexOf(a.$vnode) - vnodes.indexOf(b.$vnode));
}