diff --git a/packages/vue-v8n/src/composables/useV7d.ts b/packages/vue-v8n/src/composables/useV7d.ts index 7fab4e3..3ccf3c8 100644 --- a/packages/vue-v8n/src/composables/useV7d.ts +++ b/packages/vue-v8n/src/composables/useV7d.ts @@ -4,7 +4,8 @@ import type { RuleDefinition, UseV7dOptions } from '../types' export function useV7d(value: T, rules: MaybeRefOrGetter, options?: UseV7dOptions) { const { - immediate = false + immediate = false, + touchOnFocus = true } = options || {} const $el = ref() @@ -14,10 +15,12 @@ export function useV7d(value: T, rules: MaybeRefOrGetter, o const _error = ref('') const _errors = ref([]) - watch($el, () => { - $el.value?.removeEventListener('focus', $touch) - $el.value?.addEventListener('focus', $touch) - }, { immediate: true }) + if (touchOnFocus) { + watch($el, () => { + $el.value?.removeEventListener('focus', $touch) + $el.value?.addEventListener('focus', $touch) + }, { immediate: true }) + } watch(_value, $validate, { immediate }) diff --git a/packages/vue-v8n/src/types.ts b/packages/vue-v8n/src/types.ts index b61cae1..68bbc9a 100644 --- a/packages/vue-v8n/src/types.ts +++ b/packages/vue-v8n/src/types.ts @@ -14,6 +14,7 @@ export interface RuleContext {} export interface UseV7dOptions { immediate?: boolean + touchOnFocus?: boolean } export interface VueV8nOptions {} diff --git a/playground/src/App.vue b/playground/src/App.vue index 716242b..ae30ee3 100644 --- a/playground/src/App.vue +++ b/playground/src/App.vue @@ -30,33 +30,49 @@ watch(password, () => validatedValues.passwordConfirmation = passwordConfirmatio watch(passwordConfirmation.value, () => validatedValues.passwordConfirmation = passwordConfirmation.$validate()) watch(passwordConfirmationRuleOptions, () => validatedValues.passwordConfirmation = passwordConfirmation.$validate(), { deep: true }) +const noTouchedNameRuleOptions = 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 +}) + +watch(noTouchedNameRuleOptions, () => validatedValues.noTouchedName = noTouchedName.$validate(), { deep: true }) + const state = reactive({ name, count, - passwordConfirmation + passwordConfirmation, + noTouchedName }) 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, }) function validate() { validatedValues.name = name.$validate() validatedValues.count = count.$validate() validatedValues.passwordConfirmation = passwordConfirmation.$validate() + validatedValues.noTouchedName = noTouchedName.$validate() } function touch() { name.$touch() count.$touch() passwordConfirmation.$touch() + noTouchedName.$touch() } function reset() { name.$reset() count.$reset() passwordConfirmation.$reset() + noTouchedName.$reset() } onMounted(() => console.log(getCurrentInstance()?.appContext.config.globalProperties.vueV8n)) @@ -144,6 +160,28 @@ onMounted(() => console.log(getCurrentInstance()?.appContext.config.globalProper +
+ + +

{{ noTouchedName.$error.value }}

+
+

Rules:

+ +
+
+ + + +
+

[State]