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

feat: Checkbox Tile Variant #1104

Merged
merged 7 commits into from
Apr 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions src/components/forms/Checkbox/Checkbox.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,26 @@ export const withRichLabel = (): React.ReactElement => (
label={<strong>My Checkbox</strong>}
/>
)

export const WithLabelDescription = (): React.ReactElement => (
<Checkbox
id="checkbox"
name="checkbox"
label="My Checkbox"
labelDescription="This is optional text that can be used to describe the label in more detail."
/>
)

export const tile = (): React.ReactElement => (
<Checkbox id="checkbox" name="checkbox" label="My Checkbox" tile />
)

export const tileWithLabelDescription = (): React.ReactElement => (
<Checkbox
id="checkbox"
name="checkbox"
label="My Checkbox"
labelDescription="This is optional text that can be used to describe the label in more detail."
tile
/>
)
29 changes: 29 additions & 0 deletions src/components/forms/Checkbox/Checkbox.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,32 @@ describe('Checkbox component', () => {
expect(queryByTestId('checkbox')).toBeInTheDocument()
})
})

it('renders tiled checkbox', () => {
const { queryByLabelText } = render(
<Checkbox
id="input-checkbox"
name="input-checkbox"
label="My checkbox"
tile
/>
)
expect(queryByLabelText('My checkbox')).toHaveClass(
'usa-checkbox__input usa-checkbox__input--tile'
)
})

it('renders label description', () => {
const { queryByText } = render(
<Checkbox
id="input-checkbox"
name="input-checkbox"
label="My checkbox"
labelDescription="Label description"
tile
/>
)
expect(queryByText('Label description')).toHaveClass(
'usa-checkbox__label-description'
)
})
14 changes: 13 additions & 1 deletion src/components/forms/Checkbox/Checkbox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ interface CheckboxProps {
| React.RefObject<HTMLInputElement>
| null
| undefined
tile?: boolean
labelDescription?: React.ReactNode
brandonlenz marked this conversation as resolved.
Show resolved Hide resolved
}

export const Checkbox = ({
Expand All @@ -20,14 +22,19 @@ export const Checkbox = ({
className,
label,
inputRef,
tile,
labelDescription,
...inputProps
}: CheckboxProps & JSX.IntrinsicElements['input']): React.ReactElement => {
const classes = classnames('usa-checkbox', className)
const checkboxClasses = classnames('usa-checkbox__input', {
'usa-checkbox__input--tile': tile,
})

return (
<div data-testid="checkbox" className={classes}>
<input
className="usa-checkbox__input"
className={checkboxClasses}
id={id}
type="checkbox"
name={name}
Expand All @@ -36,6 +43,11 @@ export const Checkbox = ({
/>
<label className="usa-checkbox__label" htmlFor={id}>
{label}
{labelDescription && (
<span className="usa-checkbox__label-description">
{labelDescription}
</span>
)}
</label>
</div>
)
Expand Down