Skip to content

Commit 4146ab4

Browse files
Fix Select keyboard navigation and FieldItem label focus
- Focus custom controls when their FieldItem label is clicked - Preserve native label behavior and respect disabled controls - Support Tab, Shift+Tab, Enter and Space on Select’s clear action - Prevent clearing and tooltip updates from opening options or stealing focus - Reuse Blueprint’s item comparison helper - Add regression tests for label focus and keyboard navigation
1 parent 7d7c807 commit 4146ab4

5 files changed

Lines changed: 451 additions & 23 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,13 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this p
88

99
### Fixed
1010

11+
- `<FieldItem />`
12+
- Clicking a label focuses its control when a native label association is not available, including Select comboboxes and CodeEditor.
1113
- `<Select />`
1214
- Restore keyboard navigation for non-filterable Select.
1315
- Restore predictable Enter, arrow key, Escape, and Tab behavior, with a visible focus indicator.
1416
- Keep focus on the combobox and expose the active option to screen readers.
17+
- Keep the clear action reachable with Tab and Shift+Tab and support clearing with Enter or Space without opening the options.
1518

1619
## [26.2.1] - 2026-11-02
1720

‎src/components/Form/FieldItem.tsx‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,9 +220,37 @@ export const FieldItem = ({
220220
return () => partsObserver.disconnect();
221221
}, [connectParts, preventAriaAttribution]);
222222

223+
const handleLabelClick: React.MouseEventHandler<HTMLLabelElement> = (event) => {
224+
labelProps?.onClick?.(event);
225+
if (event.defaultPrevented || disabled || preventAriaAttribution) {
226+
return;
227+
}
228+
const labelElement = event.currentTarget;
229+
if (labelElement instanceof HTMLLabelElement && labelElement.control) {
230+
return;
231+
}
232+
if (!(event.target instanceof Node) || !labelElement.contains(event.target)) {
233+
return;
234+
}
235+
const interactiveTarget =
236+
event.target instanceof Element
237+
? event.target.closest(
238+
"a[href], button, input, select, textarea, summary, [tabindex], [contenteditable=true], [role=button], [role=link]",
239+
)
240+
: null;
241+
if (interactiveTarget && labelElement.contains(interactiveTarget)) {
242+
return;
243+
}
244+
const control = connectedInputRef.current;
245+
if (control && !control.matches(':disabled, [aria-disabled="true"], [contenteditable="false"]')) {
246+
control.focus();
247+
}
248+
};
249+
223250
const label = (
224251
<Label
225252
{...labelProps}
253+
onClick={handleLabelClick}
226254
className={`${eccgui}-fielditem__label` + (labelProps?.className ? " " + labelProps.className : "")}
227255
disabled={disabled}
228256
/>

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

Lines changed: 171 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import React from "react";
2-
import { render, waitFor } from "@testing-library/react";
2+
import { render, screen, waitFor } from "@testing-library/react";
3+
import userEvent from "@testing-library/user-event";
4+
import { createPortal } from "react-dom";
35

46
import "@testing-library/jest-dom";
57

@@ -401,6 +403,150 @@ describe("FieldItem", () => {
401403
});
402404
});
403405

406+
describe("FieldItem label activation", () => {
407+
const renderCombobox = (props: React.ComponentProps<typeof FieldItem> = {}) =>
408+
renderFieldItem({
409+
labelProps: { text: "Type" },
410+
children: (
411+
<div className={`${eccgui}-select`} role="combobox" tabIndex={0}>
412+
<button type="button" tabIndex={-1}>
413+
Value
414+
</button>
415+
</div>
416+
),
417+
...props,
418+
});
419+
420+
it("focuses the combobox when clicking its label", async () => {
421+
const user = userEvent.setup();
422+
renderCombobox();
423+
424+
await user.click(screen.getByText("Type"));
425+
426+
expect(screen.getByRole("combobox", { name: "Type" })).toHaveFocus();
427+
});
428+
429+
it("focuses an input when its label is rendered as a span", async () => {
430+
const user = userEvent.setup();
431+
renderFieldItem({
432+
labelProps: { text: "Name", isLayoutForElement: "span" },
433+
children: <input />,
434+
});
435+
436+
await user.click(screen.getByText("Name"));
437+
438+
expect(screen.getByRole("textbox", { name: "Name" })).toHaveFocus();
439+
});
440+
441+
it("focuses the control inside a focusable ancestor", async () => {
442+
const user = userEvent.setup();
443+
render(
444+
<div tabIndex={-1}>
445+
<FieldItem labelProps={{ text: "Type" }}>
446+
<div className={`${eccgui}-select`} role="combobox" tabIndex={0}>
447+
Value
448+
</div>
449+
</FieldItem>
450+
</div>,
451+
);
452+
453+
await user.click(screen.getByText("Type"));
454+
455+
expect(screen.getByRole("combobox")).toHaveFocus();
456+
});
457+
458+
it("preserves native activation of an explicitly labelled second input", async () => {
459+
const user = userEvent.setup();
460+
const { input } = renderFieldItem({
461+
labelProps: { text: "Second", htmlFor: "second" },
462+
children: (
463+
<>
464+
<input id="first" />
465+
<input id="second" />
466+
</>
467+
),
468+
});
469+
const focusFirst = jest.spyOn(input, "focus");
470+
471+
await user.click(screen.getByText("Second"));
472+
473+
expect(screen.getByRole("textbox", { name: "Second" })).toHaveFocus();
474+
expect(focusFirst).not.toHaveBeenCalled();
475+
});
476+
477+
it.each([false, true])("respects a label click handler with preventDefault=%s", async (cancel) => {
478+
const user = userEvent.setup();
479+
const onClick = jest.fn((event: React.MouseEvent<HTMLLabelElement>) => {
480+
if (cancel) event.preventDefault();
481+
});
482+
renderCombobox({ labelProps: { text: "Type", onClick } });
483+
484+
await user.click(screen.getByText("Type"));
485+
486+
expect(onClick).toHaveBeenCalledTimes(1);
487+
if (cancel) {
488+
expect(screen.getByRole("combobox")).not.toHaveFocus();
489+
} else {
490+
expect(screen.getByRole("combobox")).toHaveFocus();
491+
}
492+
});
493+
494+
it.each(["link", "button", "focusable text"])("preserves clicks on a %s inside the label", async (kind) => {
495+
const user = userEvent.setup();
496+
const help =
497+
kind === "link" ? (
498+
<a href="#field-help">Help</a>
499+
) : kind === "button" ? (
500+
<button type="button">Help</button>
501+
) : (
502+
<span tabIndex={0}>Help</span>
503+
);
504+
renderCombobox({ labelProps: { text: "Type", additionalElements: help } });
505+
const focusControl = jest.spyOn(screen.getByRole("combobox"), "focus");
506+
507+
await user.click(screen.getByText("Help"));
508+
509+
expect(focusControl).not.toHaveBeenCalled();
510+
expect(screen.getByText("Help")).toHaveFocus();
511+
});
512+
513+
it("preserves clicks on portaled label content", async () => {
514+
const user = userEvent.setup();
515+
renderCombobox({
516+
labelProps: {
517+
text: "Type",
518+
additionalElements: createPortal(<button type="button">Help</button>, document.body),
519+
},
520+
});
521+
const focusControl = jest.spyOn(screen.getByRole("combobox"), "focus");
522+
523+
await user.click(screen.getByText("Help"));
524+
525+
expect(focusControl).not.toHaveBeenCalled();
526+
expect(screen.getByText("Help")).toHaveFocus();
527+
});
528+
529+
it("does not focus the control of a disabled field", async () => {
530+
const user = userEvent.setup();
531+
renderCombobox({ disabled: true });
532+
const focusControl = jest.spyOn(screen.getByRole("combobox"), "focus");
533+
534+
await user.click(screen.getByText("Type"));
535+
536+
expect(focusControl).not.toHaveBeenCalled();
537+
});
538+
539+
it("does not add focus behavior when automatic connections are disabled", async () => {
540+
const user = userEvent.setup();
541+
renderCombobox({ preventAriaAttribution: true });
542+
const focusControl = jest.spyOn(screen.getByRole("combobox"), "focus");
543+
544+
await user.click(screen.getByText("Type"));
545+
546+
expect(focusControl).not.toHaveBeenCalled();
547+
});
548+
});
549+
404550
describe("FieldItem with CodeEditor", () => {
405551
beforeAll(() => {
406552
// the code editor needs range support that is not provided by jsdom
@@ -435,4 +581,28 @@ describe("FieldItem with CodeEditor", () => {
435581
expect(input).toHaveAttribute("aria-labelledby", label.id);
436582
expect(input).toHaveAttribute("aria-describedby", message.id);
437583
});
584+
585+
it.each([
586+
[false, false],
587+
[false, true],
588+
[true, false],
589+
[true, true],
590+
])("handles label clicks with disabled=%s and readOnly=%s", async (disabled, readOnly) => {
591+
const user = userEvent.setup();
592+
const { container } = render(
593+
<FieldItem labelProps={{ text: "Expression" }}>
594+
<CodeEditor name="expression" mode="yaml" disabled={disabled} readOnly={readOnly} />
595+
</FieldItem>,
596+
);
597+
const input = container.querySelector(".cm-content");
598+
await waitFor(() => expect(input).toHaveAccessibleName("Expression"));
599+
600+
await user.click(screen.getByText("Expression"));
601+
602+
if (disabled) {
603+
expect(input).not.toHaveFocus();
604+
} else {
605+
expect(input).toHaveFocus();
606+
}
607+
});
438608
});

0 commit comments

Comments
 (0)