1
1
import { shallowRef , toValue } from 'vue' ;
2
2
import { InputEvents , Reactivify } from '../types' ;
3
- import { normalizeProps , propsToValues , useCaptureProps } from '../utils/common' ;
3
+ import { normalizeProps , propsToValues , useCaptureProps , useUniqId } from '../utils/common' ;
4
4
import { TextControlProps } from './types' ;
5
5
import { useInputValidity } from '../validation' ;
6
6
import { FieldState , useFieldStateInjection } from '../useFieldState' ;
7
7
import { FormField , useFormFieldInjection } from '../useFormField' ;
8
+ import { FieldTypePrefixes } from '../constants' ;
8
9
9
10
interface FormControlContext {
10
- inputId : string ;
11
11
field ?: FormField ;
12
12
state ?: FieldState < string | undefined > ;
13
13
}
14
14
15
15
export function useTextControl ( _props : Reactivify < TextControlProps > , ctx ?: FormControlContext ) {
16
16
const inputEl = shallowRef < HTMLInputElement > ( ) ;
17
+ const inputId = useUniqId ( FieldTypePrefixes . TextField ) ;
17
18
const props = normalizeProps ( _props ) ;
18
19
const field = ctx ?. field ?? useFormFieldInjection ( ) ;
19
20
const state = ctx ?. state ?? useFieldStateInjection < string | undefined > ( ) ;
@@ -30,7 +31,7 @@ export function useTextControl(_props: Reactivify<TextControlProps>, ctx?: FormC
30
31
if ( field ) {
31
32
field . registerControl ( {
32
33
getControlElement : ( ) => inputEl . value ,
33
- getControlId : ( ) => toValue ( ctx ?. inputId ) ,
34
+ getControlId : ( ) => inputId ,
34
35
} ) ;
35
36
}
36
37
@@ -50,7 +51,7 @@ export function useTextControl(_props: Reactivify<TextControlProps>, ctx?: FormC
50
51
return {
51
52
...propsToValues ( props , [ 'name' , 'type' , 'placeholder' , 'autocomplete' , 'required' , 'readonly' ] ) ,
52
53
...handlers ,
53
- id : toValue ( ctx ?. inputId ) ?? undefined ,
54
+ id : inputId ,
54
55
...field ?. accessibleErrorProps . value ,
55
56
...field ?. describedByProps . value ,
56
57
...field ?. labelledByProps . value ,
0 commit comments