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 + .../atoms/forms/boolean-field.stories.tsx | 10 +- src/components/atoms/forms/boolean-field.test.tsx | 2 +- src/components/atoms/forms/boolean-field.tsx | 34 ++---- src/components/atoms/forms/field.stories.tsx | 2 +- src/components/atoms/forms/field.test.tsx | 2 +- src/components/atoms/forms/field.tsx | 20 ++-- src/components/atoms/forms/form.test.tsx | 2 +- src/components/atoms/forms/form.tsx | 32 +++-- src/components/atoms/forms/index.ts | 5 + src/components/atoms/forms/label.stories.tsx | 2 +- src/components/atoms/forms/label.test.tsx | 2 +- src/components/atoms/forms/label.tsx | 23 +--- src/components/atoms/forms/select.stories.tsx | 2 +- src/components/atoms/forms/select.test.tsx | 2 +- src/components/atoms/forms/select.tsx | 32 +---- src/components/atoms/headings/heading.stories.tsx | 2 +- src/components/atoms/headings/heading.test.tsx | 2 +- src/components/atoms/headings/heading.tsx | 34 +++--- src/components/atoms/headings/index.ts | 1 + src/components/atoms/icons/arrow.stories.tsx | 2 +- src/components/atoms/icons/arrow.test.tsx | 2 +- src/components/atoms/icons/arrow.tsx | 28 ++--- src/components/atoms/icons/career.stories.tsx | 2 +- src/components/atoms/icons/career.test.tsx | 2 +- src/components/atoms/icons/career.tsx | 21 +--- src/components/atoms/icons/cc-by-sa.stories.tsx | 2 +- src/components/atoms/icons/cc-by-sa.test.tsx | 2 +- src/components/atoms/icons/cc-by-sa.tsx | 14 +-- src/components/atoms/icons/close.stories.tsx | 2 +- src/components/atoms/icons/close.test.tsx | 2 +- src/components/atoms/icons/close.tsx | 21 +--- src/components/atoms/icons/cog.stories.tsx | 2 +- src/components/atoms/icons/cog.test.tsx | 2 +- src/components/atoms/icons/cog.tsx | 21 +--- .../atoms/icons/computer-screen.stories.tsx | 2 +- .../atoms/icons/computer-screen.test.tsx | 2 +- src/components/atoms/icons/computer-screen.tsx | 21 +--- src/components/atoms/icons/envelop.stories.tsx | 2 +- src/components/atoms/icons/envelop.test.tsx | 2 +- src/components/atoms/icons/envelop.tsx | 21 +--- src/components/atoms/icons/feed.stories.tsx | 2 +- src/components/atoms/icons/feed.test.tsx | 2 +- src/components/atoms/icons/feed.tsx | 21 +--- src/components/atoms/icons/hamburger.stories.tsx | 2 +- src/components/atoms/icons/hamburger.test.tsx | 2 +- src/components/atoms/icons/hamburger.tsx | 5 +- src/components/atoms/icons/home.stories.tsx | 2 +- src/components/atoms/icons/home.test.tsx | 2 +- src/components/atoms/icons/home.tsx | 21 +--- src/components/atoms/icons/index.ts | 15 +++ .../atoms/icons/magnifying-glass.stories.tsx | 2 +- .../atoms/icons/magnifying-glass.test.tsx | 2 +- src/components/atoms/icons/magnifying-glass.tsx | 21 +--- src/components/atoms/icons/moon.stories.tsx | 2 +- src/components/atoms/icons/moon.test.tsx | 2 +- src/components/atoms/icons/moon.tsx | 20 +--- src/components/atoms/icons/plus-minus.stories.tsx | 2 +- src/components/atoms/icons/plus-minus.test.tsx | 2 +- src/components/atoms/icons/plus-minus.tsx | 8 +- src/components/atoms/icons/posts-stack.stories.tsx | 2 +- src/components/atoms/icons/posts-stack.test.tsx | 2 +- src/components/atoms/icons/posts-stack.tsx | 24 ++-- src/components/atoms/icons/sun.stories.tsx | 2 +- src/components/atoms/icons/sun.test.tsx | 2 +- src/components/atoms/icons/sun.tsx | 18 +-- src/components/atoms/images/index.ts | 1 + src/components/atoms/images/logo.stories.tsx | 2 +- src/components/atoms/images/logo.test.tsx | 2 +- src/components/atoms/images/logo.tsx | 13 +- src/components/atoms/index.ts | 9 ++ src/components/atoms/layout/column.stories.tsx | 2 +- src/components/atoms/layout/column.test.tsx | 2 +- src/components/atoms/layout/column.tsx | 12 +- src/components/atoms/layout/copyright.stories.tsx | 4 +- src/components/atoms/layout/copyright.test.tsx | 4 +- src/components/atoms/layout/copyright.tsx | 4 +- src/components/atoms/layout/index.ts | 7 ++ src/components/atoms/layout/main.stories.tsx | 2 +- src/components/atoms/layout/main.test.tsx | 2 +- src/components/atoms/layout/main.tsx | 16 +-- src/components/atoms/layout/no-script.stories.tsx | 2 +- src/components/atoms/layout/no-script.test.tsx | 2 +- src/components/atoms/layout/no-script.tsx | 7 +- src/components/atoms/layout/notice.stories.tsx | 2 +- src/components/atoms/layout/notice.test.tsx | 2 +- src/components/atoms/layout/notice.tsx | 24 ++-- src/components/atoms/layout/section.stories.tsx | 2 +- src/components/atoms/layout/section.test.tsx | 2 +- src/components/atoms/layout/section.tsx | 25 ++-- src/components/atoms/layout/sidebar.stories.tsx | 2 +- src/components/atoms/layout/sidebar.test.tsx | 2 +- src/components/atoms/layout/sidebar.tsx | 22 ++-- src/components/atoms/links/index.ts | 4 + src/components/atoms/links/link.stories.tsx | 2 +- src/components/atoms/links/link.test.tsx | 2 +- src/components/atoms/links/link.tsx | 27 ++--- src/components/atoms/links/nav-link.stories.tsx | 2 +- src/components/atoms/links/nav-link.test.tsx | 2 +- src/components/atoms/links/nav-link.tsx | 4 +- .../atoms/links/sharing-link.stories.tsx | 10 +- src/components/atoms/links/sharing-link.test.tsx | 2 +- src/components/atoms/links/sharing-link.tsx | 7 +- src/components/atoms/links/social-link.stories.tsx | 2 +- src/components/atoms/links/social-link.test.tsx | 2 +- src/components/atoms/links/social-link.tsx | 6 +- .../atoms/lists/description-list-group.module.scss | 40 +++++++ .../atoms/lists/description-list-group.stories.tsx | 132 +++++++++++++++++++++ .../atoms/lists/description-list-group.test.tsx | 17 +++ .../atoms/lists/description-list-group.tsx | 70 +++++++++++ .../atoms/lists/description-list-item.module.scss | 40 ------- .../atoms/lists/description-list-item.stories.tsx | 132 --------------------- .../atoms/lists/description-list-item.test.tsx | 17 --- .../atoms/lists/description-list-item.tsx | 73 ------------ .../atoms/lists/description-list.stories.tsx | 2 +- .../atoms/lists/description-list.test.tsx | 2 +- src/components/atoms/lists/description-list.tsx | 38 +++--- src/components/atoms/lists/index.ts | 3 + src/components/atoms/lists/list.stories.tsx | 10 +- src/components/atoms/lists/list.test.tsx | 2 +- src/components/atoms/lists/list.tsx | 11 +- src/components/atoms/loaders/index.ts | 2 + .../atoms/loaders/progress-bar.stories.tsx | 2 +- src/components/atoms/loaders/progress-bar.test.tsx | 2 +- src/components/atoms/loaders/progress-bar.tsx | 10 +- src/components/atoms/loaders/spinner.stories.tsx | 2 +- src/components/atoms/loaders/spinner.test.tsx | 2 +- src/components/atoms/loaders/spinner.tsx | 6 +- 134 files changed, 656 insertions(+), 879 deletions(-) create mode 100644 src/components/atoms/buttons/index.ts create mode 100644 src/components/atoms/forms/index.ts create mode 100644 src/components/atoms/headings/index.ts create mode 100644 src/components/atoms/icons/index.ts create mode 100644 src/components/atoms/images/index.ts create mode 100644 src/components/atoms/index.ts create mode 100644 src/components/atoms/layout/index.ts create mode 100644 src/components/atoms/links/index.ts create mode 100644 src/components/atoms/lists/description-list-group.module.scss create mode 100644 src/components/atoms/lists/description-list-group.stories.tsx create mode 100644 src/components/atoms/lists/description-list-group.test.tsx create mode 100644 src/components/atoms/lists/description-list-group.tsx delete mode 100644 src/components/atoms/lists/description-list-item.module.scss delete mode 100644 src/components/atoms/lists/description-list-item.stories.tsx delete mode 100644 src/components/atoms/lists/description-list-item.test.tsx delete mode 100644 src/components/atoms/lists/description-list-item.tsx create mode 100644 src/components/atoms/lists/index.ts create mode 100644 src/components/atoms/loaders/index.ts (limited to 'src/components/atoms') 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'; diff --git a/src/components/atoms/forms/boolean-field.stories.tsx b/src/components/atoms/forms/boolean-field.stories.tsx index 8b6136b..3d6f8c3 100644 --- a/src/components/atoms/forms/boolean-field.stories.tsx +++ b/src/components/atoms/forms/boolean-field.stories.tsx @@ -1,13 +1,13 @@ import { ComponentMeta, ComponentStory } from '@storybook/react'; import { useState } from 'react'; -import BooleanFieldComponent from './boolean-field'; +import { BooleanField } from './boolean-field'; /** * BooleanField - Storybook Meta */ export default { title: 'Atoms/Forms', - component: BooleanFieldComponent, + component: BooleanField, args: { hidden: false, }, @@ -130,9 +130,9 @@ export default { }, }, }, -} as ComponentMeta; +} as ComponentMeta; -const Template: ComponentStory = ({ +const Template: ComponentStory = ({ checked, onChange: _onChange, ...args @@ -140,7 +140,7 @@ const Template: ComponentStory = ({ const [isChecked, setIsChecked] = useState(checked); return ( - { setIsChecked(!isChecked); diff --git a/src/components/atoms/forms/boolean-field.test.tsx b/src/components/atoms/forms/boolean-field.test.tsx index b6fa625..503d1ce 100644 --- a/src/components/atoms/forms/boolean-field.test.tsx +++ b/src/components/atoms/forms/boolean-field.test.tsx @@ -1,5 +1,5 @@ import { render, screen } from '../../../../tests/utils'; -import BooleanField from './boolean-field'; +import { BooleanField } from './boolean-field'; describe('BooleanField', () => { it('renders an unchecked checkbox', () => { diff --git a/src/components/atoms/forms/boolean-field.tsx b/src/components/atoms/forms/boolean-field.tsx index 946e375..8f33a42 100644 --- a/src/components/atoms/forms/boolean-field.tsx +++ b/src/components/atoms/forms/boolean-field.tsx @@ -1,23 +1,14 @@ -import { ChangeEventHandler, FC, MouseEventHandler } from 'react'; +import { FC, InputHTMLAttributes } from 'react'; import styles from './boolean-field.module.scss'; -export type BooleanFieldProps = { - /** - * One or more ids that refers to the checkbox name. - */ - 'aria-labelledby'?: string; +export type BooleanFieldProps = Omit< + InputHTMLAttributes, + 'checked' | 'hidden' | 'name' | 'type' | 'value' +> & { /** * True if the field should be checked. */ checked: boolean; - /** - * Add classnames to the checkbox. - */ - className?: string; - /** - * Field id attribute. - */ - id: string; /** * True if the field should be visually hidden. Default: false. */ @@ -26,14 +17,6 @@ export type BooleanFieldProps = { * Field name attribute. */ name: string; - /** - * Callback function to handle state change. - */ - onChange: ChangeEventHandler; - /** - * A callback function to handle click. - */ - onClick?: MouseEventHandler; /** * The input type. */ @@ -49,14 +32,13 @@ export type BooleanFieldProps = { * * Render a checkbox or a radio input type. */ -const BooleanField: FC = ({ +export const BooleanField: FC = ({ className = '', hidden = false, ...props }) => { const modifier = hidden ? 'hidden' : ''; + const inputClass = `${styles[modifier]} ${className}`; - return ; + return ; }; - -export default BooleanField; diff --git a/src/components/atoms/forms/field.stories.tsx b/src/components/atoms/forms/field.stories.tsx index 00a183d..27fd3be 100644 --- a/src/components/atoms/forms/field.stories.tsx +++ b/src/components/atoms/forms/field.stories.tsx @@ -1,6 +1,6 @@ import { ComponentMeta, ComponentStory } from '@storybook/react'; import { useState } from 'react'; -import Field from './field'; +import { Field } from './field'; /** * Field - Storybook Meta diff --git a/src/components/atoms/forms/field.test.tsx b/src/components/atoms/forms/field.test.tsx index 26ad764..492aa48 100644 --- a/src/components/atoms/forms/field.test.tsx +++ b/src/components/atoms/forms/field.test.tsx @@ -1,5 +1,5 @@ import { render, screen } from '../../../../tests/utils'; -import Field from './field'; +import { Field } from './field'; describe('Field', () => { it('renders a text input', () => { diff --git a/src/components/atoms/forms/field.tsx b/src/components/atoms/forms/field.tsx index 377e1b0..a4553e3 100644 --- a/src/components/atoms/forms/field.tsx +++ b/src/components/atoms/forms/field.tsx @@ -72,12 +72,7 @@ export type FieldProps = { value: string; }; -/** - * Field component. - * - * Render either an input or a textarea. - */ -const Field: ForwardRefRenderFunction = ( +const FieldWithRef: ForwardRefRenderFunction = ( { className = '', setValue, type, ...props }, ref ) => { @@ -93,19 +88,24 @@ const Field: ForwardRefRenderFunction = ( return type === 'textarea' ? (