Skip to content

Commit bdd37cd

Browse files
ryo-manbasnowystingerLFDanLu
authored
Support for focus events in useRadioGroup hooks (#5441)
* Add onBlur event handling to useRadioGroup hook * Update packages/react-aria-components/test/RadioGroup.test.js Co-authored-by: Robert Snow <[email protected]> * refac: user user action instead of act * feat: support focus events --------- Co-authored-by: Robert Snow <[email protected]> Co-authored-by: Robert Snow <[email protected]> Co-authored-by: Daniel Lu <[email protected]>
1 parent 054ccfb commit bdd37cd

File tree

3 files changed

+32
-3
lines changed

3 files changed

+32
-3
lines changed

packages/@react-aria/radio/src/useRadioGroup.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,14 @@ export function useRadioGroup(props: AriaRadioGroupProps, state: RadioGroupState
6464
// there is no selection. This allows tabbing into the group from either
6565
// direction to go to the first or last radio.
6666
let {focusWithinProps} = useFocusWithin({
67-
onBlurWithin() {
67+
onBlurWithin(e) {
68+
props.onBlur?.(e);
6869
if (!state.selectedValue) {
6970
state.setLastFocusedValue(null);
7071
}
71-
}
72+
},
73+
onFocusWithin: props.onFocus,
74+
onFocusWithinChange: props.onFocusChange
7275
});
7376

7477
let onKeyDown = (e) => {

packages/@react-types/radio/src/index.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
AriaValidationProps,
1616
DOMProps,
1717
FocusableProps,
18+
FocusEvents,
1819
HelpTextProps,
1920
InputBase,
2021
InputDOMProps,
@@ -28,7 +29,7 @@ import {
2829
} from '@react-types/shared';
2930
import {ReactElement, ReactNode} from 'react';
3031

31-
export interface RadioGroupProps extends ValueBase<string>, InputBase, InputDOMProps, Validation<string | null>, LabelableProps, HelpTextProps {
32+
export interface RadioGroupProps extends ValueBase<string>, InputBase, InputDOMProps, Validation<string | null>, LabelableProps, HelpTextProps, FocusEvents {
3233
/**
3334
* The axis the Radio Button(s) should align with.
3435
* @default 'vertical'

packages/react-aria-components/test/RadioGroup.test.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,31 @@ describe('RadioGroup', () => {
453453
expect(group).not.toHaveAttribute('data-invalid');
454454
});
455455

456+
it('should support focus events', async () => {
457+
let onBlur = jest.fn();
458+
let onFocus = jest.fn();
459+
let onFocusChange = jest.fn();
460+
461+
let {getAllByRole} = renderGroup({onBlur, onFocus, onFocusChange});
462+
let radio = getAllByRole('radio')[0];
463+
464+
await user.tab();
465+
expect(document.activeElement).toBe(radio);
466+
expect(onBlur).toHaveBeenCalledTimes(0);
467+
expect(onFocus).toHaveBeenCalledTimes(1);
468+
expect(onFocusChange).toHaveBeenCalledTimes(1);
469+
470+
await user.keyboard('[ArrowRight]');
471+
expect(onBlur).toHaveBeenCalledTimes(0);
472+
expect(onFocus).toHaveBeenCalledTimes(1);
473+
expect(onFocusChange).toHaveBeenCalledTimes(1);
474+
475+
await user.tab();
476+
expect(onBlur).toHaveBeenCalledTimes(1);
477+
expect(onFocus).toHaveBeenCalledTimes(1);
478+
expect(onFocusChange).toHaveBeenCalledTimes(2);
479+
});
480+
456481
it('should support refs', () => {
457482
let groupRef = React.createRef();
458483
let radioRef = React.createRef();

0 commit comments

Comments
 (0)