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

Button "to" prop to use react router #908

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion src/components/Button/Button.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,7 @@ describe('Button', () => {
expect(buttonElement.getAttribute('type')).toBe(null);
});

test('it renders a target attribute if one is passed, the element is an anchor, and there is a href', () => {
test('it renders a target attribute if one is passed, the element is an anchor, and there is an href', () => {
render(
<Button href="http://palmetto.com" as="a" target="_blank">
hey there
Expand Down
19 changes: 16 additions & 3 deletions src/components/Button/Button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
ButtonHTMLAttributes,
} from 'react';
import classNames from 'classnames';
import { Link } from 'react-router-dom';
import { IconName, ResponsiveProp } from '../../types';
import { generateResponsiveClasses } from '../../lib/generateResponsiveClasses';
import { handleReactRouterClick } from '../../lib/reactRouterClickHandler';
Expand Down Expand Up @@ -48,9 +49,11 @@
id?: string;
/**
* URL to navigate to when clicked. Passing this attribute automatically
* renders an anchor <a> tag, NOT a <button> element.
* renders an anchor <a> tag, NOT a <button> element, and does not use react-router.
*/
href?: string;
/** URL to navigate to via react-router. */
to?: string;
/**
* Disables the button, making it inoperable.
*/
Expand Down Expand Up @@ -132,6 +135,7 @@
fullWidth = false,
id = undefined,
href = undefined,
to = undefined,
iconPrefix = undefined,
iconSuffix = undefined,
isDisabled = false,
Expand Down Expand Up @@ -257,8 +261,7 @@
);

const buttonElement = getElementType(Button, { as });

return createElement(
const button = createElement(
buttonElement,
{
id,
Expand All @@ -267,7 +270,7 @@
disabled,
target: as === 'a' && href ? target : null,
onBlur: handleBlur,
onClick: (event: MouseEvent<HTMLAnchorElement | HTMLButtonElement>) => handleClick(event, onClick, target, navigate),

Check warning on line 273 in src/components/Button/Button.tsx

View workflow job for this annotation

GitHub Actions / lint js

This line has a length of 125. Maximum allowed is 120
onFocus: handleFocus,
ref,
type: type || (as !== 'a' && !href ? 'button' : undefined),
Expand All @@ -276,5 +279,15 @@
},
buttonContent,
);

if (to) {
return (
<Link to={to}>
{button}
</Link>
);
}

return button;
},
);
Loading