Skip to content

Commit f29d8a7

Browse files
committed
feat: added validateOn prop to text control
1 parent 73dc880 commit f29d8a7

File tree

3 files changed

+23
-4
lines changed

3 files changed

+23
-4
lines changed

packages/core/src/useTextField/types.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
import { EventExpression } from '../helpers/useEventListener';
12
import { StandardSchema } from '../types';
23
import {
34
AriaDescribableProps,
45
AriaLabelableProps,
56
AriaValidatableProps,
7+
Arrayable,
68
InputEvents,
79
Numberish,
810
TextInputBaseAttributes,
@@ -94,4 +96,9 @@ export interface TextControlProps {
9496
* Whether to disable HTML5 validation.
9597
*/
9698
disableHtmlValidation?: TransparentWrapper<boolean>;
99+
100+
/**
101+
* Events to validate on.
102+
*/
103+
validateOn?: Arrayable<EventExpression>;
97104
}

packages/core/src/useTextField/useTextControl.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { shallowRef, toValue } from 'vue';
22
import { InputEvents, Reactivify } from '../types';
3-
import { propsToValues, useCaptureProps } from '../utils/common';
3+
import { normalizeProps, propsToValues, useCaptureProps } from '../utils/common';
44
import { TextControlProps } from './types';
55
import { useInputValidity } from '../validation';
66
import { FieldState, useFieldStateInjection } from '../useFieldState';
@@ -12,13 +12,19 @@ interface FormControlContext {
1212
state?: FieldState<string | undefined>;
1313
}
1414

15-
export function useTextControl(props: Reactivify<TextControlProps>, ctx?: FormControlContext) {
15+
export function useTextControl(_props: Reactivify<TextControlProps>, ctx?: FormControlContext) {
1616
const inputEl = shallowRef<HTMLInputElement>();
17+
const props = normalizeProps(_props);
1718
const field = ctx?.field ?? useFormFieldInjection();
1819
const state = ctx?.state ?? useFieldStateInjection<string | undefined>();
1920

2021
if (state) {
21-
useInputValidity({ inputEl, field: state, disableHtmlValidation: props.disableHtmlValidation });
22+
useInputValidity({
23+
inputEl,
24+
field: state,
25+
disableHtmlValidation: props.disableHtmlValidation,
26+
events: () => toValue(props.validateOn) ?? ['change', 'blur'],
27+
});
2228
}
2329

2430
if (field) {

packages/playground/src/App.vue

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
<script setup lang="ts">
2+
import { ref } from 'vue';
23
import FormField from './components/FormField.vue';
34
import TextControl from './components/TextControl.vue';
5+
6+
const validationOn = ref(['test']);
47
</script>
58

69
<template>
710
<FormField label="Name">
8-
<TextControl required />
11+
<TextControl required :validate-on="validationOn" />
12+
13+
<button @click="validationOn.push('blur')">Add blur</button>
14+
{{ validationOn }}
915
</FormField>
1016
</template>
1117

0 commit comments

Comments
 (0)