Skip to content

Commit 6df470a

Browse files
Simplify form group aria-describedby handling
1 parent ba639d7 commit 6df470a

File tree

1 file changed

+11
-11
lines changed

1 file changed

+11
-11
lines changed

src/components/utils/FormGroup.tsx

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ export const FormGroup = <T extends BaseFormElementRenderProps>(
5858
children,
5959
hint,
6060
label,
61-
id,
6261
legend,
6362
legendProps,
6463
fieldsetProps,
@@ -67,11 +66,11 @@ export const FormGroup = <T extends BaseFormElementRenderProps>(
6766
hintProps,
6867
errorProps,
6968
formGroupProps,
70-
inputType,
69+
id,
7170
name,
72-
'aria-describedby': ariaDescribedBy,
7371
...rest
7472
} = props;
73+
7574
const [generatedID] = useState<string>(generateRandomID(inputType));
7675
const { registerComponent, passError } = useContext<IFormGroupContext>(FormGroupContext);
7776
const { disableErrorFromComponents } = useFormContext();
@@ -85,6 +84,7 @@ export const FormGroup = <T extends BaseFormElementRenderProps>(
8584
const hasError = !disableErrorFromComponents && !!error;
8685

8786
// Build list of IDs for aria-describedby
87+
let { 'aria-describedby': ariaDescribedBy } = rest;
8888
const ariaDescribedByIds = ariaDescribedBy ? [ariaDescribedBy] : [];
8989

9090
// Add optional hint ID
@@ -97,11 +97,14 @@ export const FormGroup = <T extends BaseFormElementRenderProps>(
9797
ariaDescribedByIds.push(errorID);
9898
}
9999

100+
// Update aria-describedby with new IDs
101+
ariaDescribedBy = ariaDescribedByIds.join(' ') || undefined;
102+
100103
const childProps = {
101-
error,
102-
name: name ?? elementID,
103-
id: elementID,
104104
...rest,
105+
'id': elementID,
106+
'name': name ?? elementID,
107+
'aria-describedby': !hasFieldset ? ariaDescribedBy : undefined,
105108
} as FormElementRenderProps<T>;
106109

107110
useEffect(() => {
@@ -124,7 +127,7 @@ export const FormGroup = <T extends BaseFormElementRenderProps>(
124127
)}
125128
>
126129
{hasFieldset ? (
127-
<Fieldset {...fieldsetProps} aria-describedby={ariaDescribedByIds.join(' ') || undefined}>
130+
<Fieldset {...fieldsetProps} aria-describedby={ariaDescribedBy}>
128131
<Legend {...legendProps}>{legend}</Legend>
129132
<HintText id={hintID} {...hintProps}>
130133
{hint}
@@ -145,10 +148,7 @@ export const FormGroup = <T extends BaseFormElementRenderProps>(
145148
<ErrorMessage id={errorID} {...errorProps}>
146149
{error}
147150
</ErrorMessage>
148-
{children({
149-
...childProps,
150-
'aria-describedby': ariaDescribedByIds.join(' ') || undefined,
151-
})}
151+
{children(childProps)}
152152
</>
153153
)}
154154
</div>

0 commit comments

Comments
 (0)