From aa5e7d5fd55588aaee4e4b85637bbc0862089aab Mon Sep 17 00:00:00 2001 From: Nozomu Ikuta <16436160+NozomuIkuta@users.noreply.github.com> Date: Thu, 4 Jan 2024 04:17:39 +0900 Subject: [PATCH] feat: add `validateOnBlur` option --- packages/vue-v8n/src/composables/useV7d.ts | 5 +-- packages/vue-v8n/src/types.ts | 1 + playground/src/App.vue | 37 +++++++++++----------- 3 files changed, 23 insertions(+), 20 deletions(-) diff --git a/packages/vue-v8n/src/composables/useV7d.ts b/packages/vue-v8n/src/composables/useV7d.ts index 3ccf3c8..746fc2f 100644 --- a/packages/vue-v8n/src/composables/useV7d.ts +++ b/packages/vue-v8n/src/composables/useV7d.ts @@ -5,7 +5,8 @@ import type { RuleDefinition, UseV7dOptions } from '../types' export function useV7d(value: T, rules: MaybeRefOrGetter, options?: UseV7dOptions) { const { immediate = false, - touchOnFocus = true + touchOnFocus = true, + validateOnBlur = true } = options || {} const $el = ref() @@ -31,7 +32,7 @@ export function useV7d(value: T, rules: MaybeRefOrGetter, o _touched.value = true - if (!isAlreadyTouched) { + if (!isAlreadyTouched && validateOnBlur) { $el.value?.removeEventListener('blur', $validate) $el.value?.addEventListener('blur', $validate, { once: true }) } diff --git a/packages/vue-v8n/src/types.ts b/packages/vue-v8n/src/types.ts index 68bbc9a..9f6bda7 100644 --- a/packages/vue-v8n/src/types.ts +++ b/packages/vue-v8n/src/types.ts @@ -15,6 +15,7 @@ export interface RuleContext {} export interface UseV7dOptions { immediate?: boolean touchOnFocus?: boolean + validateOnBlur?: boolean } export interface VueV8nOptions {} diff --git a/playground/src/App.vue b/playground/src/App.vue index ae30ee3..637d437 100644 --- a/playground/src/App.vue +++ b/playground/src/App.vue @@ -30,49 +30,50 @@ watch(password, () => validatedValues.passwordConfirmation = passwordConfirmatio watch(passwordConfirmation.value, () => validatedValues.passwordConfirmation = passwordConfirmation.$validate()) watch(passwordConfirmationRuleOptions, () => validatedValues.passwordConfirmation = passwordConfirmation.$validate(), { deep: true }) -const noTouchedNameRuleOptions = ref([ +const noEventListeningNameRuleOptions = ref([ { label: 'required', rule: required, selected: false }, { label: 'min(5)', rule: min(5), selected: false }, { label: 'max(10)', rule: max(10), selected: false } ]) -const noTouchedName = useV7d('', computed(() => noTouchedNameRuleOptions.value.flatMap(({ selected, rule }) => selected ? [rule] : [])), { - touchOnFocus: false +const noEventListeningName = useV7d('', computed(() => noEventListeningNameRuleOptions.value.flatMap(({ selected, rule }) => selected ? [rule] : [])), { + touchOnFocus: false, + validateOnBlur: false }) -watch(noTouchedNameRuleOptions, () => validatedValues.noTouchedName = noTouchedName.$validate(), { deep: true }) +watch(noEventListeningNameRuleOptions, () => validatedValues.noEventListeningName = noEventListeningName.$validate(), { deep: true }) const state = reactive({ name, count, passwordConfirmation, - noTouchedName + noEventListeningName }) const validatedValues = reactive({} as { name: typeof name.value.value | Error, count: typeof count.value.value | Error, passwordConfirmation: typeof passwordConfirmation.value.value | Error, - noTouchedName: typeof noTouchedName.value.value | Error, + noEventListeningName: typeof noEventListeningName.value.value | Error, }) function validate() { validatedValues.name = name.$validate() validatedValues.count = count.$validate() validatedValues.passwordConfirmation = passwordConfirmation.$validate() - validatedValues.noTouchedName = noTouchedName.$validate() + validatedValues.noEventListeningName = noEventListeningName.$validate() } function touch() { name.$touch() count.$touch() passwordConfirmation.$touch() - noTouchedName.$touch() + noEventListeningName.$touch() } function reset() { name.$reset() count.$reset() passwordConfirmation.$reset() - noTouchedName.$reset() + noEventListeningName.$reset() } onMounted(() => console.log(getCurrentInstance()?.appContext.config.globalProperties.vueV8n)) @@ -160,26 +161,26 @@ onMounted(() => console.log(getCurrentInstance()?.appContext.config.globalProper -
- +
+ -

{{ noTouchedName.$error.value }}

+

{{ noEventListeningName.$error.value }}

Rules:

-
- - - + + +