From 98b18014ba69d0c4860c34282f7adb41fb789d37 Mon Sep 17 00:00:00 2001
From: Mollpo <97024183+mollpo@users.noreply.github.com>
Date: Fri, 22 Sep 2023 09:20:52 +0200
Subject: [PATCH] change alert and actions to tsx instead of mdx (#284)
* change alert and actions to tsx instead of mdx
* lint fix
---
src/components/action/Actions.stories.mdx | 64 -----
src/components/action/Actions.stories.tsx | 70 +++++
src/components/action/Actions.tsx | 2 +-
src/components/alert/Alert.stories.mdx | 243 -----------------
src/components/alert/Alert.stories.tsx | 252 ++++++++++++++++++
.../content/__test__/WithPlaceholder.test.tsx | 90 +++++++
6 files changed, 413 insertions(+), 308 deletions(-)
delete mode 100644 src/components/action/Actions.stories.mdx
create mode 100644 src/components/action/Actions.stories.tsx
delete mode 100644 src/components/alert/Alert.stories.mdx
create mode 100644 src/components/alert/Alert.stories.tsx
create mode 100644 src/components/content/__test__/WithPlaceholder.test.tsx
diff --git a/src/components/action/Actions.stories.mdx b/src/components/action/Actions.stories.mdx
deleted file mode 100644
index 0f20992b..00000000
--- a/src/components/action/Actions.stories.mdx
+++ /dev/null
@@ -1,64 +0,0 @@
-import { Meta, Story, Canvas, ArgsTable } from '@storybook/addon-docs'
-import { Theme } from '../../../.storybook/components'
-import { Button, ButtonVariant } from '../button'
-import { Tone } from '../types'
-import { Actions } from './Actions'
-
-
-
-# Actions
-
-The `Actions` component positions children in a flex container.
-It makes sure that there is space between each item and that they stack on mobile.
-This is very handy in a content area or a dialog, where you have two buttons next to each other.
-
-export const Template = ({ children, ...args }) => (
- {children}
-)
-
-
-
-### Props
-
-
-
-
-
-
-
-## Multiple children
-
-
diff --git a/src/components/action/Actions.stories.tsx b/src/components/action/Actions.stories.tsx
new file mode 100644
index 00000000..6e4cd9b4
--- /dev/null
+++ b/src/components/action/Actions.stories.tsx
@@ -0,0 +1,70 @@
+import {
+ Controls,
+ Markdown,
+ Primary,
+ Stories,
+ Subheading,
+ Title,
+} from '@storybook/addon-docs'
+import { Meta, StoryObj } from '@storybook/react'
+import { Button, ButtonVariant } from '../button'
+import { Tone } from '../types'
+import { Theme } from '../../../.storybook/components'
+import { Actions } from './Actions'
+
+const children = {
+ options: ['One button', 'Two buttons'],
+ mapping: {
+ 'One button': ,
+ 'Two buttons': (
+ <>
+
+
+ >
+ ),
+ },
+ control: { type: 'select' },
+}
+
+const meta = {
+ component: Actions,
+ title: 'Components/Actions/Actions',
+ args: {
+ children: children.mapping['One button'],
+ },
+ argTypes: {
+ children,
+ },
+ parameters: {
+ docs: {
+ page: () => (
+ <>
+
+
+ The `Actions` component positions children in a flex container. It
+ makes sure that there is space between each item and that they stack
+ on mobile. This is very handy in a content area or a dialog, where
+ you have two buttons next to each other.
+
+
+ Props
+
+
+
+ >
+ ),
+ },
+ },
+} satisfies Meta
+
+export default meta
+type Story = StoryObj
+
+export const Default: Story = {}
+export const MultipleChildren: Story = {
+ args: {
+ children: { ...children.mapping['Two buttons'] },
+ },
+}
diff --git a/src/components/action/Actions.tsx b/src/components/action/Actions.tsx
index a6e904eb..af80509a 100644
--- a/src/components/action/Actions.tsx
+++ b/src/components/action/Actions.tsx
@@ -10,7 +10,7 @@ export type ActionsProps = ClassNameProps & {
**/
position?: ActionsPosition
/**
- * Defineds the children to be rendered.
+ * Defines the children to be rendered.
*/
children?: ReactNode
}
diff --git a/src/components/alert/Alert.stories.mdx b/src/components/alert/Alert.stories.mdx
deleted file mode 100644
index d2a75bed..00000000
--- a/src/components/alert/Alert.stories.mdx
+++ /dev/null
@@ -1,243 +0,0 @@
-import IconWarning from '@aboutbits/react-material-icons/dist/IconWarning'
-import IconCheck from '@aboutbits/react-material-icons/dist/IconCheck'
-import IconInfo from '@aboutbits/react-material-icons/dist/IconInfo'
-import { Meta, Story, Canvas, ArgsTable } from '@storybook/addon-docs'
-import { Theme } from '../../../.storybook/components'
-import { Size, Tone } from '../types'
-import { ButtonVariant } from '../button/types'
-import { Button } from '../button'
-import { Alert } from './Alert'
-import { AlertActionsPosition } from './types'
-import {
- AlertSuccess,
- AlertWarning,
- AlertCritical,
- AlertInformative,
-} from './ConvenientAlerts'
-
-
-
-# Alert
-
-This alert component can be used to highlight a message to the user.
-It supports different types of tones. It is possible to specify an icon, a title, actions and the position of the actions.
-
-The library also provides convenient alert components with predefined tone and icon.
-
-export const Template = ({ children, ...args }) => (
- {children}
-)
-
-export const DissmissCriticalButton = (
-
-)
-
-export const YesNoCriticalButtons = (
- <>
-
-
- >
-)
-
-
-
-### Props
-
-
-
-
-
-
-
-## Tone
-
-
-
-### Icon
-
-
-
-### Title
-
-
-
-## Actions
-
-
-
-
-
-
-
-## Very Long Message
-
-
-
-## Convenient alert components
-
-
diff --git a/src/components/alert/Alert.stories.tsx b/src/components/alert/Alert.stories.tsx
new file mode 100644
index 00000000..eba7c4cc
--- /dev/null
+++ b/src/components/alert/Alert.stories.tsx
@@ -0,0 +1,252 @@
+import {
+ Controls,
+ Markdown,
+ Primary,
+ Stories,
+ Subheading,
+ Title,
+} from '@storybook/addon-docs'
+import { Meta, StoryObj } from '@storybook/react'
+import IconWarning from '@aboutbits/react-material-icons/dist/IconWarning'
+import IconCheck from '@aboutbits/react-material-icons/dist/IconCheck'
+import IconInfo from '@aboutbits/react-material-icons/dist/IconInfo'
+import { Button, ButtonVariant } from '../button'
+import { Size, Tone } from '../types'
+import { Theme } from '../../../.storybook/components'
+import { Alert } from './Alert'
+import { AlertActionsPosition } from './types'
+import {
+ AlertCritical,
+ AlertInformative,
+ AlertSuccess,
+ AlertWarning,
+} from './ConvenientAlerts'
+
+const icons = {
+ options: ['None', 'Warning', 'Check', 'Info'],
+ mapping: {
+ None: undefined,
+ Warning: IconWarning,
+ Check: IconCheck,
+ Info: IconInfo,
+ },
+ control: { type: 'select' },
+}
+const DissmissCriticalButton = (
+
+)
+
+const DissmissSuccessButton = (
+
+)
+
+const YesNoCriticalButtons = (
+ <>
+
+
+ >
+)
+
+const actions = {
+ options: ['None', 'DismissCritical', 'YesNoCritical'],
+ mapping: {
+ None: undefined,
+ DismissCritical: DissmissCriticalButton,
+ YesNoCritical: YesNoCriticalButtons,
+ },
+ control: { type: 'select' },
+}
+
+const meta = {
+ component: Alert,
+ title: 'Components/Alert/Alert',
+ args: {
+ children:
+ 'This alert component can be used to highlight a message to the user.',
+ tone: Tone.Critical,
+ icon: IconWarning,
+ },
+ argTypes: {
+ title: { control: 'text' },
+ icon: icons,
+ actions,
+ },
+ parameters: {
+ docs: {
+ page: () => (
+ <>
+
+
+ This alert component can be used to highlight a message to the user.
+ It supports different types of tones. It is possible to specify an
+ icon, a title, actions and the position of the actions. The library
+ also provides convenient alert components with predefined tone and
+ icon.
+
+
+ Props
+
+
+
+ >
+ ),
+ },
+ },
+} satisfies Meta
+
+export default meta
+type Story = StoryObj
+
+export const Default: Story = {}
+
+export const Tones: Story = {
+ args: {
+ icon: undefined,
+ },
+ render: (args) => (
+
+
+ Primary message
+
+
+ Neutral message
+
+
+ Warning message
+
+
+ Critical message
+
+
+ Success message
+
+
+ Informative message
+
+
+ ),
+}
+
+export const ConvinientAlertComponents: Story = {
+ render: () => (
+
+
Success message
+
Warning message
+
Critical message
+
Informative message
+
+ ),
+ parameters: {
+ controls: {
+ disable: true,
+ },
+ },
+}
+
+export const ActionsResponsive: Story = {
+ args: {
+ icon: IconCheck,
+ tone: Tone.Success,
+ actions: DissmissSuccessButton,
+ actionsPosition: AlertActionsPosition.Responsive,
+ },
+ render: (args) => (
+
+ Success message with actions responsive
+
+ ),
+ argTypes: {
+ children: { control: { disabled: true } },
+ },
+}
+
+export const ActionsFixedRight: Story = {
+ args: {
+ icon: IconCheck,
+ tone: Tone.Success,
+ actions: DissmissSuccessButton,
+ actionsPosition: AlertActionsPosition.FixedRight,
+ },
+ render: (args) => (
+
+ Success message with actions fixed to the right
+
+ ),
+ argTypes: {
+ children: {
+ table: {
+ disable: true,
+ },
+ },
+ },
+}
+
+export const ActionsFixedBottom: Story = {
+ args: {
+ icon: IconCheck,
+ tone: Tone.Success,
+ actions: DissmissSuccessButton,
+ actionsPosition: AlertActionsPosition.FixedBottom,
+ },
+ render: (args) => (
+
+ Success message with actions fixed to the right
+
+ ),
+ argTypes: {
+ children: {
+ table: {
+ disable: true,
+ },
+ },
+ },
+}
+
+export const VeryLongMessage: Story = {
+ args: {
+ icon: IconWarning,
+ tone: Tone.Critical,
+ },
+ render: (args) => (
+
+ Duis mauris ligula, aliquam id maximus at, aliquam ut dui. Morbi ac neque
+ mattis libero cursus lobortis quis ac urna. Nullam vel augue vitae lacus
+ fermentum ullamcorper eu eget sapien.
+
+ ),
+ argTypes: {
+ children: {
+ table: {
+ disable: true,
+ },
+ },
+ },
+}
+
+export const WithTitle: Story = {
+ args: { title: 'This is the title' },
+}
diff --git a/src/components/content/__test__/WithPlaceholder.test.tsx b/src/components/content/__test__/WithPlaceholder.test.tsx
new file mode 100644
index 00000000..10676493
--- /dev/null
+++ b/src/components/content/__test__/WithPlaceholder.test.tsx
@@ -0,0 +1,90 @@
+import { queryByText, render } from '@testing-library/react'
+import { ReactNode } from 'react'
+import { WithPlaceholder } from '../WithPlaceholder/WithPlaceholder'
+
+const expectTextContent = (
+ children: ReactNode,
+ text: number | string,
+ placeholder?: number | string,
+) => {
+ const { container } = render(
+ {children},
+ )
+ expect(queryByText(container, text)).toHaveTextContent(text.toString())
+}
+
+const expectNotTextContent = (
+ children: ReactNode,
+ text: number | string,
+ placeholder?: number | string,
+) => {
+ const { container } = render(
+ {children},
+ )
+ expect(queryByText(container, text)).toBeNull()
+}
+
+const placeholder = '/'
+
+const expectPlaceholder = (children: ReactNode) => {
+ expectTextContent(children, placeholder, placeholder)
+}
+
+const expectNotPlaceholder = (children: ReactNode) => {
+ expectNotTextContent(children, placeholder, placeholder)
+}
+
+describe('WithPlaceholder', () => {
+ it('should render the placeholder if NaN is passed', () => {
+ expectPlaceholder(NaN)
+ })
+ it('should render the placeholder if undefined is passed', () => {
+ expectPlaceholder(undefined)
+ })
+ it('should render the placeholder if null is passed', () => {
+ expectPlaceholder(null)
+ })
+ it('should render the placeholder if an empty string is passed', () => {
+ expectPlaceholder('')
+ })
+ it('should render 0 if 0 is passed', () => {
+ const value = 0
+ expectTextContent(value, value)
+ })
+ it('should render 1 if 1 is passed', () => {
+ const value = 1
+ expectTextContent(value, value)
+ })
+ it('should render -1 if -1 is passed', () => {
+ const value = -1
+ expectTextContent(value, value)
+ })
+ it('should render the given string if the string is not empty', () => {
+ const value = 'test'
+ expectTextContent(value, value)
+ })
+ it('should render the given div containing a string', () => {
+ const value = 'test'
+ expectTextContent({value}
, value)
+ })
+ it('should render the given div containing 0', () => {
+ const value = 0
+ expectTextContent({value}
, value)
+ })
+ it('should render the given div even if it contains NaN', () => {
+ const value = NaN
+ expectTextContent({value}
, value)
+ })
+ it('should render the given div even if it contains undefined', () => {
+ const value = undefined
+ expectNotPlaceholder({value}
)
+ })
+ it('should render the given div even if it contains null', () => {
+ const value = null
+ expectNotPlaceholder({value}
)
+ })
+ it('should render the given div even if it contains an empty string', () => {
+ const value = ''
+ expectNotPlaceholder({value}
)
+ })
+})