Skip to content

Commit

Permalink
Add to prop to Button
Browse files Browse the repository at this point in the history
  • Loading branch information
nick-at-work committed Oct 18, 2024
1 parent d63ec4f commit 7ac6ccb
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
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 @@ import React, {
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 @@ export interface BaseButtonProps {
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 @@ export const Button = forwardRef<
fullWidth = false,
id = undefined,
href = undefined,
to = undefined,
iconPrefix = undefined,
iconSuffix = undefined,
isDisabled = false,
Expand Down Expand Up @@ -257,8 +261,7 @@ export const Button = forwardRef<
);

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

return createElement(
const button = createElement(
buttonElement,
{
id,
Expand All @@ -276,5 +279,15 @@ export const Button = forwardRef<
},
buttonContent,
);

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

return button;
},
);

0 comments on commit 7ac6ccb

Please sign in to comment.