feat(Form): support setting multile validate-trigger (#10544)

This commit is contained in:
neverland 2022-04-23 11:47:30 +08:00 committed by GitHub
parent c378f4ead1
commit 6baf42fa4a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 55 additions and 13 deletions

View File

@ -16,6 +16,7 @@ import {
isDef,
extend,
addUnit,
toArray,
FORM_KEY,
numericProp,
unknownProp,
@ -239,12 +240,12 @@ export default defineComponent({
const validateWithTrigger = (trigger: FieldValidateTrigger) => {
if (form && props.rules) {
const defaultTrigger = form.props.validateTrigger === trigger;
const { validateTrigger } = form.props;
const defaultTrigger = toArray(validateTrigger).includes(trigger);
const rules = props.rules.filter((rule) => {
if (rule.trigger) {
return rule.trigger === trigger;
return toArray(rule.trigger).includes(trigger);
}
return defaultTrigger;
});

View File

@ -59,7 +59,7 @@ export type FiledRuleFormatter = (value: any, rule: FieldRule) => string;
export type FieldRule = {
pattern?: RegExp;
trigger?: FieldValidateTrigger;
trigger?: FieldValidateTrigger | FieldValidateTrigger[];
message?: FieldRuleMessage;
required?: boolean;
validator?: FieldRuleValidator;

View File

@ -6,7 +6,6 @@ import {
truthProp,
numericProp,
preventDefault,
makeStringProp,
createNamespace,
} from '../utils';
@ -35,9 +34,14 @@ const formProps = {
scrollToError: Boolean,
validateFirst: Boolean,
submitOnEnter: truthProp,
validateTrigger: makeStringProp<FieldValidateTrigger>('onBlur'),
showErrorMessage: truthProp,
errorMessageAlign: String as PropType<FieldTextAlign>,
validateTrigger: {
type: [String, Array] as PropType<
FieldValidateTrigger | FieldValidateTrigger[]
>,
default: 'onBlur',
},
};
export type FormProps = ExtractPropTypes<typeof formProps>;

View File

@ -497,7 +497,7 @@ export default {
| label-align | Field label align, can be set to `center` `right` | _string_ | `left` |
| input-align | Field input align, can be set to `center` `right` | _string_ | `left` |
| error-message-align | Error message align, can be set to `center` `right` | _string_ | `left` |
| validate-trigger | When to validate the form, can be set to `onChange``onSubmit` | _string_ | `onBlur` |
| validate-trigger | When to validate the form, can be set to `onChange``onSubmit`, supports using array to set multiple values | _string \| string[]_ | `onBlur` |
| colon | Whether to display colon after label | _boolean_ | `false` |
| disabled | Whether to disable form | _boolean_ | `false` |
| readonly | Whether to be readonly | _boolean_ | `false` |

View File

@ -529,7 +529,7 @@ export default {
| label-align | 表单项 label 对齐方式,可选值为 `center` `right` | _string_ | `left` |
| input-align | 输入框对齐方式,可选值为 `center` `right` | _string_ | `left` |
| error-message-align | 错误提示文案对齐方式,可选值为 `center` `right` | _string_ | `left` |
| validate-trigger | 表单校验触发时机,可选值为 `onChange``onSubmit`详见下表 | _string_ | `onBlur` |
| validate-trigger | 表单校验触发时机,可选值为 `onChange``onSubmit`支持通过数组同时设置多个值,具体用法见下方表格 | _string \| string[]_ | `onBlur` |
| colon | 是否在 label 后面添加冒号 | _boolean_ | `false` |
| disabled | 是否禁用表单中的所有输入框 | _boolean_ | `false` |
| readonly | 是否将表单中的所有输入框设置为只读状态 | _boolean_ | `false` |

View File

@ -293,6 +293,43 @@ test('should trigger validate after inputting when validate-trigger prop is onCh
expect(wrapper.find('.van-field__error-message').exists).toBeTruthy();
});
test('should trigger validate correctly when validate-trigger prop is array', async () => {
const wrapper = mount({
data() {
return {
...getSimpleRules(),
value: '',
};
},
render() {
return (
<Form ref="form" validateTrigger={['onBlur', 'onChange']}>
<Field
v-model={this.value}
name="A"
rules={this.rulesA}
modelValue=""
/>
</Form>
);
},
});
const input = wrapper.find('input');
await input.trigger('input');
expect(wrapper.find('.van-field__error-message').exists()).toBeFalsy();
await input.trigger('blur');
expect(wrapper.find('.van-field__error-message').exists()).toBeTruthy();
await wrapper.setData({ value: '1' });
expect(wrapper.find('.van-field__error-message').exists()).toBeFalsy();
await wrapper.setData({ value: '' });
expect(wrapper.find('.van-field__error-message').exists).toBeTruthy();
});
test('should allow to custom trigger in rules', async () => {
const rulesA = [
{

View File

@ -11,6 +11,7 @@ import {
import {
pick,
extend,
toArray,
isPromise,
truthProp,
Interceptor,
@ -24,7 +25,6 @@ import {
import {
bem,
name,
toArray,
isOversize,
filterFiles,
isImageFile,

View File

@ -1,4 +1,4 @@
import { createNamespace, isFunction } from '../utils';
import { toArray, createNamespace, isFunction } from '../utils';
import type {
UploaderMaxSize,
UploaderResultType,
@ -9,9 +9,6 @@ const [name, bem, t] = createNamespace('uploader');
export { name, bem, t };
export const toArray = <T>(item: T | T[]): T[] =>
Array.isArray(item) ? item : [item];
export function readFileContent(file: File, resultType: UploaderResultType) {
return new Promise<string | void>((resolve) => {
if (resultType === 'file') {

View File

@ -36,3 +36,6 @@ export function pick<T, U extends keyof T>(
return ret;
}, {} as Writeable<Pick<T, U>>);
}
export const toArray = <T>(item: T | T[]): T[] =>
Array.isArray(item) ? item : [item];