Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions packages/mui-material/src/ButtonBase/ButtonBase.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,16 @@ const ButtonBase = React.forwardRef(function ButtonBase(inProps, ref) {

const isNonNativeButton = () => {
const button = buttonRef.current;
return component && component !== 'button' && !(button.tagName === 'A' && button.href);

if (!button) {
return component && component !== 'button';
}

if (button.tagName === 'BUTTON') {
return false;
}

return !(button.tagName === 'A' && button.href);
};

const handleKeyDown = useEventCallback((event) => {
Expand Down Expand Up @@ -240,7 +249,8 @@ const ButtonBase = React.forwardRef(function ButtonBase(inProps, ref) {
event.target === event.currentTarget &&
isNonNativeButton() &&
event.key === ' ' &&
!event.defaultPrevented
!event.defaultPrevented &&
!disabled
) {
event.currentTarget.click();
}
Expand Down
57 changes: 57 additions & 0 deletions packages/mui-material/src/ButtonBase/ButtonBase.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -824,6 +824,32 @@ describe('<ButtonBase />', () => {
setProps({ disabled: false });
expect(button).not.to.have.attribute('aria-disabled');
});

it('should not propagate click events when Space is released on a disabled non-native button', async () => {
const parentClickSpy = spy();
const buttonClickSpy = spy();

const { user } = render(
<div onClick={parentClickSpy}>
<ButtonBase component="span" disabled onClick={buttonClickSpy}>
Hello
</ButtonBase>
</div>,
);

const button = screen.getByRole('button');

// We don't use `user.tab()` because normal tab focus won't land on a disabled
// ButtonBase, only programmatic focus can happen
await act(async () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would user.tab() work here instead?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is kind of an edge case to begin with since it's a disabled non-native button, because it's disabled a Tab wouldn't land focus there to begin with, only programmatic focus is possible (added a comment in the test)

button.focus();
});

await user.keyboard(' ');

expect(buttonClickSpy.callCount).to.equal(0);
expect(parentClickSpy.callCount).to.equal(0);
});
});

describe('prop: component', () => {
Expand Down Expand Up @@ -1182,6 +1208,37 @@ describe('<ButtonBase />', () => {
expect(onClickSpy.callCount).to.equal(0);
});

it('should preserve native button keyboard behavior when a custom component renders a native button', async () => {
const onClickSpy = spy();
const onKeyDownSpy = spy();

/** @type {React.ForwardRefExoticComponent<React.ButtonHTMLAttributes<HTMLButtonElement>>} */
const MyButton = React.forwardRef((props, ref) => <button ref={ref} {...props} />);

const { user } = render(
<ButtonBase component={MyButton} onClick={onClickSpy} onKeyDown={onKeyDownSpy}>
Hello
</ButtonBase>,
);

await user.tab();

await user.keyboard('{Enter}');

expect(onKeyDownSpy.callCount).to.equal(1);
expect(onClickSpy.callCount).to.equal(1);
expect(onKeyDownSpy.firstCall.args[0]).to.have.property('defaultPrevented', false);

onClickSpy.resetHistory();
onKeyDownSpy.resetHistory();

await user.keyboard(' ');

expect(onKeyDownSpy.callCount).to.equal(1);
expect(onClickSpy.callCount).to.equal(1);
expect(onKeyDownSpy.firstCall.args[0]).to.have.property('defaultPrevented', false);
});

it('prevents default on Enter with an anchor and empty href', async () => {
const onClickSpy = spy();
const onKeyDownSpy = spy();
Expand Down
Loading