Skip to content

Commit

Permalink
fix(form): event blur not trigger validate
Browse files Browse the repository at this point in the history
  • Loading branch information
liweijie0812 committed Nov 13, 2024
1 parent 39f00de commit c90b2eb
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
10 changes: 10 additions & 0 deletions src/form/form-item.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
nextTick,
onBeforeUnmount,
onMounted,
provide,
reactive,
ref,
toRefs,
Expand Down Expand Up @@ -37,6 +38,7 @@ import {
ErrorListType,
FormInjectionKey,
FormItemContext,
FormItemInjectionKey,
SuccessListType,
ValidateStatus,
} from './const';
Expand Down Expand Up @@ -312,6 +314,14 @@ export default defineComponent({
},
);

const handleBlur = async () => {
await validateHandler('blur');
};

provide(FormItemInjectionKey, {
handleBlur,
});

return () => {
const renderRightIconContent = () => {
if (!props.arrow) {
Expand Down
7 changes: 5 additions & 2 deletions src/input/input.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { PropType, ref, computed, defineComponent, nextTick, watch } from 'vue';
import { PropType, ref, computed, defineComponent, nextTick, watch, inject } from 'vue';
import {
BrowseIcon as TBrowseIcon,
BrowseOffIcon as TBrowseOffIcon,
Expand All @@ -9,6 +9,7 @@ import config from '../config';
import InputProps from './props';
import { InputValue, TdInputProps } from './type';
import { getCharacterLength, useDefault, extendAPI } from '../shared';
import { FormItemInjectionKey } from '../form/const';
import { useFormDisabled } from '../form/hooks';
import { usePrefixClass } from '../hooks/useClass';
import { useTNodeJSX } from '../hooks/tnode';
Expand Down Expand Up @@ -45,6 +46,7 @@ export default defineComponent({
const status = props.status || 'default';
const renderType = ref(props.type);
const focused = ref(false);
const formItem = inject(FormItemInjectionKey, undefined);

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

const handleBlur = (e: FocusEvent) => {
focused.value = false;

// 失焦时处理 format
if (isFunction(props.format)) {
innerValue.value = props.format(innerValue.value);
nextTick(() => {
setInputValue(innerValue.value);
props.onBlur?.(innerValue.value, { e });
formItem.handleBlur();
});
return;
}

props.onBlur?.(innerValue.value, { e });
formItem.handleBlur();

Check failure on line 150 in src/input/input.tsx

View workflow job for this annotation

GitHub Actions / call-test-build / test

Unhandled error

TypeError: Cannot read properties of undefined (reading 'handleBlur') ❯ handleBlur src/input/input.tsx:150:16 ❯ callWithErrorHandling node_modules/@vue/runtime-dom/node_modules/@vue/runtime-core/dist/runtime-core.cjs.js:156:18 ❯ Object.callWithAsyncErrorHandling node_modules/@vue/runtime-dom/node_modules/@vue/runtime-core/dist/runtime-core.cjs.js:164:17 ❯ HTMLInputElement.invoker node_modules/@vue/runtime-dom/dist/runtime-dom.cjs.js:598:17 ❯ HTMLInputElement.callTheUserObjectsOperation node_modules/jsdom/lib/jsdom/living/generated/EventListener.js:26:30 ❯ innerInvokeEventListeners node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:350:25 ❯ invokeEventListeners node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:286:3 ❯ HTMLInputElementImpl._dispatch node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:233:9 ❯ HTMLInputElementImpl.dispatchEvent node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:104:17 ❯ HTMLInputElement.dispatchEvent node_modules/jsdom/lib/jsdom/living/generated/EventTarget.js:241:34 This error originated in "src/input/__test__/index.test.jsx" test file. It doesn't mean the error was thrown inside the file itself, but while it was running. The latest test that might've caused the error is ": focus && blur". It might mean one of the following: - The error was thrown, while Vitest was running this test. - If the error occurred after the test had been completed, this was the last documented test before it was thrown.

Check failure on line 150 in src/input/input.tsx

View workflow job for this annotation

GitHub Actions / call-test-build / test

Unhandled error

TypeError: Cannot read properties of undefined (reading 'handleBlur') ❯ handleBlur src/input/input.tsx:150:16 ❯ callWithErrorHandling node_modules/@vue/runtime-dom/node_modules/@vue/runtime-core/dist/runtime-core.cjs.js:156:18 ❯ Object.callWithAsyncErrorHandling node_modules/@vue/runtime-dom/node_modules/@vue/runtime-core/dist/runtime-core.cjs.js:164:17 ❯ HTMLInputElement.invoker node_modules/@vue/runtime-dom/dist/runtime-dom.cjs.js:598:17 ❯ HTMLInputElement.callTheUserObjectsOperation node_modules/jsdom/lib/jsdom/living/generated/EventListener.js:26:30 ❯ innerInvokeEventListeners node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:350:25 ❯ invokeEventListeners node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:286:3 ❯ HTMLInputElementImpl._dispatch node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:233:9 ❯ HTMLInputElementImpl.dispatchEvent node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:104:17 ❯ HTMLInputElement.dispatchEvent node_modules/jsdom/lib/jsdom/living/generated/EventTarget.js:241:34 This error originated in "src/input/__test__/index.test.jsx" test file. It doesn't mean the error was thrown inside the file itself, but while it was running. The latest test that might've caused the error is ": onBlur". It might mean one of the following: - The error was thrown, while Vitest was running this test. - If the error occurred after the test had been completed, this was the last documented test before it was thrown.

Check failure on line 150 in src/input/input.tsx

View workflow job for this annotation

GitHub Actions / call-test-build / test

Unhandled error

TypeError: Cannot read properties of undefined (reading 'handleBlur') ❯ handleBlur src/input/input.tsx:150:16 ❯ callWithErrorHandling node_modules/@vue/runtime-dom/node_modules/@vue/runtime-core/dist/runtime-core.cjs.js:156:18 ❯ Object.callWithAsyncErrorHandling node_modules/@vue/runtime-dom/node_modules/@vue/runtime-core/dist/runtime-core.cjs.js:164:17 ❯ HTMLInputElement.invoker node_modules/@vue/runtime-dom/dist/runtime-dom.cjs.js:598:17 ❯ HTMLInputElement.callTheUserObjectsOperation node_modules/jsdom/lib/jsdom/living/generated/EventListener.js:26:30 ❯ innerInvokeEventListeners node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:350:25 ❯ invokeEventListeners node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:286:3 ❯ HTMLInputElementImpl._dispatch node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:233:9 ❯ HTMLInputElementImpl.dispatchEvent node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:104:17 ❯ HTMLInputElement.dispatchEvent node_modules/jsdom/lib/jsdom/living/generated/EventTarget.js:241:34 This error originated in "src/input/__test__/index.test.jsx" test file. It doesn't mean the error was thrown inside the file itself, but while it was running. The latest test that might've caused the error is ": focus && blur". It might mean one of the following: - The error was thrown, while Vitest was running this test. - If the error occurred after the test had been completed, this was the last documented test before it was thrown.

Check failure on line 150 in src/input/input.tsx

View workflow job for this annotation

GitHub Actions / call-test-build / test

Unhandled error

TypeError: Cannot read properties of undefined (reading 'handleBlur') ❯ handleBlur src/input/input.tsx:150:16 ❯ callWithErrorHandling node_modules/@vue/runtime-dom/node_modules/@vue/runtime-core/dist/runtime-core.cjs.js:156:18 ❯ Object.callWithAsyncErrorHandling node_modules/@vue/runtime-dom/node_modules/@vue/runtime-core/dist/runtime-core.cjs.js:164:17 ❯ HTMLInputElement.invoker node_modules/@vue/runtime-dom/dist/runtime-dom.cjs.js:598:17 ❯ HTMLInputElement.callTheUserObjectsOperation node_modules/jsdom/lib/jsdom/living/generated/EventListener.js:26:30 ❯ innerInvokeEventListeners node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:350:25 ❯ invokeEventListeners node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:286:3 ❯ HTMLInputElementImpl._dispatch node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:233:9 ❯ HTMLInputElementImpl.dispatchEvent node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:104:17 ❯ HTMLInputElement.dispatchEvent node_modules/jsdom/lib/jsdom/living/generated/EventTarget.js:241:34 This error originated in "src/input/__test__/index.test.jsx" test file. It doesn't mean the error was thrown inside the file itself, but while it was running. The latest test that might've caused the error is ": onBlur". It might mean one of the following: - The error was thrown, while Vitest was running this test. - If the error occurred after the test had been completed, this was the last documented test before it was thrown.
};

const handleCompositionend = (e: CompositionEvent) => {
Expand Down
5 changes: 4 additions & 1 deletion src/textarea/textarea.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { computed, ref, onMounted, defineComponent, toRefs, nextTick, watch } from 'vue';
import { computed, ref, onMounted, defineComponent, toRefs, nextTick, watch, inject } from 'vue';
import { getCharacterLength, useVModel } from '../shared';
import config from '../config';
import props from './props';
import { TextareaValue } from './type';
import calcTextareaHeight from '../_common/js/utils/calcTextareaHeight';
import { FormItemInjectionKey } from '../form/const';
import { useFormDisabled } from '../form/hooks';
import { usePrefixClass } from '../hooks/useClass';
import { useTNodeJSX } from '../hooks/tnode';
Expand All @@ -16,6 +17,7 @@ export default defineComponent({
setup(props, context) {
const renderTNodeJSX = useTNodeJSX();
const isDisabled = useFormDisabled();
const formItem = inject(FormItemInjectionKey, undefined);

const textareaClass = usePrefixClass('textarea');

Expand Down Expand Up @@ -107,6 +109,7 @@ export default defineComponent({
props.onFocus?.(innerValue.value, { e });
};
const handleBlur = (e: FocusEvent) => {
formItem.handleBlur();

Check failure on line 112 in src/textarea/textarea.tsx

View workflow job for this annotation

GitHub Actions / call-test-build / test

Unhandled error

TypeError: Cannot read properties of undefined (reading 'handleBlur') ❯ handleBlur src/textarea/textarea.tsx:112:16 ❯ callWithErrorHandling node_modules/@vue/runtime-dom/node_modules/@vue/runtime-core/dist/runtime-core.cjs.js:156:18 ❯ Object.callWithAsyncErrorHandling node_modules/@vue/runtime-dom/node_modules/@vue/runtime-core/dist/runtime-core.cjs.js:164:17 ❯ HTMLTextAreaElement.invoker node_modules/@vue/runtime-dom/dist/runtime-dom.cjs.js:598:17 ❯ HTMLTextAreaElement.callTheUserObjectsOperation node_modules/jsdom/lib/jsdom/living/generated/EventListener.js:26:30 ❯ innerInvokeEventListeners node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:350:25 ❯ invokeEventListeners node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:286:3 ❯ HTMLTextAreaElementImpl._dispatch node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:233:9 ❯ HTMLTextAreaElementImpl.dispatchEvent node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:104:17 ❯ HTMLTextAreaElement.dispatchEvent node_modules/jsdom/lib/jsdom/living/generated/EventTarget.js:241:34 This error originated in "src/textarea/__test__/index.test.jsx" test file. It doesn't mean the error was thrown inside the file itself, but while it was running. The latest test that might've caused the error is ": onBlur". It might mean one of the following: - The error was thrown, while Vitest was running this test. - If the error occurred after the test had been completed, this was the last documented test before it was thrown.

Check failure on line 112 in src/textarea/textarea.tsx

View workflow job for this annotation

GitHub Actions / call-test-build / test

Unhandled error

TypeError: Cannot read properties of undefined (reading 'handleBlur') ❯ handleBlur src/textarea/textarea.tsx:112:16 ❯ callWithErrorHandling node_modules/@vue/runtime-dom/node_modules/@vue/runtime-core/dist/runtime-core.cjs.js:156:18 ❯ Object.callWithAsyncErrorHandling node_modules/@vue/runtime-dom/node_modules/@vue/runtime-core/dist/runtime-core.cjs.js:164:17 ❯ HTMLTextAreaElement.invoker node_modules/@vue/runtime-dom/dist/runtime-dom.cjs.js:598:17 ❯ HTMLTextAreaElement.callTheUserObjectsOperation node_modules/jsdom/lib/jsdom/living/generated/EventListener.js:26:30 ❯ innerInvokeEventListeners node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:350:25 ❯ invokeEventListeners node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:286:3 ❯ HTMLTextAreaElementImpl._dispatch node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:233:9 ❯ HTMLTextAreaElementImpl.dispatchEvent node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:104:17 ❯ HTMLTextAreaElement.dispatchEvent node_modules/jsdom/lib/jsdom/living/generated/EventTarget.js:241:34 This error originated in "src/textarea/__test__/index.test.jsx" test file. It doesn't mean the error was thrown inside the file itself, but while it was running. The latest test that might've caused the error is ": onBlur". It might mean one of the following: - The error was thrown, while Vitest was running this test. - If the error occurred after the test had been completed, this was the last documented test before it was thrown.
props.onBlur?.(innerValue.value, { e });
};

Expand Down

0 comments on commit c90b2eb

Please sign in to comment.