From ee6d489bf479cd7e7f1048a5116d7bcfe129fac1 Mon Sep 17 00:00:00 2001 From: tangjinzhou <415800467@qq.com> Date: Thu, 17 Mar 2022 21:53:14 +0800 Subject: [PATCH] fix: formItem error repeat show, close #5349 --- components/form/FormItem.tsx | 29 +++++++++++++++++++--------- components/form/FormItemInput.tsx | 5 ++--- components/form/utils/useDebounce.ts | 18 +++++++++++++++++ 3 files changed, 40 insertions(+), 12 deletions(-) create mode 100644 components/form/utils/useDebounce.ts diff --git a/components/form/FormItem.tsx b/components/form/FormItem.tsx index a0d673ba6..9ba4d4310 100644 --- a/components/form/FormItem.tsx +++ b/components/form/FormItem.tsx @@ -34,6 +34,7 @@ import FormItemLabel from './FormItemLabel'; import FormItemInput from './FormItemInput'; import type { ValidationRule } from './Form'; import { useProvideFormItemContext } from './FormItemContext'; +import useDebounce from './utils/useDebounce'; const ValidateStatuses = tuple('success', 'warning', 'error', 'validating', ''); export type ValidateStatus = typeof ValidateStatuses[number]; @@ -368,15 +369,24 @@ export default defineComponent({ onBeforeUnmount(() => { formContext.removeField(eventKey); }); + const debounceErrors = useDebounce(errors); + const mergedValidateStatus = computed(() => { + if (props.validateStatus !== undefined) { + return props.validateStatus; + } else if (debounceErrors.value.length) { + return 'error'; + } + return validateState.value; + }); const itemClassName = computed(() => ({ [`${prefixCls.value}-item`]: true, // Status - [`${prefixCls.value}-item-has-feedback`]: validateState.value && props.hasFeedback, - [`${prefixCls.value}-item-has-success`]: validateState.value === 'success', - [`${prefixCls.value}-item-has-warning`]: validateState.value === 'warning', - [`${prefixCls.value}-item-has-error`]: validateState.value === 'error', - [`${prefixCls.value}-item-is-validating`]: validateState.value === 'validating', + [`${prefixCls.value}-item-has-feedback`]: mergedValidateStatus.value && props.hasFeedback, + [`${prefixCls.value}-item-has-success`]: mergedValidateStatus.value === 'success', + [`${prefixCls.value}-item-has-warning`]: mergedValidateStatus.value === 'warning', + [`${prefixCls.value}-item-has-error`]: mergedValidateStatus.value === 'error', + [`${prefixCls.value}-item-is-validating`]: mergedValidateStatus.value === 'validating', [`${prefixCls.value}-item-hidden`]: props.hidden, })); return () => { @@ -387,7 +397,7 @@ export default defineComponent({ {...attrs} class={[ itemClassName.value, - (help !== undefined && help !== null) || errors.value.length + (help !== undefined && help !== null) || debounceErrors.value.length ? `${prefixCls.value}-item-with-help` : '', attrs.class, @@ -409,10 +419,11 @@ export default defineComponent({ {/* Input Group */} (value: Ref): Ref { + const cacheValue = shallowRef(value.value.slice()); + let timeout: any = null; + watchEffect(() => { + clearTimeout(timeout); + timeout = setTimeout( + () => { + cacheValue.value = value.value; + }, + value.value.length ? 0 : 10, + ); + }); + + return cacheValue; +}