Skip to content

Commit 9169375

Browse files
authored
[react-interactions] Add onFocusWithin event to FocusWithin responder (#17115)
1 parent d7feeb2 commit 9169375

File tree

1 file changed

+24
-1
lines changed
  • packages/react-interactions/events/src/dom

1 file changed

+24
-1
lines changed

packages/react-interactions/events/src/dom/Focus.js

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ type FocusEventType = 'focus' | 'blur' | 'focuschange' | 'focusvisiblechange';
4949

5050
type FocusWithinProps = {
5151
disabled?: boolean,
52+
onFocusWithin?: (e: FocusEvent) => void,
5253
onBlurWithin?: (e: FocusEvent) => void,
5354
onFocusWithinChange?: boolean => void,
5455
onFocusWithinVisibleChange?: boolean => void,
@@ -57,7 +58,8 @@ type FocusWithinProps = {
5758
type FocusWithinEventType =
5859
| 'focuswithinvisiblechange'
5960
| 'focuswithinchange'
60-
| 'blurwithin';
61+
| 'blurwithin'
62+
| 'focuswithin';
6163

6264
/**
6365
* Shared between Focus and FocusWithin
@@ -239,6 +241,26 @@ function dispatchBlurEvents(
239241
}
240242
}
241243

244+
function dispatchFocusWithinEvents(
245+
context: ReactDOMResponderContext,
246+
event: ReactDOMResponderEvent,
247+
props: FocusWithinProps,
248+
state: FocusState,
249+
) {
250+
const pointerType = state.pointerType;
251+
const target = ((state.focusTarget: any): Element | Document) || event.target;
252+
const onFocusWithin = (props.onFocusWithin: any);
253+
if (isFunction(onFocusWithin)) {
254+
const syntheticEvent = createFocusEvent(
255+
context,
256+
'focuswithin',
257+
target,
258+
pointerType,
259+
);
260+
context.dispatchEvent(syntheticEvent, onFocusWithin, DiscreteEvent);
261+
}
262+
}
263+
242264
function dispatchBlurWithinEvents(
243265
context: ReactDOMResponderContext,
244266
event: ReactDOMResponderEvent,
@@ -474,6 +496,7 @@ const focusWithinResponderImpl = {
474496
state.isFocusVisible = isGlobalFocusVisible;
475497
dispatchFocusWithinVisibleChangeEvent(context, props, state, true);
476498
}
499+
dispatchFocusWithinEvents(context, event, props, state);
477500
break;
478501
}
479502
case 'blur': {

0 commit comments

Comments
 (0)