Skip to content

Commit

Permalink
Alllow storybook to infer prop defaults
Browse files Browse the repository at this point in the history
- Destruccuting props as args in functional component signatures allows storybook to infer default values of props
  • Loading branch information
brandonlenz committed Mar 9, 2021
1 parent 384a2e6 commit 27f16c2
Show file tree
Hide file tree
Showing 43 changed files with 258 additions and 328 deletions.
15 changes: 9 additions & 6 deletions src/components/Accordion/Accordion.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,14 @@ interface AccordionProps {
className?: string
}

export const AccordionItem = (props: AccordionItem): React.ReactElement => {
const { title, id, content, expanded, className, handleToggle } = props

export const AccordionItem = ({
title,
id,
content,
expanded,
className,
handleToggle
}: AccordionItem): React.ReactElement => {
const headingClasses = classnames('usa-accordion__heading', className)
const contentClasses = classnames(
'usa-accordion__content',
Expand Down Expand Up @@ -50,9 +55,7 @@ export const AccordionItem = (props: AccordionItem): React.ReactElement => {
)
}

export const Accordion = (props: AccordionProps): React.ReactElement => {
const { bordered, items, className } = props

export const Accordion = ({ bordered, items, className }: AccordionProps): React.ReactElement => {
const [openItems, setOpenState] = useState(
items.filter((i) => !!i.expanded).map((i) => i.id)
)
Expand Down
7 changes: 2 additions & 5 deletions src/components/Button/Button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,7 @@ interface ButtonProps {
unstyled?: boolean
}

export const Button = (
props: ButtonProps & JSX.IntrinsicElements['button']
): React.ReactElement => {
const {
export const Button = ({
type,
children,
secondary,
Expand All @@ -45,7 +42,7 @@ export const Button = (
onClick,
className,
...defaultProps
} = props
}: ButtonProps & JSX.IntrinsicElements['button']): React.ReactElement => {

if (big) {
deprecationWarning('Button property big is deprecated. Use size')
Expand Down
9 changes: 6 additions & 3 deletions src/components/ButtonGroup/ButtonGroup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@ type ButtonGroupProps = {
type?: 'default' | 'segmented'
} & JSX.IntrinsicElements['ul']

export const ButtonGroup = (props: ButtonGroupProps): React.ReactElement => {
const { className, children, type = 'default', ...restProps } = props

export const ButtonGroup = ({
className,
children,
type = 'default',
...restProps
}: ButtonGroupProps): React.ReactElement => {
const classes = classnames(
'usa-button-group',
{
Expand Down
7 changes: 3 additions & 4 deletions src/components/Footer/SocialLinks/SocialLinks.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@ type SocialLinksProps = {
links: React.ReactNode[]
}

export const SocialLinks = (
props: SocialLinksProps & JSX.IntrinsicElements['div']
): React.ReactElement => {
const { className, links } = props
export const SocialLinks = ({
className, links
}: SocialLinksProps & JSX.IntrinsicElements['div']): React.ReactElement => {
const classes = classnames(
'usa-footer__social-links grid-row grid-gap-1',
className
Expand Down
15 changes: 6 additions & 9 deletions src/components/GovBanner/GovBanner.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -101,15 +101,12 @@ interface GovBannerProps {
language?: Language
}

export const GovBanner = (
props: GovBannerProps & JSX.IntrinsicElements['section']
): React.ReactElement => {
const {
tld = '.gov',
language = 'english',
className,
...sectionProps
} = props
export const GovBanner = ({
tld = '.gov',
language = 'english',
className,
...sectionProps
}: GovBannerProps & JSX.IntrinsicElements['section']): React.ReactElement => {
const [isOpen, setOpenState] = useState(false)

const classes = classnames('usa-banner', className)
Expand Down
27 changes: 12 additions & 15 deletions src/components/Search/Search.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,18 @@ interface SearchInputProps {
placeholder?: string
}

export const Search = (
props: SearchInputProps & OptionalFormProps
): React.ReactElement => {
const {
onSubmit,
inputId = 'search-field',
inputName = 'search',
size,
big,
small,
label = 'Search',
className,
placeholder,
...formProps
} = props
export const Search = ({
onSubmit,
inputId = 'search-field',
inputName = 'search',
size,
big,
small,
label = 'Search',
className,
placeholder,
...formProps
}: SearchInputProps & OptionalFormProps): React.ReactElement => {
if (big) {
deprecationWarning('Search property big is deprecated. Use size')
}
Expand Down
6 changes: 3 additions & 3 deletions src/components/Table/Table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ interface TableProps {
fixed?: boolean
}

export const Table = (props: TableProps): React.ReactElement => {
const { bordered, caption, children, fullWidth, fixed } = props

export const Table = ({
bordered, caption, children, fullWidth, fixed
}: TableProps): React.ReactElement => {
const classes = classnames('usa-table', {
'usa-table--borderless': !bordered,
[`${styles.fullwidth}`]: fullWidth,
Expand Down
7 changes: 3 additions & 4 deletions src/components/Tag/Tag.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@ interface TagProps {
background?: string
}

export const Tag = (
props: TagProps & JSX.IntrinsicElements['span']
): React.ReactElement => {
const { children, background, className, ...spanProps } = props
export const Tag = ({
children, background, className, ...spanProps
}: TagProps & JSX.IntrinsicElements['span']): React.ReactElement => {

const style: React.CSSProperties = {}
if (background) {
Expand Down
10 changes: 6 additions & 4 deletions src/components/breadcrumb/Breadcrumb/Breadcrumb.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ export interface BreadcrumbProps {
current?: boolean
}

export const Breadcrumb = (
props: BreadcrumbProps & JSX.IntrinsicElements['li']
): React.ReactElement => {
const { children, current = false, className, ...listItemProps } = props
export const Breadcrumb = ({
children,
current = false,
className,
...listItemProps
}: BreadcrumbProps & JSX.IntrinsicElements['li']): React.ReactElement => {
const classes = classnames(
'usa-breadcrumb__list-item',
{
Expand Down
17 changes: 7 additions & 10 deletions src/components/breadcrumb/BreadcrumbBar/BreadcrumbBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,13 @@ interface BreadcrumbBarProps {
listProps?: JSX.IntrinsicElements['ol']
}

export const BreadcrumbBar = (
props: BreadcrumbBarProps
): React.ReactElement => {
const {
variant = 'default',
children,
className,
navProps,
listProps,
} = props
export const BreadcrumbBar = ({
variant = 'default',
children,
className,
navProps,
listProps,
}: BreadcrumbBarProps): React.ReactElement => {
const classes = classnames(
'usa-breadcrumb',
{
Expand Down
19 changes: 12 additions & 7 deletions src/components/breadcrumb/BreadcrumbLink/BreadcrumbLink.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,20 @@ import {

export function BreadcrumbLink(props: DefaultLinkProps): React.ReactElement
export function BreadcrumbLink<T>(props: CustomLinkProps<T>): React.ReactElement
export function BreadcrumbLink<FCProps = DefaultLinkProps>(
props: DefaultLinkProps | CustomLinkProps<FCProps>
): React.ReactElement {
const { className } = props
export function BreadcrumbLink<FCProps = DefaultLinkProps>({
className,
...passThroughProps
}: DefaultLinkProps | CustomLinkProps<FCProps>): React.ReactElement {
const classes = classnames(className, 'usa-breadcrumb__link')

if (isCustomProps(props)) {
return <Link<FCProps> {...props} className={classes} variant="unstyled" />
const linkProps = {
...passThroughProps,
className: classes
} as DefaultLinkProps | CustomLinkProps<FCProps>

if (isCustomProps(linkProps)) {
return <Link<FCProps> {...linkProps} variant="unstyled" />
}

return <Link {...props} className={classes} variant="unstyled" />
return <Link {...linkProps} variant="unstyled" />
}
22 changes: 9 additions & 13 deletions src/components/card/Card/Card.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,15 @@ interface CardProps {
containerProps?: React.HTMLAttributes<HTMLDivElement>
}

export const Card = (
props: CardProps & JSX.IntrinsicElements['li'] & GridLayoutProp
): React.ReactElement => {
const {
layout = 'standardDefault',
headerFirst,
children,
className,
gridLayout,
containerProps,
...liProps
} = props

export const Card = ({
layout = 'standardDefault',
headerFirst,
children,
className,
gridLayout,
containerProps,
...liProps
}: CardProps & JSX.IntrinsicElements['li'] & GridLayoutProp): React.ReactElement => {
const { className: containerClass, ...restContainerProps } =
containerProps || {}

Expand Down
8 changes: 3 additions & 5 deletions src/components/card/CardBody/CardBody.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import React from 'react'
import classnames from 'classnames'

export const CardBody = (
props: { exdent?: boolean } & JSX.IntrinsicElements['div']
): React.ReactElement => {
const { exdent, children, className, ...bodyProps } = props

export const CardBody = ({
exdent, children, className, ...bodyProps
}: { exdent?: boolean } & JSX.IntrinsicElements['div']): React.ReactElement => {
const classes = classnames(
'usa-card__body',
{
Expand Down
8 changes: 3 additions & 5 deletions src/components/card/CardFooter/CardFooter.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import React from 'react'
import classnames from 'classnames'

export const CardFooter = (
props: { exdent?: boolean } & JSX.IntrinsicElements['div']
): React.ReactElement => {
const { exdent, children, className, ...footerProps } = props

export const CardFooter = ({
exdent, children, className, ...footerProps
}: { exdent?: boolean } & JSX.IntrinsicElements['div']): React.ReactElement => {
const classes = classnames(
'usa-card__footer',
{
Expand Down
8 changes: 3 additions & 5 deletions src/components/card/CardGroup/CardGroup.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import React from 'react'
import classnames from 'classnames'

export const CardGroup = (
props: JSX.IntrinsicElements['ul']
): React.ReactElement => {
const { children, className, ...ulProps } = props

export const CardGroup = ({
children, className, ...ulProps
}: JSX.IntrinsicElements['ul']): React.ReactElement => {
const classes = classnames('usa-card-group', className)

return (
Expand Down
7 changes: 3 additions & 4 deletions src/components/card/CardHeader/CardHeader.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import React from 'react'
import classnames from 'classnames'

export const CardHeader = (
props: { exdent?: boolean } & JSX.IntrinsicElements['header']
): React.ReactElement => {
const { exdent, children, className, ...headerProps } = props
export const CardHeader = ({
exdent, children, className, ...headerProps
}: { exdent?: boolean } & JSX.IntrinsicElements['header']): React.ReactElement => {

const classes = classnames(
'usa-card__header',
Expand Down
20 changes: 8 additions & 12 deletions src/components/card/CardMedia/CardMedia.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,14 @@ interface CardMediaProps {
children: React.ReactNode
}

export const CardMedia = (
props: CardMediaProps & JSX.IntrinsicElements['div']
): React.ReactElement => {
const {
exdent,
inset,
imageClass,
children,
className,
...mediaProps
} = props

export const CardMedia = ({
exdent,
inset,
imageClass,
children,
className,
...mediaProps
}: CardMediaProps & JSX.IntrinsicElements['div']): React.ReactElement => {
const classes = classnames(
'usa-card__media',
{
Expand Down
8 changes: 3 additions & 5 deletions src/components/forms/Checkbox/Checkbox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,9 @@ interface CheckboxProps {
| undefined
}

export const Checkbox = (
props: CheckboxProps & JSX.IntrinsicElements['input']
): React.ReactElement => {
const { id, name, className, label, inputRef, ...inputProps } = props

export const Checkbox = ({
id, name, className, label, inputRef, ...inputProps
}: CheckboxProps & JSX.IntrinsicElements['input']): React.ReactElement => {
const classes = classnames('usa-checkbox', className)

return (
Expand Down
Loading

0 comments on commit 27f16c2

Please sign in to comment.