1- import { computed , watch } from 'vue' ;
2- import { hasValue , isArray , isObject } from '../core/utils/helpers' ;
1+ import { computed , ComputedRef , watch } from 'vue' ;
2+ import { hasValue } from '../core/utils/helpers' ;
33
44import { useInputValidation } from '@/composables/use-validation' ;
5- import { BindingObject } from '.. ' ;
5+ import { ValidationTriggerTypes } from '@/core/models ' ;
66
7- export function useInputEvents ( props : any , emit : any ) {
8- const { validate } = useInputValidation ( props , emit ) ;
7+ interface InputEventsComposition {
8+ onInput : ( $event : Event ) => void ;
9+ onCheck : ( $event : Event ) => void ;
10+ onFocus : ( ) => void ;
11+ onBlur : ( ) => void ;
12+ getClasses : ComputedRef < ( string | { [ key : string ] : boolean } ) [ ] > ;
13+ }
14+
15+ export function useInputEvents ( props : any , emit : any ) : InputEventsComposition {
16+ const { validate, getValidationClasses } = useInputValidation ( props , emit ) ;
17+
18+ function onInput ( $event : Event ) : void {
19+ const element = $event . target as HTMLInputElement ;
920
10- function onChange ( $event ) : void {
11- if ( props . control && hasValue ( $event . target . value ) ) {
21+ if ( props . control && hasValue ( element . value ) ) {
1222 $event . stopImmediatePropagation ( ) ;
1323
14- validate ( ) ;
24+ if (
25+ ( ! props . control . valid &&
26+ props . control . validationTrigger . type ===
27+ ValidationTriggerTypes . BLUR ) ||
28+ ( props . control . validationTrigger . type ===
29+ ValidationTriggerTypes . CHANGE &&
30+ element . value . length >= props . control . validationTrigger . threshold )
31+ ) {
32+ validate ( ) ;
33+ }
1534 emit ( 'change' , {
1635 name : props . control . name ,
17- value : $event . target . value ,
36+ value : element . value ,
1837 } ) ;
1938 }
2039 }
21- function onCheck ( $event ) : void {
40+ function onCheck ( $event : Event ) : void {
41+ const element = $event . target as HTMLInputElement ;
2242 if ( props . control ) {
2343 $event . stopImmediatePropagation ( ) ;
2444
2545 emit ( 'change' , {
2646 name : props . control . name ,
27- value : $event . target . checked ,
47+ value : element . checked ,
2848 } ) ;
2949 }
3050 }
@@ -33,18 +53,16 @@ export function useInputEvents(props: any, emit: any) {
3353 }
3454 function onBlur ( ) : void {
3555 emit ( 'blur' , { name : props . control . name } ) ;
36- validate ( ) ;
37- }
3856
39- const getClasses = computed ( ( ) => {
40- const classes = [ 'form-control' ] ;
41- if ( isArray ( props . control . customClass ) ) {
42- return [ ...classes , ...( props . control . customClass as BindingObject [ ] ) ] ;
43- }
44- if ( isObject ( props . control . customClass ) ) {
45- return [ ...classes , props . control . customClass ] ;
57+ if ( props . control . validationTrigger . type === ValidationTriggerTypes . BLUR ) {
58+ validate ( ) ;
4659 }
47- return [ classes , props . control . customClass ] ;
60+ }
61+
62+ const getClasses : ComputedRef <
63+ ( string | { [ key : string ] : boolean } ) [ ]
64+ > = computed ( ( ) => {
65+ return [ 'form-control' , ...getValidationClasses . value ] ;
4866 } ) ;
4967
5068 watch (
@@ -61,7 +79,7 @@ export function useInputEvents(props: any, emit: any) {
6179
6280 return {
6381 onFocus,
64- onChange ,
82+ onInput ,
6583 onBlur,
6684 onCheck,
6785 getClasses,
0 commit comments