Skip to content

Commit 95d0361

Browse files
committed
refactor: generate uniq id on the control level
1 parent f29d8a7 commit 95d0361

File tree

2 files changed

+7
-10
lines changed

2 files changed

+7
-10
lines changed

packages/core/src/useTextField/useTextControl.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
11
import { shallowRef, toValue } from 'vue';
22
import { InputEvents, Reactivify } from '../types';
3-
import { normalizeProps, propsToValues, useCaptureProps } from '../utils/common';
3+
import { normalizeProps, propsToValues, useCaptureProps, useUniqId } from '../utils/common';
44
import { TextControlProps } from './types';
55
import { useInputValidity } from '../validation';
66
import { FieldState, useFieldStateInjection } from '../useFieldState';
77
import { FormField, useFormFieldInjection } from '../useFormField';
8+
import { FieldTypePrefixes } from '../constants';
89

910
interface FormControlContext {
10-
inputId: string;
1111
field?: FormField;
1212
state?: FieldState<string | undefined>;
1313
}
1414

1515
export function useTextControl(_props: Reactivify<TextControlProps>, ctx?: FormControlContext) {
1616
const inputEl = shallowRef<HTMLInputElement>();
17+
const inputId = useUniqId(FieldTypePrefixes.TextField);
1718
const props = normalizeProps(_props);
1819
const field = ctx?.field ?? useFormFieldInjection();
1920
const state = ctx?.state ?? useFieldStateInjection<string | undefined>();
@@ -30,7 +31,7 @@ export function useTextControl(_props: Reactivify<TextControlProps>, ctx?: FormC
3031
if (field) {
3132
field.registerControl({
3233
getControlElement: () => inputEl.value,
33-
getControlId: () => toValue(ctx?.inputId),
34+
getControlId: () => inputId,
3435
});
3536
}
3637

@@ -50,7 +51,7 @@ export function useTextControl(_props: Reactivify<TextControlProps>, ctx?: FormC
5051
return {
5152
...propsToValues(props, ['name', 'type', 'placeholder', 'autocomplete', 'required', 'readonly']),
5253
...handlers,
53-
id: toValue(ctx?.inputId) ?? undefined,
54+
id: inputId,
5455
...field?.accessibleErrorProps.value,
5556
...field?.describedByProps.value,
5657
...field?.labelledByProps.value,

packages/core/src/useTextField/useTextField.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
import { toValue } from 'vue';
22
import { registerField } from '@formwerk/devtools';
3-
import { normalizeProps, useUniqId } from '../utils/common';
3+
import { normalizeProps } from '../utils/common';
44
import { Reactivify } from '../types/common';
55
import { useFieldState, exposeField } from '../useFieldState';
66
import { TextControlProps } from './types';
77
import { useTextControl } from './useTextControl';
8-
import { FieldTypePrefixes } from '../constants';
98
import { useFormField } from '../useFormField';
109

1110
export interface TextFieldProps extends TextControlProps {
@@ -21,7 +20,6 @@ export interface TextFieldProps extends TextControlProps {
2120

2221
export function useTextField(_props: Reactivify<TextFieldProps, 'schema'>) {
2322
const props = normalizeProps(_props, ['schema']);
24-
2523
const state = useFieldState<string | undefined>({
2624
path: props.name,
2725
initialValue: toValue(props.modelValue) ?? toValue(props.value),
@@ -37,9 +35,7 @@ export function useTextField(_props: Reactivify<TextFieldProps, 'schema'>) {
3735
state,
3836
);
3937

40-
const inputId = useUniqId(FieldTypePrefixes.TextField);
41-
42-
const { inputEl, inputProps } = useTextControl(props, { state, field, inputId });
38+
const { inputEl, inputProps } = useTextControl(props, { state, field });
4339

4440
if (__DEV__) {
4541
registerField(state, 'Text');

0 commit comments

Comments
 (0)