Skip to content

Commit 8999188

Browse files
committed
[fix] Pressable disabled focus behavior
Fix necolas#2127
1 parent 36681c4 commit 8999188

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,25 @@ describe('components/Pressable', () => {
9191
expect(container.firstChild).toMatchSnapshot();
9292
});
9393

94+
test('focus interaction (disabled)', () => {
95+
const onBlur = jest.fn();
96+
const onFocus = jest.fn();
97+
const ref = React.createRef();
98+
act(() => {
99+
render(<Pressable disabled={true} onBlur={onBlur} onFocus={onFocus} ref={ref} />);
100+
});
101+
const target = createEventTarget(ref.current);
102+
const body = createEventTarget(document.body);
103+
act(() => {
104+
target.focus();
105+
});
106+
expect(onFocus).not.toBeCalled();
107+
act(() => {
108+
body.focus({ relatedTarget: target.node });
109+
});
110+
expect(onBlur).not.toBeCalled();
111+
});
112+
94113
test('hover interaction', () => {
95114
let container;
96115
const onHoverIn = jest.fn();

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,26 +144,32 @@ function Pressable(props: Props, forwardedRef): React.Node {
144144

145145
const blurHandler = React.useCallback(
146146
(e) => {
147+
if (disabled) {
148+
return;
149+
}
147150
if (e.nativeEvent.target === hostRef.current) {
148151
setFocused(false);
149152
if (onBlur != null) {
150153
onBlur(e);
151154
}
152155
}
153156
},
154-
[hostRef, setFocused, onBlur]
157+
[disabled, hostRef, setFocused, onBlur]
155158
);
156159

157160
const focusHandler = React.useCallback(
158161
(e) => {
162+
if (disabled) {
163+
return;
164+
}
159165
if (e.nativeEvent.target === hostRef.current) {
160166
setFocused(true);
161167
if (onFocus != null) {
162168
onFocus(e);
163169
}
164170
}
165171
},
166-
[hostRef, setFocused, onFocus]
172+
[disabled, hostRef, setFocused, onFocus]
167173
);
168174

169175
const contextMenuHandler = React.useCallback(

0 commit comments

Comments
 (0)