Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ButtonUnstyled] Don't set redundant role=button #28488

Merged
merged 6 commits into from
Oct 4, 2021
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
32 changes: 32 additions & 0 deletions packages/mui-core/src/ButtonUnstyled/ButtonUnstyled.test.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as React from 'react';
import { createMount, createClientRender, describeConformanceUnstyled } from 'test/utils';
import ButtonUnstyled, { buttonUnstyledClasses } from '@mui/core/ButtonUnstyled';
import { expect } from 'chai';

describe('<ButtonUnstyled />', () => {
const mount = createMount();
Expand All @@ -19,4 +20,35 @@ describe('<ButtonUnstyled />', () => {
},
},
}));

describe('role attribute', () => {
it('is set when the root component is an HTML element other than a button', () => {
const { getByRole } = render(<ButtonUnstyled component="span" />);
expect(getByRole('button')).not.to.equal(null);
});

it('is set when the root component is a component that renders an HTML component other than a button', () => {
const WrappedSpan = React.forwardRef(
(
props: React.HTMLAttributes<HTMLSpanElement>,
ref: React.ForwardedRef<HTMLSpanElement>,
) => <span role={props.role} ref={ref} />,
);

const { getByRole } = render(<ButtonUnstyled component={WrappedSpan} />);
expect(getByRole('button')).not.to.equal(null);
});

it('is not set when the root component is a component that renders an HTML button component', () => {
const WrappedButton = React.forwardRef(
(
props: React.HTMLAttributes<HTMLButtonElement>,
ref: React.ForwardedRef<HTMLButtonElement>,
) => <button role={props.role} ref={ref} />,
);

const { getByRole } = render(<ButtonUnstyled component={WrappedButton} />);
expect(getByRole('button')).not.to.have.attribute('role');
});
});
});
20 changes: 14 additions & 6 deletions packages/mui-core/src/ButtonUnstyled/useButton.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as React from 'react';
import {
unstable_setRef as setRef,
unstable_useEventCallback as useEventCallback,
unstable_useForkRef as useForkRef,
unstable_useIsFocusVisible as useIsFocusVisible,
Expand Down Expand Up @@ -145,11 +146,21 @@ export default function useButton(props: UseButtonProps) {
},
);

const handleOwnRef = useForkRef(focusVisibleRef, buttonRef);
const handleRef = useForkRef(ref, handleOwnRef);

const [hostElementName, setHostElementName] = React.useState<string>('');

const updateRef = (instance: HTMLElement | null) => {
setHostElementName(instance?.tagName ?? '');
setRef(handleRef, instance);
};

const buttonProps: Record<string, unknown> = {};
if (elementType === 'button') {
if (hostElementName === 'BUTTON') {
buttonProps.type = type ?? 'button';
buttonProps.disabled = disabled;
} else {
} else if (hostElementName !== '') {
if (!href && !to) {
buttonProps.role = 'button';
}
Expand All @@ -158,9 +169,6 @@ export default function useButton(props: UseButtonProps) {
}
}

const handleOwnRef = useForkRef(focusVisibleRef, buttonRef);
const handleRef = useForkRef(ref, handleOwnRef);

const getRootProps = (otherHandlers?: Record<string, React.EventHandler<any>>) => {
const propsEventHandlers = extractEventHandlers(props);
const externalEventHandlers = { ...propsEventHandlers, ...otherHandlers };
Expand All @@ -187,7 +195,7 @@ export default function useButton(props: UseButtonProps) {
return {
tabIndex: disabled ? -1 : tabIndex,
type,
ref: handleRef as React.Ref<any>,
ref: updateRef,
...buttonProps,
...mergedEventHandlers,
};
Expand Down