mirror of
https://gitee.com/vant-contrib/vant.git
synced 2025-04-05 19:41:42 +08:00
feat(Field): add start-validate and end-validate event (#10726)
This commit is contained in:
parent
7ca4db9c64
commit
6f94627bf4
@ -128,6 +128,8 @@ export default defineComponent({
|
||||
'clear',
|
||||
'keypress',
|
||||
'click-input',
|
||||
'end-validate',
|
||||
'start-validate',
|
||||
'click-left-icon',
|
||||
'click-right-icon',
|
||||
'update:modelValue',
|
||||
@ -218,19 +220,24 @@ export default defineComponent({
|
||||
state.validateMessage = '';
|
||||
};
|
||||
|
||||
const endValidate = () => emit('end-validate', { status: state.status });
|
||||
|
||||
const validate = (rules = props.rules) =>
|
||||
new Promise<FieldValidateError | void>((resolve) => {
|
||||
resetValidation();
|
||||
if (rules) {
|
||||
emit('start-validate');
|
||||
runRules(rules).then(() => {
|
||||
if (state.status === 'failed') {
|
||||
resolve({
|
||||
name: props.name,
|
||||
message: state.validateMessage,
|
||||
});
|
||||
endValidate();
|
||||
} else {
|
||||
state.status = 'passed';
|
||||
resolve();
|
||||
endValidate();
|
||||
}
|
||||
});
|
||||
} else {
|
||||
|
@ -298,6 +298,8 @@ Use `input-align` prop to align the input value.
|
||||
| click-input | Emitted when the input is clicked | _event: MouseEvent_ |
|
||||
| click-left-icon | Emitted when the left icon is clicked | _event: MouseEvent_ |
|
||||
| click-right-icon | Emitted when the right icon is clicked | _event: MouseEvent_ |
|
||||
| start-validate `v3.5.1` | Emitted when start validation | - |
|
||||
| end-validate `v3.5.1` | Emitted when end validation | _{ status: string }_ |
|
||||
|
||||
### Methods
|
||||
|
||||
|
@ -308,7 +308,7 @@ export default {
|
||||
### Events
|
||||
|
||||
| 事件 | 说明 | 回调参数 |
|
||||
| ------------------ | -------------------- | ------------------------------ |
|
||||
| --- | --- | --- |
|
||||
| update:model-value | 输入框内容变化时触发 | _value: string (当前输入的值)_ |
|
||||
| focus | 输入框获得焦点时触发 | _event: Event_ |
|
||||
| blur | 输入框失去焦点时触发 | _event: Event_ |
|
||||
@ -317,6 +317,8 @@ export default {
|
||||
| click-input | 点击输入区域时触发 | _event: MouseEvent_ |
|
||||
| click-left-icon | 点击左侧图标时触发 | _event: MouseEvent_ |
|
||||
| click-right-icon | 点击右侧图标时触发 | _event: MouseEvent_ |
|
||||
| start-validate `v3.5.1` | 开始表单校验时触发 | - |
|
||||
| end-validate `v3.5.1` | 结束表单校验时触发 | _{ status: string }_ |
|
||||
|
||||
### 方法
|
||||
|
||||
|
@ -1,4 +1,4 @@
|
||||
import { mount } from '../../../test';
|
||||
import { later, mount } from '../../../test';
|
||||
import { submitForm, mountSimpleRulesForm } from './shared';
|
||||
import { Form } from '..';
|
||||
import { Field } from '../../field';
|
||||
@ -64,3 +64,55 @@ test('should emit failed event correctly when rule message is empty', async () =
|
||||
values: { A: '' },
|
||||
});
|
||||
});
|
||||
|
||||
test('Field should emit start-validate event when validation start', async () => {
|
||||
const onStart = jest.fn();
|
||||
const wrapper = mount({
|
||||
render() {
|
||||
return (
|
||||
<Form>
|
||||
<Field
|
||||
name="A"
|
||||
rules={[{ required: true }]}
|
||||
modelValue="bar"
|
||||
onStart-validate={onStart}
|
||||
/>
|
||||
</Form>
|
||||
);
|
||||
},
|
||||
});
|
||||
|
||||
await submitForm(wrapper);
|
||||
expect(onStart).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
test('Field should emit end-validate event when validation end', async () => {
|
||||
const onEnd = jest.fn();
|
||||
const rules = [
|
||||
{
|
||||
validator: () =>
|
||||
new Promise<boolean>((resolve) => {
|
||||
setTimeout(() => resolve(true), 10);
|
||||
}),
|
||||
},
|
||||
];
|
||||
const wrapper = mount({
|
||||
render() {
|
||||
return (
|
||||
<Form>
|
||||
<Field
|
||||
name="A"
|
||||
rules={rules}
|
||||
modelValue="bar"
|
||||
onEnd-validate={onEnd}
|
||||
/>
|
||||
</Form>
|
||||
);
|
||||
},
|
||||
});
|
||||
|
||||
await submitForm(wrapper);
|
||||
expect(onEnd).toHaveBeenCalledTimes(0);
|
||||
await later(50);
|
||||
expect(onEnd).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
Loading…
x
Reference in New Issue
Block a user