aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorArmand Philippot <git@armandphilippot.com>2023-10-02 18:07:34 +0200
committerArmand Philippot <git@armandphilippot.com>2023-11-11 18:14:41 +0100
commitb52b8183ce299b5a2d3c3b2f4f8cb94bb443d746 (patch)
tree2ec276a4ebd5d39b891ff8fe8d8bf45c478fb97e
parent6221fa82e41915702d8139cade5c25e9d61e619f (diff)
refactor(components): rewrite Notice component
* Rename message prop to children prop and set ReactNode as type
-rw-r--r--src/components/atoms/index.ts1
-rw-r--r--src/components/atoms/layout/index.ts1
-rw-r--r--src/components/atoms/layout/notice.test.tsx12
-rw-r--r--src/components/atoms/notice/index.ts1
-rw-r--r--src/components/atoms/notice/notice.module.scss (renamed from src/components/atoms/layout/notice.module.scss)3
-rw-r--r--src/components/atoms/notice/notice.stories.tsx (renamed from src/components/atoms/layout/notice.stories.tsx)30
-rw-r--r--src/components/atoms/notice/notice.test.tsx37
-rw-r--r--src/components/atoms/notice/notice.tsx (renamed from src/components/atoms/layout/notice.tsx)18
-rw-r--r--src/components/templates/page/page-layout.tsx16
-rw-r--r--src/pages/blog/index.tsx5
-rw-r--r--src/pages/contact.tsx8
-rw-r--r--src/pages/recherche/index.tsx5
12 files changed, 80 insertions, 57 deletions
diff --git a/src/components/atoms/index.ts b/src/components/atoms/index.ts
index abcc198..4aa367a 100644
--- a/src/components/atoms/index.ts
+++ b/src/components/atoms/index.ts
@@ -7,4 +7,5 @@ export * from './links';
export * from './lists';
export * from './loaders';
export * from './modal';
+export * from './notice';
export * from './sidebar';
diff --git a/src/components/atoms/layout/index.ts b/src/components/atoms/layout/index.ts
index 9f467dc..3f2f8dc 100644
--- a/src/components/atoms/layout/index.ts
+++ b/src/components/atoms/layout/index.ts
@@ -5,5 +5,4 @@ export * from './footer';
export * from './header';
export * from './main';
export * from './nav';
-export * from './notice';
export * from './section';
diff --git a/src/components/atoms/layout/notice.test.tsx b/src/components/atoms/layout/notice.test.tsx
deleted file mode 100644
index f5213e4..0000000
--- a/src/components/atoms/layout/notice.test.tsx
+++ /dev/null
@@ -1,12 +0,0 @@
-import { describe, expect, it } from '@jest/globals';
-import { render, screen } from '../../../../tests/utils';
-import { Notice } from './notice';
-
-const message = 'Tenetur consequuntur tempore.';
-
-describe('Notice', () => {
- it('renders a message', () => {
- render(<Notice kind="info" message={message} />);
- expect(screen.getByText(message)).toBeInTheDocument();
- });
-});
diff --git a/src/components/atoms/notice/index.ts b/src/components/atoms/notice/index.ts
new file mode 100644
index 0000000..6351718
--- /dev/null
+++ b/src/components/atoms/notice/index.ts
@@ -0,0 +1 @@
+export * from './notice';
diff --git a/src/components/atoms/layout/notice.module.scss b/src/components/atoms/notice/notice.module.scss
index b532464..31cfc2d 100644
--- a/src/components/atoms/layout/notice.module.scss
+++ b/src/components/atoms/notice/notice.module.scss
@@ -1,8 +1,9 @@
@use "../../../styles/abstracts/functions" as fun;
-.wrapper {
+.notice {
padding: var(--spacing-2xs) var(--spacing-xs);
border: fun.convert-px(2) solid;
+ border-radius: fun.convert-px(4);
font-weight: bold;
&--error {
diff --git a/src/components/atoms/layout/notice.stories.tsx b/src/components/atoms/notice/notice.stories.tsx
index 57dddb3..60d97d9 100644
--- a/src/components/atoms/layout/notice.stories.tsx
+++ b/src/components/atoms/notice/notice.stories.tsx
@@ -1,11 +1,11 @@
-import { ComponentMeta, ComponentStory } from '@storybook/react';
+import type { ComponentMeta, ComponentStory } from '@storybook/react';
import { Notice as NoticeComponent } from './notice';
/**
* Notice - Storybook Meta
*/
export default {
- title: 'Atoms/Layout/Notice',
+ title: 'Atoms/Notice',
component: NoticeComponent,
argTypes: {
className: {
@@ -32,7 +32,7 @@ export default {
required: true,
},
},
- message: {
+ children: {
control: {
type: 'text',
},
@@ -52,35 +52,35 @@ const Template: ComponentStory<typeof NoticeComponent> = (args) => (
/**
* Notice stories - Error
*/
-export const Error = Template.bind({});
-Error.args = {
+export const ErrorKind = Template.bind({});
+ErrorKind.args = {
+ children: 'Nisi provident sapiente.',
kind: 'error',
- message: 'Nisi provident sapiente.',
};
/**
* Notice stories - Info
*/
-export const Info = Template.bind({});
-Info.args = {
+export const InfoKind = Template.bind({});
+InfoKind.args = {
+ children: 'Nisi provident sapiente.',
kind: 'info',
- message: 'Nisi provident sapiente.',
};
/**
* Notice stories - Success
*/
-export const Success = Template.bind({});
-Success.args = {
+export const SuccessKind = Template.bind({});
+SuccessKind.args = {
+ children: 'Nisi provident sapiente.',
kind: 'success',
- message: 'Nisi provident sapiente.',
};
/**
* Notice stories - Warning
*/
-export const Warning = Template.bind({});
-Warning.args = {
+export const WarningKind = Template.bind({});
+WarningKind.args = {
+ children: 'Nisi provident sapiente.',
kind: 'warning',
- message: 'Nisi provident sapiente.',
};
diff --git a/src/components/atoms/notice/notice.test.tsx b/src/components/atoms/notice/notice.test.tsx
new file mode 100644
index 0000000..dd6bef6
--- /dev/null
+++ b/src/components/atoms/notice/notice.test.tsx
@@ -0,0 +1,37 @@
+import { describe, expect, it } from '@jest/globals';
+import { render, screen as rtlScreen } from '@testing-library/react';
+import { Notice } from './notice';
+
+describe('Notice', () => {
+ it('can render an error notice', () => {
+ const body = 'culpa sint ut';
+
+ render(<Notice kind="error">{body}</Notice>);
+
+ expect(rtlScreen.getByText(body)).toHaveClass('notice--error');
+ });
+
+ it('can render an informative notice', () => {
+ const body = 'labore optio rerum';
+
+ render(<Notice kind="info">{body}</Notice>);
+
+ expect(rtlScreen.getByText(body)).toHaveClass('notice--info');
+ });
+
+ it('can render a success notice', () => {
+ const body = 'dolorem voluptatem velit';
+
+ render(<Notice kind="success">{body}</Notice>);
+
+ expect(rtlScreen.getByText(body)).toHaveClass('notice--success');
+ });
+
+ it('can render a warning notice', () => {
+ const body = 'ut non nihil';
+
+ render(<Notice kind="warning">{body}</Notice>);
+
+ expect(rtlScreen.getByText(body)).toHaveClass('notice--warning');
+ });
+});
diff --git a/src/components/atoms/layout/notice.tsx b/src/components/atoms/notice/notice.tsx
index 9f69af2..0c8a60c 100644
--- a/src/components/atoms/layout/notice.tsx
+++ b/src/components/atoms/notice/notice.tsx
@@ -1,17 +1,17 @@
-import { FC, HTMLAttributes } from 'react';
+import type { FC, HTMLAttributes, ReactNode } from 'react';
import styles from './notice.module.scss';
export type NoticeKind = 'error' | 'info' | 'success' | 'warning';
export type NoticeProps = Omit<HTMLAttributes<HTMLElement>, 'children'> & {
/**
- * The notice kind.
+ * The notice body.
*/
- kind: NoticeKind;
+ children: ReactNode;
/**
- * The notice body.
+ * The notice kind.
*/
- message: string;
+ kind: NoticeKind;
};
/**
@@ -21,16 +21,16 @@ export type NoticeProps = Omit<HTMLAttributes<HTMLElement>, 'children'> & {
*/
export const Notice: FC<NoticeProps> = ({
className = '',
+ children,
kind,
- message,
...props
}) => {
- const kindClass = `wrapper--${kind}`;
- const noticeClass = `${styles.wrapper} ${styles[kindClass]} ${className}`;
+ const kindClass = styles[`notice--${kind}`];
+ const noticeClass = `${styles.notice} ${kindClass} ${className}`;
return (
<div {...props} className={noticeClass}>
- {message}
+ {children}
</div>
);
};
diff --git a/src/components/templates/page/page-layout.tsx b/src/components/templates/page/page-layout.tsx
index dfd9353..ee3fd3a 100644
--- a/src/components/templates/page/page-layout.tsx
+++ b/src/components/templates/page/page-layout.tsx
@@ -281,11 +281,9 @@ export const PageLayout: FC<PageLayoutProps> = ({
depth={2}
Notice={
commentStatus?.isReply ? (
- <Notice
- className={styles.notice}
- kind={commentStatus.kind}
- message={commentStatus.message}
- />
+ <Notice className={styles.notice} kind={commentStatus.kind}>
+ {commentStatus.message}
+ </Notice>
) : null
}
saveComment={saveComment}
@@ -307,11 +305,9 @@ export const PageLayout: FC<PageLayoutProps> = ({
title={commentFormTitle}
Notice={
commentStatus && !commentStatus.isReply ? (
- <Notice
- className={styles.notice}
- kind={commentStatus.kind}
- message={commentStatus.message}
- />
+ <Notice className={styles.notice} kind={commentStatus.kind}>
+ {commentStatus.message}
+ </Notice>
) : null
}
/>
diff --git a/src/pages/blog/index.tsx b/src/pages/blog/index.tsx
index 7f6c540..7eef2f0 100644
--- a/src/pages/blog/index.tsx
+++ b/src/pages/blog/index.tsx
@@ -196,12 +196,13 @@ const BlogPage: NextPageWithLayout<BlogPageProps> = ({
<Notice
// eslint-disable-next-line react/jsx-no-literals -- Kind allowed
kind="error"
- message={intl.formatMessage({
+ >
+ {intl.formatMessage({
defaultMessage: 'Failed to load.',
description: 'BlogPage: failed to load text',
id: 'C/XGkH',
})}
- />
+ </Notice>
) : null}
</PageLayout>
</>
diff --git a/src/pages/contact.tsx b/src/pages/contact.tsx
index d187a93..679896c 100644
--- a/src/pages/contact.tsx
+++ b/src/pages/contact.tsx
@@ -158,11 +158,9 @@ const ContactPage: NextPageWithLayout = () => {
sendMail={submitMail}
Notice={
statusMessage ? (
- <Notice
- kind={statusKind}
- message={statusMessage}
- className={styles.notice}
- />
+ <Notice className={styles.notice} kind={statusKind}>
+ {statusMessage}
+ </Notice>
) : undefined
}
/>
diff --git a/src/pages/recherche/index.tsx b/src/pages/recherche/index.tsx
index 5acf352..54244b1 100644
--- a/src/pages/recherche/index.tsx
+++ b/src/pages/recherche/index.tsx
@@ -222,12 +222,13 @@ const SearchPage: NextPageWithLayout<SearchPageProps> = ({
<Notice
// eslint-disable-next-line react/jsx-no-literals -- Kind allowed
kind="error"
- message={intl.formatMessage({
+ >
+ {intl.formatMessage({
defaultMessage: 'Failed to load.',
description: 'SearchPage: failed to load text',
id: 'fOe8rH',
})}
- />
+ </Notice>
) : null}
</PageLayout>
</>