|
@@ -1,4 +1,4 @@
|
|
|
-import { cloneElement } from 'react';
|
|
|
|
|
|
|
+import { cloneElement, useId } from 'react';
|
|
|
import type { CSSProperties, ReactElement, ReactNode } from 'react';
|
|
import type { CSSProperties, ReactElement, ReactNode } from 'react';
|
|
|
import { Controller } from 'react-hook-form';
|
|
import { Controller } from 'react-hook-form';
|
|
|
import type { Control, ControllerProps, FieldValues, Path } from 'react-hook-form';
|
|
import type { Control, ControllerProps, FieldValues, Path } from 'react-hook-form';
|
|
@@ -48,6 +48,9 @@ export function FormField<T extends FieldValues = FieldValues>({
|
|
|
}: FormFieldProps<T>) {
|
|
}: FormFieldProps<T>) {
|
|
|
const { t } = useTranslation();
|
|
const { t } = useTranslation();
|
|
|
const dottedName = toDotted(name) as Path<T>;
|
|
const dottedName = toDotted(name) as Path<T>;
|
|
|
|
|
+ const generatedId = useId();
|
|
|
|
|
+ const explicitId = (children as ReactElement<{ id?: string }>).props.id;
|
|
|
|
|
+ const fieldId = explicitId ?? generatedId;
|
|
|
|
|
|
|
|
return (
|
|
return (
|
|
|
<Controller
|
|
<Controller
|
|
@@ -60,6 +63,7 @@ export function FormField<T extends FieldValues = FieldValues>({
|
|
|
? t(fieldState.error.message, { defaultValue: fieldState.error.message })
|
|
? t(fieldState.error.message, { defaultValue: fieldState.error.message })
|
|
|
: undefined;
|
|
: undefined;
|
|
|
const childProps: Record<string, unknown> = {
|
|
const childProps: Record<string, unknown> = {
|
|
|
|
|
+ id: fieldId,
|
|
|
[valueProp]: displayValue,
|
|
[valueProp]: displayValue,
|
|
|
onChange: (...args: unknown[]) => {
|
|
onChange: (...args: unknown[]) => {
|
|
|
const raw = normalizeAntdOnChange(args, valueProp);
|
|
const raw = normalizeAntdOnChange(args, valueProp);
|
|
@@ -73,6 +77,7 @@ export function FormField<T extends FieldValues = FieldValues>({
|
|
|
return (
|
|
return (
|
|
|
<Form.Item
|
|
<Form.Item
|
|
|
label={label}
|
|
label={label}
|
|
|
|
|
+ htmlFor={label ? fieldId : undefined}
|
|
|
tooltip={tooltip}
|
|
tooltip={tooltip}
|
|
|
extra={extra}
|
|
extra={extra}
|
|
|
required={required}
|
|
required={required}
|