Skip to content

Commit 0eaeedf

Browse files
committed
improve description of preventAriaAttribution and set own label click handler only if it is not true
1 parent 071183a commit 0eaeedf

2 files changed

Lines changed: 7 additions & 4 deletions

File tree

‎src/components/Form/FieldItem.tsx‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ export interface FieldItemProps extends React.HTMLAttributes<HTMLDivElement>, Te
3333
/**
3434
* Prevent the automatic connection of the field item parts.
3535
* By default, label, input element, helper text and message are connected to each
36-
* other via `for`, `aria-labelledby` and `aria-describedby`.
36+
* other via `for`, `aria-labelledby` and `aria-describedby`, and a click on the label focuses
37+
* input elements that cannot be connected natively via `for`.
3738
* Set it to `true` if the using application manages the accessibility attributes itself.
3839
*/
3940
preventAriaAttribution?: boolean;
@@ -237,7 +238,7 @@ export const FieldItem = ({
237238

238239
const handleLabelClick: React.MouseEventHandler<HTMLLabelElement> = (event) => {
239240
labelProps?.onClick?.(event);
240-
if (event.defaultPrevented || disabled || preventAriaAttribution) {
241+
if (event.defaultPrevented || disabled) {
241242
return;
242243
}
243244
const labelElement = event.currentTarget;
@@ -265,7 +266,7 @@ export const FieldItem = ({
265266
const label = (
266267
<Label
267268
{...labelProps}
268-
onClick={handleLabelClick}
269+
onClick={preventAriaAttribution ? labelProps?.onClick : handleLabelClick}
269270
className={`${eccgui}-fielditem__label` + (labelProps?.className ? " " + labelProps.className : "")}
270271
disabled={disabled}
271272
/>

‎src/components/Form/tests/FieldItem.test.tsx‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -668,11 +668,13 @@ describe("FieldItem label activation", () => {
668668

669669
it("does not add focus behavior when automatic connections are disabled", async () => {
670670
const user = userEvent.setup();
671-
renderCombobox({ preventAriaAttribution: true });
671+
const onClick = jest.fn();
672+
renderCombobox({ preventAriaAttribution: true, labelProps: { text: "Type", onClick } });
672673
const focusControl = jest.spyOn(screen.getByRole("combobox"), "focus");
673674

674675
await user.click(screen.getByText("Type"));
675676

677+
expect(onClick).toHaveBeenCalledTimes(1);
676678
expect(focusControl).not.toHaveBeenCalled();
677679
});
678680
});

0 commit comments

Comments
 (0)