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] Update to render as link when href or to is provided #34337

Merged
merged 5 commits into from Oct 27, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
34 changes: 34 additions & 0 deletions packages/mui-base/src/ButtonUnstyled/ButtonUnstyled.test.tsx
Expand Up @@ -147,4 +147,38 @@ describe('<ButtonUnstyled />', () => {
});
});
});

describe('prop: href', () => {
it('renders as a link when the "href" prop is provided', () => {
const { getByRole } = render(<ButtonUnstyled href="#" />);
expect(getByRole('link')).not.to.equal(null);
});

it('renders as the element provided in the "component" prop, even with a "href" prop', () => {
const { getByRole } = render(<ButtonUnstyled component="h1" href="#" />);
expect(getByRole('heading')).not.to.equal(null);
});

it('renders as the element provided in the "components.Root" prop, even with a "href" prop', () => {
const { getByRole } = render(<ButtonUnstyled slots={{ root: 'h1' }} href="#" />);
expect(getByRole('heading')).not.to.equal(null);
});
});

describe('prop: to', () => {
it('renders as a link when the "to" prop is provided', () => {
const { container } = render(<ButtonUnstyled to="#" />);
expect(container.querySelector('a')).not.to.equal(null);
});

it('renders as the element provided in the "component" prop, even with a "to" prop', () => {
const { getByRole } = render(<ButtonUnstyled component="h1" to="#" />);
expect(getByRole('heading')).not.to.equal(null);
});

it('renders as the element provided in the "components.Root" prop, even with a "to" prop', () => {
const { getByRole } = render(<ButtonUnstyled slots={{ root: 'h1' }} to="#" />);
expect(getByRole('heading')).not.to.equal(null);
});
});
});
11 changes: 10 additions & 1 deletion packages/mui-base/src/ButtonUnstyled/ButtonUnstyled.tsx
Expand Up @@ -86,7 +86,8 @@ const ButtonUnstyled = React.forwardRef(function ButtonUnstyled<

const classes = useUtilityClasses(ownerState);

const Root: React.ElementType = component ?? slots.root ?? 'button';
const defaultElement = other.href || other.to ? 'a' : 'button';
const Root: React.ElementType = component ?? slots.root ?? defaultElement;
const rootProps: WithOptionalOwnerState<ButtonUnstyledRootSlotProps> = useSlotProps({
elementType: Root,
getSlotProps: getRootProps,
Expand Down Expand Up @@ -137,6 +138,10 @@ ButtonUnstyled.propTypes /* remove-proptypes */ = {
* @default false
*/
focusableWhenDisabled: PropTypes.bool,
/**
* @ignore
*/
href: PropTypes.string,
/**
* @ignore
*/
Expand Down Expand Up @@ -180,6 +185,10 @@ ButtonUnstyled.propTypes /* remove-proptypes */ = {
slots: PropTypes.shape({
root: PropTypes.elementType,
}),
/**
* @ignore
*/
to: PropTypes.string,
} as any;

export default ButtonUnstyled;