From 35c3a93a727f2cdc42158635546bb87279c6555a Mon Sep 17 00:00:00 2001 From: Muhammed Mustafa Date: Fri, 16 Dec 2022 18:57:39 +0200 Subject: [PATCH] feat: add the ability to create a download button (#48697) * feat: add the ability to create a download button * Allow the creating of download button * update test, and stories props --- .../src/button/button.stories.tsx | 17 +++++++++++--- .../ui-components/src/button/button.test.tsx | 8 +++---- tools/ui-components/src/button/button.tsx | 23 ++++++++++++++----- tools/ui-components/src/button/types.ts | 5 ++-- tools/ui-components/src/link/types.ts | 3 ++- 5 files changed, 40 insertions(+), 16 deletions(-) diff --git a/tools/ui-components/src/button/button.stories.tsx b/tools/ui-components/src/button/button.stories.tsx index 1650aecfeaf..dc01dd5e055 100644 --- a/tools/ui-components/src/button/button.stories.tsx +++ b/tools/ui-components/src/button/button.stories.tsx @@ -14,7 +14,8 @@ const story = { 'size', 'disabled', 'block', - 'to', + 'href', + 'download', 'target', 'onClick' ] @@ -41,7 +42,10 @@ const story = { onClick: { action: 'clicked' }, - to: { + href: { + control: { type: 'text' } + }, + download: { control: { type: 'text' } } } @@ -95,7 +99,14 @@ FullWidth.args = { export const AsALink = Template.bind({}); AsALink.args = { children: "I'm a link that looks like a button", - to: 'https://www.freecodecamp.org' + href: 'https://www.freecodecamp.org' +}; + +export const AsADownloadLink = Template.bind({}); +AsALink.args = { + children: "I'm a link that looks like a button", + href: 'https://www.freecodecamp.org', + download: 'download.txt' }; export default story; diff --git a/tools/ui-components/src/button/button.test.tsx b/tools/ui-components/src/button/button.test.tsx index 689a29cc0a8..2498d61c6de 100644 --- a/tools/ui-components/src/button/button.test.tsx +++ b/tools/ui-components/src/button/button.test.tsx @@ -72,8 +72,8 @@ describe('Button', () => { expect(onClick).not.toBeCalled(); }); - it('should render an anchor element if the `to` prop is defined', () => { - render(); + it('should render an anchor element if the `href` prop is defined', () => { + render(); const link = screen.getByRole('link', { name: /freeCodeCamp/i }); const button = screen.queryByRole('button', { name: /freeCodeCamp/i }); @@ -84,9 +84,9 @@ describe('Button', () => { expect(button).not.toBeInTheDocument(); }); - it('should render a button element if the `to` and `disabled` props are both defined', () => { + it('should render a button element if the `href` and `disabled` props are both defined', () => { render( - ); diff --git a/tools/ui-components/src/button/button.tsx b/tools/ui-components/src/button/button.tsx index 90735342b67..e4d1bf731bf 100644 --- a/tools/ui-components/src/button/button.tsx +++ b/tools/ui-components/src/button/button.tsx @@ -116,7 +116,8 @@ export const Button = React.forwardRef< children, disabled, block, - to, + href, + download, target }, ref @@ -140,7 +141,7 @@ export const Button = React.forwardRef< [onClick] ); - const renderButton = useCallback(() => { + const renderButton: () => JSX.Element = useCallback(() => { return (