Skip to content

Commit 00e2d4c

Browse files
author
v_sshuairen
committed
fix(form): add success validation type to setValidateMessage
1 parent 8446389 commit 00e2d4c

File tree

5 files changed

+57
-42
lines changed

5 files changed

+57
-42
lines changed

packages/components/common.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,6 @@ export interface UploadDisplayDragEvents {
4141

4242
export type ImageEvent = Event;
4343

44-
/**
45-
* 通用全局类型
46-
* */
4744
export type PlainObject = { [key: string]: any };
4845

4946
export type OptionData = {
@@ -63,6 +60,9 @@ export type TreeOptionData<T = string | number> = {
6360
content?: string | TNode;
6461
} & PlainObject;
6562

63+
/**
64+
* 通用全局类型
65+
* */
6666
export type SizeEnum = 'small' | 'medium' | 'large';
6767

6868
export type ShapeEnum = 'circle' | 'round';

packages/components/form/form-item.tsx

Lines changed: 50 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
import {
2+
cloneDeep,
3+
isArray,
4+
isBoolean,
5+
isNil,
6+
isNumber,
7+
isString,
8+
get as lodashGet,
9+
set as lodashSet,
10+
} from 'lodash-es';
11+
import {
12+
GlobalIconType,
13+
CheckCircleFilledIcon as TdCheckCircleFilledIcon,
14+
CloseCircleFilledIcon as TdCloseCircleFilledIcon,
15+
ErrorCircleFilledIcon as TdErrorCircleFilledIcon,
16+
} from 'tdesign-icons-vue-next';
117
import {
218
computed,
319
defineComponent,
@@ -12,34 +28,7 @@ import {
1228
VNode,
1329
watch,
1430
} from 'vue';
15-
import {
16-
CheckCircleFilledIcon as TdCheckCircleFilledIcon,
17-
CloseCircleFilledIcon as TdCloseCircleFilledIcon,
18-
ErrorCircleFilledIcon as TdErrorCircleFilledIcon,
19-
GlobalIconType,
20-
} from 'tdesign-icons-vue-next';
21-
import {
22-
isNil,
23-
isArray,
24-
isNumber,
25-
isString,
26-
isBoolean,
27-
cloneDeep,
28-
get as lodashGet,
29-
set as lodashSet,
30-
} from 'lodash-es';
3131

32-
import { validate } from './utils/form-model';
33-
import {
34-
AllValidateResult,
35-
Data,
36-
FormErrorMessage,
37-
FormItemValidateMessage,
38-
FormRule,
39-
ValidateTriggerType,
40-
ValueType,
41-
} from './type';
42-
import props from './form-item-props';
4332
import {
4433
AnalysisValidateResult,
4534
ErrorListType,
@@ -50,10 +39,21 @@ import {
5039
useCLASSNAMES,
5140
ValidateStatus,
5241
} from './consts';
42+
import props from './form-item-props';
43+
import {
44+
AllValidateResult,
45+
Data,
46+
FormErrorMessage,
47+
FormItemValidateMessage,
48+
FormRule,
49+
ValidateTriggerType,
50+
ValueType,
51+
} from './type';
52+
import { validate } from './utils/form-model';
5353

54-
import { useConfig, useTNodeJSX, useGlobalIcon, usePrefixClass } from '@tdesign/shared-hooks';
55-
import { getFormItemClassName } from './utils';
5654
import { template } from '@tdesign/common-js/utils/stringTemplate';
55+
import { useConfig, useGlobalIcon, usePrefixClass, useTNodeJSX } from '@tdesign/shared-hooks';
56+
import { getFormItemClassName } from './utils';
5757

5858
export type FormItemValidateResult<T extends Data = Data> = { [key in keyof T]: boolean | AllValidateResult[] };
5959

@@ -164,7 +164,7 @@ export default defineComponent({
164164
/** Suffix Icon END */
165165

166166
/** Content Style */
167-
const errorClasses = computed(() => {
167+
const statusClasses = computed(() => {
168168
if (!showErrorMessage.value) return '';
169169
if (verifyStatus.value === ValidateStatus.SUCCESS) {
170170
return props.successBorder
@@ -176,7 +176,7 @@ export default defineComponent({
176176
if (props.status) return statusClass.value;
177177
return type === 'error' ? CLASS_NAMES.value.error : CLASS_NAMES.value.warning;
178178
});
179-
const contentClasses = computed(() => [CLASS_NAMES.value.controls, errorClasses.value]);
179+
const contentClasses = computed(() => [CLASS_NAMES.value.controls, statusClasses.value]);
180180
const contentStyle = computed(() => {
181181
let contentStyle = {};
182182
if (labelWidth.value && labelAlign.value !== 'top') {
@@ -322,13 +322,28 @@ export default defineComponent({
322322
};
323323

324324
const setValidateMessage = (validateMessage: FormItemValidateMessage[]) => {
325-
if (!validateMessage && !isArray(validateMessage)) return;
325+
if (!validateMessage || !isArray(validateMessage)) return;
326+
errorList.value = [];
327+
successList.value = [];
326328
if (validateMessage.length === 0) {
327-
errorList.value = [];
328329
verifyStatus.value = ValidateStatus.SUCCESS;
330+
return;
329331
}
330-
errorList.value = validateMessage.map((item) => ({ ...item, result: false }));
331-
verifyStatus.value = ValidateStatus.FAIL;
332+
const errors = validateMessage.filter(
333+
(item) => item.type === 'error' || item.type === 'warning',
334+
) as FormItemValidateMessage[];
335+
const successes = validateMessage.filter((item) => item.type === 'success') as FormItemValidateMessage[];
336+
errorList.value = errors.map((item) => ({
337+
message: item.message,
338+
type: item.type as 'error' | 'warning',
339+
result: false,
340+
}));
341+
successList.value = successes.map((item) => ({
342+
message: item.message,
343+
type: item.type as 'success',
344+
result: true,
345+
}));
346+
verifyStatus.value = errors.length > 0 ? ValidateStatus.FAIL : ValidateStatus.SUCCESS;
332347
};
333348

334349
const value = computed<ValueType>(() => form?.data && lodashGet(form?.data, props.name));

packages/components/form/form.en-US.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ name | params | return | description
4242
-- | -- | -- | --
4343
clearValidate | `(fields?: Array<keyof FormData>)` | \- | required
4444
reset | `(params?: FormResetParams<FormData>)` | \- | required。[see more ts definition](https://github.com/Tencent/tdesign-vue-next/blob/develop/packages/components/form/type.ts)。<br/>`interface FormResetParams<FormData> { type?: 'initial' \| 'empty'; fields?: Array<keyof FormData> }`<br/>
45-
setValidateMessage | `(message: FormValidateMessage<FormData>)` | \- | required。[see more ts definition](https://github.com/Tencent/tdesign-vue-next/blob/develop/packages/components/form/type.ts)。<br/>`type FormValidateMessage<FormData> = { [field in keyof FormData]: FormItemValidateMessage[] }`<br/><br/>`interface FormItemValidateMessage { type: 'warning' \| 'error'; message: string }`<br/>
45+
setValidateMessage | `(message: FormValidateMessage<FormData>)` | \- | required。Sets custom validation results, such as directly displaying remote validation messages. Note: This method must be called after the component is mounted. `FormData` refers to the generic type of form data。[see more ts definition](https://github.com/Tencent/tdesign-vue-next/blob/develop/packages/components/form/type.ts)。<br/>`type FormValidateMessage<FormData> = { [field in keyof FormData]: FormItemValidateMessage[] }`<br/><br/>`interface FormItemValidateMessage { type: 'warning' \| 'error' \| 'success'; message: string }`<br/>
4646
submit | `(params?: { showErrorMessage?: boolean })` | \- | required
4747
validate | `(params?: FormValidateParams)` | `Promise<FormValidateResult<FormData>>` | required。[see more ts definition](https://github.com/Tencent/tdesign-vue-next/blob/develop/packages/components/form/type.ts)。<br/>`interface FormValidateParams { fields?: Array<string>; showErrorMessage?: boolean; trigger?: ValidateTriggerType }`<br/><br/>`type ValidateTriggerType = 'blur' \| 'change' \| 'submit' \| 'all'`<br/>
4848
validateOnly | `(params?: Pick<FormValidateParams, 'fields' \| 'trigger'>)` | `Promise<FormValidateResult<FormData>>` | required

packages/components/form/form.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ validate | `(result: ValidateResultContext<FormData>)` | 校验结束后触发
4444
-- | -- | -- | --
4545
clearValidate | `(fields?: Array<keyof FormData>)` | \- | 必需。清空校验结果。可使用 fields 指定清除部分字段的校验结果,fields 值为空则表示清除所有字段校验结果。清除邮箱校验结果示例:`clearValidate(['email'])`
4646
reset | `(params?: FormResetParams<FormData>)` | \- | 必需。重置表单,表单里面没有重置按钮`<button type=\"reset\" />`时可以使用该方法,默认重置全部字段为空,该方法会触发 `reset` 事件。<br />如果表单属性 `resetType='empty'` 或者 `reset.type='empty'` 会重置为空;<br />如果表单属性 `resetType='initial'` 或者 `reset.type='initial'` 会重置为表单初始值。<br />`reset.fields` 用于设置具体重置哪些字段,示例:`reset({ type: 'initial', fields: ['name', 'age'] })`[详细类型定义](https://github.com/Tencent/tdesign-vue-next/blob/develop/packages/components/form/type.ts)。<br/>`interface FormResetParams<FormData> { type?: 'initial' \| 'empty'; fields?: Array<keyof FormData> }`<br/>
47-
setValidateMessage | `(message: FormValidateMessage<FormData>)` | \- | 必需。设置自定义校验结果,如远程校验信息直接呈现。注意需要在组件挂载结束后使用该方法。`FormData` 指表单数据泛型。[详细类型定义](https://github.com/Tencent/tdesign-vue-next/blob/develop/packages/components/form/type.ts)。<br/>`type FormValidateMessage<FormData> = { [field in keyof FormData]: FormItemValidateMessage[] }`<br/><br/>`interface FormItemValidateMessage { type: 'warning' \| 'error'; message: string }`<br/>
47+
setValidateMessage | `(message: FormValidateMessage<FormData>)` | \- | 必需。设置自定义校验结果,如远程校验信息直接呈现。注意需要在组件挂载结束后使用该方法。`FormData` 指表单数据泛型。[详细类型定义](https://github.com/Tencent/tdesign-vue-next/blob/develop/packages/components/form/type.ts)。<br/>`type FormValidateMessage<FormData> = { [field in keyof FormData]: FormItemValidateMessage[] }`<br/><br/>`interface FormItemValidateMessage { type: 'warning' \| 'error' \| 'success'; message: string }`<br/>
4848
submit | `(params?: { showErrorMessage?: boolean })` | \- | 必需。提交表单,表单里面没有提交按钮`<button type=\"submit\" />`时可以使用该方法。`showErrorMessage` 表示是否在提交校验不通过时显示校验不通过的原因,默认显示。该方法会触发 `submit` 事件
4949
validate | `(params?: FormValidateParams)` | `Promise<FormValidateResult<FormData>>` | 必需。校验函数,包含错误文本提示等功能。泛型 `FormData` 表示表单数据 TS 类型。<br/>【关于参数】`params.fields` 表示校验字段,如果设置了 `fields`,本次校验将仅对这些字段进行校验。`params.trigger` 表示本次触发校验的范围,'params.trigger = blur' 表示只触发校验规则设定为 trigger='blur' 的字段,'params.trigger = change' 表示只触发校验规则设定为 trigger='change' 的字段,默认触发全范围校验。`params.showErrorMessage` 表示校验结束后是否显示错误文本提示,默认显示。<br />【关于返回值】返回值为 true 表示校验通过;如果校验不通过,返回值为校验结果列表。[详细类型定义](https://github.com/Tencent/tdesign-vue-next/blob/develop/packages/components/form/type.ts)。<br/>`interface FormValidateParams { fields?: Array<string>; showErrorMessage?: boolean; trigger?: ValidateTriggerType }`<br/><br/>`type ValidateTriggerType = 'blur' \| 'change' \| 'submit' \| 'all'`<br/>
5050
validateOnly | `(params?: Pick<FormValidateParams, 'fields' \| 'trigger'>)` | `Promise<FormValidateResult<FormData>>` | 必需。纯净的校验函数,仅返回校验结果,不对组件进行任何操作。泛型 `FormData` 表示表单数据 TS 类型。参数和返回值含义同 `validate` 方法

packages/components/form/type.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
import { IsEmailOptions } from 'validator/es/lib/isEmail';
88
import { IsURLOptions } from 'validator/es/lib/isURL';
9-
import { TNode, FormResetEvent, FormSubmitEvent } from '../common';
9+
import { FormResetEvent, FormSubmitEvent, TNode } from '../common';
1010

1111
export interface TdFormProps<FormData extends Data = Data> {
1212
/**
@@ -378,7 +378,7 @@ export interface FormResetParams<FormData> {
378378
export type FormValidateMessage<FormData> = { [field in keyof FormData]: FormItemValidateMessage[] };
379379

380380
export interface FormItemValidateMessage {
381-
type: 'warning' | 'error';
381+
type: 'warning' | 'error' | 'success';
382382
message: string;
383383
}
384384

0 commit comments

Comments
 (0)