Skip to content

Commit 3c3e589

Browse files
committed
fix: suppress focusable warning for disabled Pressable
1 parent a6f868a commit 3c3e589

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

packages/@react-aria/interactions/src/Pressable.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,14 @@ export const Pressable = React.forwardRef(({children, ...props}: PressableProps,
3030
if (process.env.NODE_ENV === 'production') {
3131
return;
3232
}
33-
33+
3434
let el = ref.current;
3535
if (!el || !(el instanceof getOwnerWindow(el).Element)) {
3636
console.error('<Pressable> child must forward its ref to a DOM element.');
3737
return;
3838
}
3939

40-
if (!isFocusable(el)) {
40+
if (!props.isDisabled && !isFocusable(el)) {
4141
console.warn('<Pressable> child must be focusable. Please ensure the tabIndex prop is passed through.');
4242
return;
4343
}
@@ -79,7 +79,7 @@ export const Pressable = React.forwardRef(({children, ...props}: PressableProps,
7979
console.warn(`<Pressable> child must have an interactive ARIA role. Got "${role}".`);
8080
}
8181
}
82-
}, [ref]);
82+
}, [ref, props.isDisabled]);
8383

8484
// @ts-ignore
8585
let childRef = parseInt(React.version, 10) < 19 ? child.ref : child.props.ref;

packages/@react-aria/interactions/test/Pressable.test.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,19 @@ describe('Pressable', function () {
117117
expect(spy).toHaveBeenCalledWith('<Pressable> child must be focusable. Please ensure the tabIndex prop is passed through.');
118118
});
119119

120+
it('supports isDisabled', async function () {
121+
let spy = jest.spyOn(console, 'warn').mockImplementation(() => {});
122+
let {getByRole} = render(
123+
<Pressable isDisabled>
124+
<span role="button">Button</span>
125+
</Pressable>
126+
);
127+
128+
let button = getByRole('button');
129+
expect(button).not.toHaveAttribute('tabindex');
130+
expect(spy).not.toHaveBeenCalledWith('<Pressable> child must be focusable. Please ensure the tabIndex prop is passed through.');
131+
});
132+
120133
it('should warn if child does not have a role', async function () {
121134
let spy = jest.spyOn(console, 'warn').mockImplementation(() => {});
122135
render(

0 commit comments

Comments
 (0)