Skip to content

Commit 5726d83

Browse files
committed
[fix] Pressable supports onContextMenu prop
Fix #2004
1 parent d8f60b4 commit 5726d83

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

packages/react-native-web/src/exports/Pressable/__tests__/index-test.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ describe('components/Pressable', () => {
118118

119119
test('press interaction', () => {
120120
let container;
121+
const onContextMenu = jest.fn();
121122
const onPress = jest.fn();
122123
const onPressIn = jest.fn();
123124
const onPressOut = jest.fn();
@@ -126,6 +127,7 @@ describe('components/Pressable', () => {
126127
({ container } = render(
127128
<Pressable
128129
children={({ pressed }) => (pressed ? <div data-testid="press-content" /> : null)}
130+
onContextMenu={onContextMenu}
129131
onPress={onPress}
130132
onPressIn={onPressIn}
131133
onPressOut={onPressOut}
@@ -149,6 +151,10 @@ describe('components/Pressable', () => {
149151
expect(onPressOut).toBeCalled();
150152
expect(onPress).toBeCalled();
151153
expect(container.firstChild).toMatchSnapshot();
154+
act(() => {
155+
target.contextmenu({});
156+
});
157+
expect(onContextMenu).toBeCalled();
152158
});
153159

154160
describe('prop "ref"', () => {

packages/react-native-web/src/exports/Pressable/index.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ function Pressable(props: Props, forwardedRef): React.Node {
7878
disabled,
7979
focusable,
8080
onBlur,
81+
onContextMenu,
8182
onFocus,
8283
onHoverIn,
8384
onHoverOut,
@@ -128,7 +129,8 @@ function Pressable(props: Props, forwardedRef): React.Node {
128129
);
129130

130131
const pressEventHandlers = usePressEvents(hostRef, pressConfig);
131-
const { onKeyDown: onKeyDownPress } = pressEventHandlers;
132+
133+
const { onContextMenu: onContextMenuPress, onKeyDown: onKeyDownPress } = pressEventHandlers;
132134

133135
useHover(hostRef, {
134136
contain: true,
@@ -164,6 +166,18 @@ function Pressable(props: Props, forwardedRef): React.Node {
164166
[hostRef, setFocused, onFocus]
165167
);
166168

169+
const contextMenuHandler = React.useCallback(
170+
(e) => {
171+
if (onContextMenuPress != null) {
172+
onContextMenuPress(e);
173+
}
174+
if (onContextMenu != null) {
175+
onContextMenu(e);
176+
}
177+
},
178+
[onContextMenu, onContextMenuPress]
179+
);
180+
167181
const keyDownHandler = React.useCallback(
168182
(e) => {
169183
if (onKeyDownPress != null) {
@@ -183,6 +197,7 @@ function Pressable(props: Props, forwardedRef): React.Node {
183197
accessibilityDisabled={disabled}
184198
focusable={!disabled && focusable !== false}
185199
onBlur={blurHandler}
200+
onContextMenu={contextMenuHandler}
186201
onFocus={focusHandler}
187202
onKeyDown={keyDownHandler}
188203
ref={setRef}

0 commit comments

Comments
 (0)