-
Notifications
You must be signed in to change notification settings - Fork 178
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(components): add new checkbox component and story to atoms (#15137)
Include the new checkbox component in the library's ODD-specific atoms. Closes [PLAT-299](https://opentrons.atlassian.net/browse/PLAT-299)
- Loading branch information
1 parent
278510b
commit 0e297ee
Showing
5 changed files
with
168 additions
and
4 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,24 @@ | ||
import { Checkbox } from './index' | ||
|
||
import type { StoryObj, Meta } from '@storybook/react' | ||
|
||
const meta: Meta<typeof Checkbox> = { | ||
title: 'ODD/Atoms/Checkbox', | ||
component: Checkbox, | ||
} | ||
|
||
type Story = StoryObj<typeof Checkbox> | ||
|
||
export const Basic: Story = { | ||
args: { | ||
isChecked: true, | ||
labelText: 'Button Text', | ||
onClick: () => { | ||
console.log('clicked') | ||
}, | ||
tabIndex: 1, | ||
disabled: false, | ||
}, | ||
} | ||
|
||
export default meta |
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,55 @@ | ||
import * as React from 'react' | ||
import { describe, beforeEach, afterEach, vi, expect, it } from 'vitest' | ||
import { fireEvent, screen } from '@testing-library/react' | ||
import '@testing-library/jest-dom/vitest' | ||
import { renderWithProviders } from '../../../testing/utils' | ||
import { Checkbox } from '..' | ||
|
||
const render = (props: React.ComponentProps<typeof Checkbox>) => { | ||
return renderWithProviders(<Checkbox {...props} />)[0] | ||
} | ||
|
||
describe('Checkbox', () => { | ||
let props: React.ComponentProps<typeof Checkbox> | ||
|
||
beforeEach(() => { | ||
props = { | ||
onClick: vi.fn(), | ||
isChecked: false, | ||
labelText: 'fake checkbox label', | ||
tabIndex: 1, | ||
disabled: false, | ||
} | ||
}) | ||
|
||
afterEach(() => { | ||
vi.resetAllMocks() | ||
}) | ||
|
||
it('renders label with disabled true', () => { | ||
props.disabled = true | ||
render(props) | ||
const checkBoxInput = screen.getByRole('checkbox', { | ||
name: 'fake checkbox label', | ||
}) | ||
expect(checkBoxInput).toBeDisabled() | ||
}) | ||
|
||
it('renders label with correct style - tabIndex 1', () => { | ||
props.tabIndex = 1 | ||
render(props) | ||
const checkBoxInput = screen.getByRole('checkbox', { | ||
name: 'fake checkbox label', | ||
}) | ||
expect(checkBoxInput).toHaveAttribute('tabindex', '1') | ||
}) | ||
|
||
it('calls mock function when clicking checkbox', () => { | ||
render(props) | ||
const checkBoxInput = screen.getByRole('checkbox', { | ||
name: 'fake checkbox label', | ||
}) | ||
fireEvent.click(checkBoxInput) | ||
expect(props.onClick).toHaveBeenCalled() | ||
}) | ||
}) |
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,85 @@ | ||
import * as React from 'react' | ||
import { css } from 'styled-components' | ||
import { COLORS, BORDERS } from '../../helix-design-system' | ||
import { Flex } from '../../primitives' | ||
import { Icon } from '../../icons' | ||
import { | ||
ALIGN_CENTER, | ||
DIRECTION_ROW, | ||
JUSTIFY_SPACE_BETWEEN, | ||
} from '../../styles' | ||
import { SPACING, TYPOGRAPHY } from '../../ui-style-constants' | ||
import { StyledText } from '../StyledText' | ||
|
||
export interface CheckboxProps { | ||
/** checkbox is checked if value is true */ | ||
isChecked: boolean | ||
/** label text that describes the option */ | ||
labelText: string | ||
/** callback click/tap handler */ | ||
onClick: React.MouseEventHandler | ||
/** html tabindex property */ | ||
tabIndex?: number | ||
/** if disabled is true, mouse events will not trigger onClick callback */ | ||
disabled?: boolean | ||
} | ||
export function Checkbox(props: CheckboxProps): JSX.Element { | ||
const { | ||
isChecked, | ||
labelText, | ||
onClick, | ||
tabIndex = 0, | ||
disabled = false, | ||
} = props | ||
return ( | ||
<Flex | ||
as="button" | ||
role="checkbox" | ||
width="100%" | ||
flexDirection={DIRECTION_ROW} | ||
justifyContent={JUSTIFY_SPACE_BETWEEN} | ||
alignItems={ALIGN_CENTER} | ||
padding={SPACING.spacing20} | ||
borderRadius={BORDERS.borderRadius16} | ||
backgroundColor={isChecked ? COLORS.blue50 : COLORS.blue35} | ||
color={isChecked ? COLORS.white : COLORS.black90} | ||
onClick={onClick} | ||
tabIndex={tabIndex} | ||
disabled={disabled} | ||
css={css` | ||
&:active { | ||
background-color: ${isChecked ? COLORS.blue55 : COLORS.blue40}; | ||
} | ||
&:focus-visible { | ||
background-color: ${isChecked ? COLORS.blue50 : COLORS.blue35}; | ||
outline: 3px ${BORDERS.styleSolid} ${COLORS.blue50}; | ||
outline-offset: 2px; | ||
} | ||
&:disabled { | ||
background-color: ${COLORS.grey35}; | ||
color: ${COLORS.grey50}; | ||
} | ||
`} | ||
> | ||
<StyledText as="p" fontWeight={TYPOGRAPHY.fontWeightSemiBold}> | ||
{labelText} | ||
</StyledText> | ||
<Check isChecked={isChecked} /> | ||
</Flex> | ||
) | ||
} | ||
|
||
interface CheckProps { | ||
isChecked: boolean | ||
} | ||
function Check(props: CheckProps): JSX.Element { | ||
return props.isChecked ? ( | ||
<Icon name="ot-checkbox" size="1.75rem" color={COLORS.white} /> | ||
) : ( | ||
<Flex | ||
size="1.75rem" | ||
border={`2px solid ${COLORS.black90}`} | ||
borderRadius={BORDERS.borderRadius4} | ||
/> | ||
) | ||
} |
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
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