Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WithPlaceholder component #283

Merged
merged 14 commits into from
Sep 15, 2023
52 changes: 52 additions & 0 deletions src/components/content/WithPlaceholder/WithPlacehoder.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import {
Controls,
Description,
Primary,
Stories,
Subheading,
Title,
} from '@storybook/blocks'
import { Meta, StoryObj } from '@storybook/react'
import { WithPlaceholder } from './WithPlaceholder'

const children = {
options: ['Null', 'Undefined', 'Empty text', 'ReactNode'],
mapping: {
Null: null,
Undefined: undefined,
'Empty text': '',
ReactNode: 'John Doe',
},
control: { type: 'select' },
}

const meta = {
title: 'Components/Content/WithPlaceHolder',
component: WithPlaceholder,
args: {
placeholder: '-',
children: 'John Doe',
},
argTypes: {
children,
},
parameters: {
docs: {
page: () => (
<>
<Title />
<Description />
<Primary />
<Subheading>Props</Subheading>
<Controls />
<Stories />
</>
),
},
},
} satisfies Meta<typeof WithPlaceholder>
export default meta

type Story = StoryObj<typeof meta>

export const Default: Story = {}
26 changes: 26 additions & 0 deletions src/components/content/WithPlaceholder/WithPlaceholder.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { PropsWithChildren, ReactNode } from 'react'

export type WithPlaceholderProps = PropsWithChildren<{
/**
* Defines the placeholder to be rendered if the children is not valid.
*/
placeholder?: ReactNode
}>

/**
* This component validates the content and displays a placeholder if the content is empty, null or undefined.
*/
export function WithPlaceholder({
placeholder = '-',
children,
}: WithPlaceholderProps) {
return (
<>
{typeof children === 'number'
? isNaN(children)
? placeholder
: children
: children || placeholder}
</>
)
}
1 change: 1 addition & 0 deletions src/components/content/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ export * from './DescriptionItem/DescriptionItemTitle'
export * from './DescriptionItem/DescriptionItemContent'
export * from './DescriptionItem/LoadingDescriptionItem'
export * from './DescriptionItem/types'
export * from './WithPlaceholder/WithPlaceholder'