Skip to content

Commit c90b2eb

Browse files
committed
fix(form): event blur not trigger validate
1 parent 39f00de commit c90b2eb

File tree

3 files changed

+19
-3
lines changed

3 files changed

+19
-3
lines changed

src/form/form-item.tsx

+10
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
nextTick,
66
onBeforeUnmount,
77
onMounted,
8+
provide,
89
reactive,
910
ref,
1011
toRefs,
@@ -37,6 +38,7 @@ import {
3738
ErrorListType,
3839
FormInjectionKey,
3940
FormItemContext,
41+
FormItemInjectionKey,
4042
SuccessListType,
4143
ValidateStatus,
4244
} from './const';
@@ -312,6 +314,14 @@ export default defineComponent({
312314
},
313315
);
314316

317+
const handleBlur = async () => {
318+
await validateHandler('blur');
319+
};
320+
321+
provide(FormItemInjectionKey, {
322+
handleBlur,
323+
});
324+
315325
return () => {
316326
const renderRightIconContent = () => {
317327
if (!props.arrow) {

src/input/input.tsx

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { PropType, ref, computed, defineComponent, nextTick, watch } from 'vue';
1+
import { PropType, ref, computed, defineComponent, nextTick, watch, inject } from 'vue';
22
import {
33
BrowseIcon as TBrowseIcon,
44
BrowseOffIcon as TBrowseOffIcon,
@@ -9,6 +9,7 @@ import config from '../config';
99
import InputProps from './props';
1010
import { InputValue, TdInputProps } from './type';
1111
import { getCharacterLength, useDefault, extendAPI } from '../shared';
12+
import { FormItemInjectionKey } from '../form/const';
1213
import { useFormDisabled } from '../form/hooks';
1314
import { usePrefixClass } from '../hooks/useClass';
1415
import { useTNodeJSX } from '../hooks/tnode';
@@ -45,6 +46,7 @@ export default defineComponent({
4546
const status = props.status || 'default';
4647
const renderType = ref(props.type);
4748
const focused = ref(false);
49+
const formItem = inject(FormItemInjectionKey, undefined);
4850

4951
const inputClasses = computed(() => [
5052
`${inputClass.value}__control`,
@@ -133,18 +135,19 @@ export default defineComponent({
133135

134136
const handleBlur = (e: FocusEvent) => {
135137
focused.value = false;
136-
137138
// 失焦时处理 format
138139
if (isFunction(props.format)) {
139140
innerValue.value = props.format(innerValue.value);
140141
nextTick(() => {
141142
setInputValue(innerValue.value);
142143
props.onBlur?.(innerValue.value, { e });
144+
formItem.handleBlur();
143145
});
144146
return;
145147
}
146148

147149
props.onBlur?.(innerValue.value, { e });
150+
formItem.handleBlur();
148151
};
149152

150153
const handleCompositionend = (e: CompositionEvent) => {

src/textarea/textarea.tsx

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
import { computed, ref, onMounted, defineComponent, toRefs, nextTick, watch } from 'vue';
1+
import { computed, ref, onMounted, defineComponent, toRefs, nextTick, watch, inject } from 'vue';
22
import { getCharacterLength, useVModel } from '../shared';
33
import config from '../config';
44
import props from './props';
55
import { TextareaValue } from './type';
66
import calcTextareaHeight from '../_common/js/utils/calcTextareaHeight';
7+
import { FormItemInjectionKey } from '../form/const';
78
import { useFormDisabled } from '../form/hooks';
89
import { usePrefixClass } from '../hooks/useClass';
910
import { useTNodeJSX } from '../hooks/tnode';
@@ -16,6 +17,7 @@ export default defineComponent({
1617
setup(props, context) {
1718
const renderTNodeJSX = useTNodeJSX();
1819
const isDisabled = useFormDisabled();
20+
const formItem = inject(FormItemInjectionKey, undefined);
1921

2022
const textareaClass = usePrefixClass('textarea');
2123

@@ -107,6 +109,7 @@ export default defineComponent({
107109
props.onFocus?.(innerValue.value, { e });
108110
};
109111
const handleBlur = (e: FocusEvent) => {
112+
formItem.handleBlur();
110113
props.onBlur?.(innerValue.value, { e });
111114
};
112115

0 commit comments

Comments
 (0)