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 @@ -385,4 +385,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 @@ -739,4 +739,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 @@ -359,4 +359,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/radio/useRadio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,10 @@ export function useRadio(props: AriaRadioProps, state: RadioGroupState, ref: Ref
}
});

// Let the hidden radio input handle keyboard events natively so Enter can
// submit forms like a native radio control.
delete labelProps.onKeyDown;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure that deleting the keydown handler is quite the right thing to do, seems like we should selectively delegate some keys but not others to the pressProps onKeyDown. ie only ignore Enter, not Space?


let {focusableProps} = useFocusable(mergeProps(props, {
onFocus: () => state.setLastFocusedValue(value)
}), ref);
Expand Down
4 changes: 4 additions & 0 deletions packages/react-aria/src/toggle/useToggle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,10 @@ export function useToggle(props: AriaToggleProps, state: ToggleState, ref: RefOb
isDisabled: isDisabled || isReadOnly
});

// Let the hidden input handle keyboard events natively so Enter can
// submit forms like a native checkbox/switch control.
delete labelProps.onKeyDown;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there's another usePress call, does it have the same problem?


let {focusableProps} = useFocusable(props, ref);
let interactions = mergeProps(pressProps, focusableProps);
let domProps = filterDOMProps(props, {labelable: true});
Expand Down
Loading