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
16 changes: 16 additions & 0 deletions packages/react-aria-components/test/Checkbox.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -465,4 +465,20 @@ describe.each(['Checkbox', 'CheckboxField'])('%s', comp => {
expect(onBlur).not.toHaveBeenCalled();
expect(onFocus).not.toHaveBeenCalled();
});

it('should support implicit form submission from a focused checkbox on Enter', async () => {
let onSubmit = jest.fn(e => e.preventDefault());
let {getByRole} = render(
<form onSubmit={onSubmit}>
<Checkbox>Test</Checkbox>
<button type="submit">Submit</button>
</form>
);

let checkbox = getByRole('checkbox');
await user.click(checkbox);
await user.keyboard('{Enter}');

expect(onSubmit).toHaveBeenCalledTimes(1);
});
});
20 changes: 20 additions & 0 deletions packages/react-aria-components/test/RadioGroup.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -849,4 +849,24 @@ describe.each(['RadioGroup', 'RadioField'])('%s', comp => {
expect(onFocusB).toHaveBeenCalledTimes(1);
expect(onBlurA).toHaveBeenCalledTimes(1);
});

it('should support implicit form submission from a focused radio on Enter', async () => {
let onSubmit = jest.fn(e => e.preventDefault());
let {getAllByRole} = render(
<form onSubmit={onSubmit}>
<RadioGroup defaultValue="a">
<Label>Test</Label>
<Radio value="a">A</Radio>
<Radio value="b">B</Radio>
</RadioGroup>
<button type="submit">Submit</button>
</form>
);

let radio = getAllByRole('radio')[0];
await user.click(radio);
await user.keyboard('{Enter}');

expect(onSubmit).toHaveBeenCalledTimes(1);
});
});
16 changes: 16 additions & 0 deletions packages/react-aria-components/test/Switch.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -420,4 +420,20 @@ describe.each(['Switch', 'SwitchField'])('%s', comp => {
expect(checkbox).not.toHaveAttribute('aria-describedby');
});
}

it('should support implicit form submission from a focused switch on Enter', async () => {
let onSubmit = jest.fn(e => e.preventDefault());
let {getByRole} = render(
<form onSubmit={onSubmit}>
<Switch>Test</Switch>
<button type="submit">Submit</button>
</form>
);

let s = getByRole('switch');
await user.click(s);
await user.keyboard('{Enter}');

expect(onSubmit).toHaveBeenCalledTimes(1);
});
});
4 changes: 4 additions & 0 deletions packages/react-aria/src/interactions/usePress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1163,6 +1163,10 @@ function shouldPreventDefaultUp(target: Element) {

function shouldPreventDefaultKeyboard(target: Element, key: string) {
if (target instanceof HTMLInputElement) {
if (key === 'Enter' && (target.type === 'checkbox' || target.type === 'radio')) {
// Enter on a checkbox or radio should do an implicit form submission, but not toggle the input.
return false;
}
return !isValidInputKey(target, key);
}

Expand Down
2 changes: 1 addition & 1 deletion packages/react-aria/test/interactions/usePress.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3315,7 +3315,7 @@ describe('usePress', function () {
fireEvent.keyDown(el, {key: 'Enter'});
fireEvent.keyUp(el, {key: 'Enter'});

// Enter key handled should do nothing on a checkbox
// Enter key handled should not result in a press event on a checkbox
expect(events).toEqual([]);

let allow = fireEvent.keyDown(el, {key: ' '});
Expand Down