mirror of
https://gitee.com/vant-contrib/vant.git
synced 2025-08-21 19:39:45 +08:00
fix(Form): incorrect validation order when add field dynamically
This commit is contained in:
parent
02e67fdd47
commit
b8dea3c13b
@ -80,13 +80,13 @@ export default createComponent({
|
|||||||
this.$nextTick(this.adjustSize);
|
this.$nextTick(this.adjustSize);
|
||||||
|
|
||||||
if (this.vanForm) {
|
if (this.vanForm) {
|
||||||
this.vanForm.fields.push(this);
|
this.vanForm.addField(this);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
beforeDestroy() {
|
beforeDestroy() {
|
||||||
if (this.vanForm) {
|
if (this.vanForm) {
|
||||||
this.vanForm.fields = this.vanForm.fields.filter(item => item !== this);
|
this.vanForm.removeField(this);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
import { createNamespace } from '../utils';
|
import { createNamespace } from '../utils';
|
||||||
|
import { sortChildren } from '../utils/vnodes';
|
||||||
|
|
||||||
const [createComponent, bem] = createNamespace('form');
|
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() {
|
getValues() {
|
||||||
return this.fields.reduce((form, field) => {
|
return this.fields.reduce((form, field) => {
|
||||||
form[field.name] = field.formValue;
|
form[field.name] = field.formValue;
|
||||||
|
@ -35,3 +35,41 @@ test('dynamic add/remove fileds', async () => {
|
|||||||
await submitForm(wrapper);
|
await submitForm(wrapper);
|
||||||
expect(onSubmit).toHaveBeenCalledWith({ B: '' });
|
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');
|
||||||
|
});
|
||||||
|
@ -1,26 +1,10 @@
|
|||||||
import Vue, { VNode } from 'vue';
|
import Vue from 'vue';
|
||||||
|
import { sortChildren } from '../utils/vnodes';
|
||||||
|
|
||||||
type ChildrenMixinOptions = {
|
type ChildrenMixinOptions = {
|
||||||
indexKey?: any;
|
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 = {
|
type ChildrenMixinThis = {
|
||||||
disableBindRelation?: boolean;
|
disableBindRelation?: boolean;
|
||||||
};
|
};
|
||||||
@ -72,10 +56,8 @@ export function ChildrenMixin(
|
|||||||
}
|
}
|
||||||
|
|
||||||
const children = [...this.parent.children, this];
|
const children = [...this.parent.children, this];
|
||||||
const vnodes = flattenVNodes(this.parent.slots());
|
|
||||||
children.sort(
|
sortChildren(children, this.parent);
|
||||||
(a, b) => vnodes.indexOf(a.$vnode) - vnodes.indexOf(b.$vnode)
|
|
||||||
);
|
|
||||||
|
|
||||||
this.parent.children = children;
|
this.parent.children = children;
|
||||||
},
|
},
|
||||||
|
25
src/utils/vnodes.ts
Normal file
25
src/utils/vnodes.ts
Normal 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));
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user