Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 28 additions & 13 deletions packages/@react-aria/focus/src/FocusScope.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
getActiveElement,
getEventTarget,
getOwnerDocument,
getOwnerWindow,
isAndroid,
isChrome,
isFocusable,
Expand Down Expand Up @@ -295,23 +296,37 @@ function shouldContainFocus(scopeRef: ScopeRef) {
return true;
}

function isTabbableRadio(element: HTMLInputElement) {
if (element.checked) {
return true;
}
let radios: HTMLInputElement[] = [];
function getRadiosInGroup(element: HTMLInputElement): HTMLInputElement[] {
if (!element.form) {
radios = ([...getOwnerDocument(element).querySelectorAll(`input[type="radio"][name="${CSS.escape(element.name)}"]`)] as HTMLInputElement[]).filter(radio => !radio.form);
} else {
let radioList = element.form?.elements?.namedItem(element.name) as RadioNodeList;
radios = [...(radioList ?? [])] as HTMLInputElement[];
// Radio buttons outside a form - query the document
return Array.from(
getOwnerDocument(element).querySelectorAll<HTMLInputElement>(
`input[type="radio"][name="${CSS.escape(element.name)}"]`
)
).filter(radio => !radio.form);
}
if (!radios) {
return false;

// namedItem returns RadioNodeList (iterable) for 2+ elements, but a single Element for exactly 1.
// https://developer.mozilla.org/en-US/docs/Web/API/HTMLFormControlsCollection/namedItem
const radioList = element.form.elements.namedItem(element.name);
let ownerWindow = getOwnerWindow(element);
if (radioList instanceof ownerWindow.RadioNodeList) {
return Array.from(radioList).filter(
(el): el is HTMLInputElement => el instanceof ownerWindow.HTMLInputElement
);
}
let anyChecked = radios.some(radio => radio.checked);
if (radioList instanceof ownerWindow.HTMLInputElement) {
return [radioList];
}
return [];
}

return !anyChecked;
function isTabbableRadio(element: HTMLInputElement): boolean {
if (element.checked) {
return true;
}
const radios = getRadiosInGroup(element);
return radios.length > 0 && !radios.some(radio => radio.checked);
}

function useFocusContainment(scopeRef: RefObject<Element[] | null>, contain?: boolean) {
Expand Down
26 changes: 26 additions & 0 deletions packages/@react-aria/focus/test/FocusScope.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1538,6 +1538,32 @@ describe('FocusScope', function () {
expect(document.activeElement).toBe(getByTestId('button1'));
});

it('handles forms with a single radio button without crashing', async function () {
// Regression test for https://github.com/adobe/react-spectrum/issues/9569
// form.elements.namedItem() returns Element (not RadioNodeList) for single elements
function Test() {
return (
<FocusScope contain>
<button data-testid="button1">First button</button>
<form>
<input type="radio" id="only" name="option" value="only" />
<label htmlFor="only">Only Option</label>
</form>
<button data-testid="button2">Second button</button>
</FocusScope>
);
}

let {getByTestId, getByRole} = render(<Test />);
let radio = getByRole('radio');
await user.tab();
expect(document.activeElement).toBe(getByTestId('button1'));
await user.tab();
expect(document.activeElement).toBe(radio);
await user.tab();
expect(document.activeElement).toBe(getByTestId('button2'));
});

describe('nested focus scopes', function () {
it('should make child FocusScopes the active scope regardless of DOM structure', function () {
function ChildComponent(props) {
Expand Down