From f861e6a269ba9f62700776d3cd13b644a9e836d4 Mon Sep 17 00:00:00 2001 From: Armand Philippot Date: Wed, 20 Sep 2023 16:38:54 +0200 Subject: refactor: use named export for everything except pages Next expect a default export for pages so only those components should use default exports. Everything else should use named exports to reduce the number of import statements. --- .../atoms/buttons/button-link.stories.tsx | 2 +- src/components/atoms/buttons/button-link.test.tsx | 2 +- src/components/atoms/buttons/button-link.tsx | 33 +++--------- src/components/atoms/buttons/button.stories.tsx | 2 +- src/components/atoms/buttons/button.test.tsx | 2 +- src/components/atoms/buttons/button.tsx | 58 ++++++++++------------ src/components/atoms/buttons/index.ts | 2 + 7 files changed, 39 insertions(+), 62 deletions(-) create mode 100644 src/components/atoms/buttons/index.ts (limited to 'src/components/atoms/buttons') diff --git a/src/components/atoms/buttons/button-link.stories.tsx b/src/components/atoms/buttons/button-link.stories.tsx index ff0a67f..32c2a7f 100644 --- a/src/components/atoms/buttons/button-link.stories.tsx +++ b/src/components/atoms/buttons/button-link.stories.tsx @@ -1,5 +1,5 @@ import { ComponentMeta, ComponentStory } from '@storybook/react'; -import ButtonLink from './button-link'; +import { ButtonLink } from './button-link'; /** * ButtonLink - Storybook Meta diff --git a/src/components/atoms/buttons/button-link.test.tsx b/src/components/atoms/buttons/button-link.test.tsx index a5aa7b1..8fabacf 100644 --- a/src/components/atoms/buttons/button-link.test.tsx +++ b/src/components/atoms/buttons/button-link.test.tsx @@ -1,5 +1,5 @@ import { render, screen } from '../../../../tests/utils'; -import ButtonLink from './button-link'; +import { ButtonLink } from './button-link'; describe('ButtonLink', () => { it('renders a ButtonLink component', () => { diff --git a/src/components/atoms/buttons/button-link.tsx b/src/components/atoms/buttons/button-link.tsx index 7182d94..c8180c9 100644 --- a/src/components/atoms/buttons/button-link.tsx +++ b/src/components/atoms/buttons/button-link.tsx @@ -1,24 +1,12 @@ import Link from 'next/link'; -import { FC, ReactNode } from 'react'; +import { AnchorHTMLAttributes, FC, ReactNode } from 'react'; import styles from './buttons.module.scss'; -export type ButtonLinkProps = { - /** - * ButtonLink accessible label. - */ - 'aria-label'?: string; - /** - * One or more ids that refer to the accessible label. - */ - 'aria-labelledby'?: string; +export type ButtonLinkProps = AnchorHTMLAttributes & { /** * The button link body. */ children: ReactNode; - /** - * Set additional classnames to the button link. - */ - className?: string; /** * True if it is an external link. Default: false. */ @@ -42,7 +30,7 @@ export type ButtonLinkProps = { * * Use a button-like link as call to action. */ -const ButtonLink: FC = ({ +export const ButtonLink: FC = ({ children, className, target, @@ -53,24 +41,15 @@ const ButtonLink: FC = ({ }) => { const kindClass = styles[`btn--${kind}`]; const shapeClass = styles[`btn--${shape}`]; + const btnClass = `${styles.btn} ${kindClass} ${shapeClass} ${className}`; return external ? ( - + {children} ) : ( - + {children} ); }; - -export default ButtonLink; diff --git a/src/components/atoms/buttons/button.stories.tsx b/src/components/atoms/buttons/button.stories.tsx index 6803706..ba09a0d 100644 --- a/src/components/atoms/buttons/button.stories.tsx +++ b/src/components/atoms/buttons/button.stories.tsx @@ -1,5 +1,5 @@ import { ComponentMeta, ComponentStory } from '@storybook/react'; -import Button from './button'; +import { Button } from './button'; /** * Button - Storybook Meta diff --git a/src/components/atoms/buttons/button.test.tsx b/src/components/atoms/buttons/button.test.tsx index 90fca02..1162b2b 100644 --- a/src/components/atoms/buttons/button.test.tsx +++ b/src/components/atoms/buttons/button.test.tsx @@ -1,5 +1,5 @@ import { render, screen } from '../../../../tests/utils'; -import Button from './button'; +import { Button } from './button'; describe('Button', () => { it('renders the Button component', () => { diff --git a/src/components/atoms/buttons/button.tsx b/src/components/atoms/buttons/button.tsx index fecbcfd..6ef5775 100644 --- a/src/components/atoms/buttons/button.tsx +++ b/src/components/atoms/buttons/button.tsx @@ -1,56 +1,46 @@ import { + ButtonHTMLAttributes, forwardRef, ForwardRefRenderFunction, - MouseEventHandler, ReactNode, } from 'react'; import styles from './buttons.module.scss'; -export type ButtonProps = { - /** - * Button accessible label. - */ - 'aria-label'?: string; - /** - * Indicates the current "pressed" state of a toggle button. - */ - 'aria-pressed'?: boolean | 'mixed'; +export type ButtonProps = ButtonHTMLAttributes & { /** * The button body. */ children: ReactNode; /** - * Set additional classnames to the button wrapper. - */ - className?: string; - /** - * Button state. Default: false. + * Button state. + * + * @default false */ disabled?: boolean; /** - * Button kind. Default: secondary. + * Button kind. + * + * @default 'secondary' */ kind?: 'primary' | 'secondary' | 'tertiary' | 'neutral'; /** - * A callback function to handle click. - */ - onClick?: MouseEventHandler; - /** - * Button shape. Default: rectangle. + * Button shape. + * + * @default 'rectangle' */ shape?: 'circle' | 'rectangle' | 'square' | 'initial'; /** - * Button type attribute. Default: button. + * Button type attribute. + * + * @default 'button' */ type?: 'button' | 'reset' | 'submit'; }; -/** - * Button component - * - * Use a button as call to action. - */ -const Button: ForwardRefRenderFunction = ( +const ButtonWithRef: ForwardRefRenderFunction< + HTMLButtonElement, + ButtonProps +> = ( { className = '', children, @@ -64,18 +54,24 @@ const Button: ForwardRefRenderFunction = ( ) => { const kindClass = styles[`btn--${kind}`]; const shapeClass = styles[`btn--${shape}`]; + const btnClass = `${styles.btn} ${kindClass} ${shapeClass} ${className}`; return ( ); }; -export default forwardRef(Button); +/** + * Button component + * + * Use a button as call to action. + */ +export const Button = forwardRef(ButtonWithRef); diff --git a/src/components/atoms/buttons/index.ts b/src/components/atoms/buttons/index.ts new file mode 100644 index 0000000..486e485 --- /dev/null +++ b/src/components/atoms/buttons/index.ts @@ -0,0 +1,2 @@ +export * from './button'; +export * from './button-link'; -- cgit v1.2.3