-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ButtonGroup): Add new component ButtonGroup (#382)
- Loading branch information
Showing
4 changed files
with
130 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import React from 'react' | ||
import { ButtonGroup } from './ButtonGroup' | ||
import { Button } from '../Button/Button' | ||
import { Link } from '../Link/Link' | ||
|
||
export default { | ||
title: 'ButtonGroup', | ||
component: ButtonGroup, | ||
parameters: { | ||
info: ` | ||
USWDS 2.0 ButtonGroup component | ||
Source: https://designsystem.digital.gov/components/button-groups/ | ||
`, | ||
}, | ||
} | ||
|
||
export const Default = (): React.ReactElement => ( | ||
<ButtonGroup type="default"> | ||
<Link href="#" className="usa-button usa-button--outline"> | ||
Back | ||
</Link> | ||
<Button type="button">Continue</Button> | ||
</ButtonGroup> | ||
) | ||
|
||
export const Segmented = (): React.ReactElement => ( | ||
<ButtonGroup type="segmented"> | ||
<Button type="button">Map</Button> | ||
<Button type="button" outline> | ||
Satellite | ||
</Button> | ||
<Button type="button" outline> | ||
Hybrid | ||
</Button> | ||
</ButtonGroup> | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import React from 'react' | ||
import { render } from '@testing-library/react' | ||
|
||
import { ButtonGroup } from './ButtonGroup' | ||
|
||
describe('ButtonGroup component', () => { | ||
it('renders without errors', () => { | ||
const { getByRole } = render( | ||
<ButtonGroup> | ||
<button>This is a button</button> | ||
</ButtonGroup> | ||
) | ||
expect(getByRole('list')).toBeInTheDocument() | ||
}) | ||
|
||
it('renders its children', () => { | ||
const { getByText } = render(<ButtonGroup>My Text Input</ButtonGroup>) | ||
expect(getByText('My Text Input')).toBeInTheDocument() | ||
}) | ||
|
||
it('displays with expected styles', () => { | ||
const { getByRole } = render( | ||
<ButtonGroup> | ||
<button>This is a button</button> | ||
</ButtonGroup> | ||
) | ||
expect(getByRole('list')).toHaveClass('usa-button-group') | ||
}) | ||
|
||
it('handles type prop - default', () => { | ||
const { getByRole } = render( | ||
<ButtonGroup type="default">A child</ButtonGroup> | ||
) | ||
expect(getByRole('list')).not.toHaveClass('usa-button-group--segmented') | ||
}) | ||
|
||
it('handles type prop - segmented', () => { | ||
const { getByRole } = render( | ||
<ButtonGroup type="segmented">A child</ButtonGroup> | ||
) | ||
expect(getByRole('list')).toHaveClass('usa-button-group--segmented') | ||
}) | ||
|
||
it('displays list elements with expected styles', () => { | ||
const { getByRole } = render(<ButtonGroup>A child</ButtonGroup>) | ||
expect(getByRole('listitem')).toBeInTheDocument() | ||
expect(getByRole('listitem')).toHaveClass('usa-button-group__item') | ||
}) | ||
|
||
it('renders a list item wrapping each child', () => { | ||
const { getAllByRole } = render( | ||
<ButtonGroup> | ||
<p>first child</p> | ||
<p>second child</p> | ||
</ButtonGroup> | ||
) | ||
const listItems = getAllByRole('listitem') | ||
expect(listItems[0]).toHaveTextContent('first child') | ||
expect(listItems[1]).toHaveTextContent('second child') | ||
expect(listItems.length).toBe(2) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import React from 'react' | ||
import classnames from 'classnames' | ||
|
||
type ButtonGroupProps = { | ||
children: React.ReactNode | ||
className?: string | ||
type?: 'default' | 'segmented' | ||
} & JSX.IntrinsicElements['ul'] | ||
|
||
export const ButtonGroup = (props: ButtonGroupProps): React.ReactElement => { | ||
const { className, children, type = 'default', ...restProps } = props | ||
|
||
const classes = classnames( | ||
'usa-button-group', | ||
{ | ||
'usa-button-group--segmented': type == 'segmented', | ||
}, | ||
className | ||
) | ||
|
||
return ( | ||
<ul className={classes} {...restProps}> | ||
{React.Children.map(children, (child: React.ReactNode) => { | ||
return <li className="usa-button-group__item">{child}</li> | ||
})} | ||
</ul> | ||
) | ||
} | ||
|
||
export default ButtonGroup |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters