-
-
Notifications
You must be signed in to change notification settings - Fork 2
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
Add Checkbox Component with Storybook Integration and Tests #19
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// | ||
// This source file is part of the Stanford Biodesign Digital Health Spezi Web Design System open-source project | ||
// | ||
// SPDX-FileCopyrightText: 2024 Stanford University and the project authors (see CONTRIBUTORS.md) | ||
// | ||
// SPDX-License-Identifier: MIT | ||
// | ||
|
||
import { type Meta, type StoryObj } from '@storybook/react' | ||
import { fn } from '@storybook/test' | ||
import { useState } from 'react' | ||
import { SideLabel } from '@/components/SideLabel' | ||
import { Checkbox } from './Checkbox' | ||
|
||
const meta: Meta<typeof Checkbox> = { | ||
title: 'Components/Checkbox', | ||
component: Checkbox, | ||
args: { | ||
onCheckedChange: fn(), | ||
}, | ||
} | ||
|
||
export default meta | ||
|
||
type Story = StoryObj<typeof Checkbox> | ||
|
||
export const Unchecked: Story = { args: { checked: false } } | ||
|
||
export const Chekced: Story = { args: { checked: true } } | ||
|
||
export const Functional = () => { | ||
const [checked, setChecked] = useState<boolean | 'indeterminate'>(false) | ||
return <Checkbox checked={checked} onCheckedChange={setChecked} /> | ||
} | ||
|
||
export const Labeled = () => ( | ||
<SideLabel label="Show unread only"> | ||
<Checkbox checked /> | ||
</SideLabel> | ||
) |
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,31 @@ | ||||||||
// | ||||||||
// This source file is part of the Stanford Biodesign Digital Health Spezi Web Design System open-source project | ||||||||
// | ||||||||
// SPDX-FileCopyrightText: 2024 Stanford University and the project authors (see CONTRIBUTORS.md) | ||||||||
// | ||||||||
// SPDX-License-Identifier: MIT | ||||||||
// | ||||||||
|
||||||||
import { fireEvent, render, screen } from '@testing-library/react' | ||||||||
import { vitest } from 'vitest' | ||||||||
import { Checkbox } from '.' | ||||||||
|
||||||||
describe('Checkbox', () => { | ||||||||
it('renders functional Checkbox element', () => { | ||||||||
const onCheckedChange = vitest.fn() | ||||||||
|
||||||||
render( | ||||||||
<Checkbox | ||||||||
checked={true} | ||||||||
onCheckedChange={onCheckedChange} | ||||||||
aria-label="Checkbox" | ||||||||
/>, | ||||||||
) | ||||||||
|
||||||||
const element = screen.getByLabelText('Checkbox') | ||||||||
fireEvent.click(element) | ||||||||
|
||||||||
const newCheckedValue = false | ||||||||
expect(onCheckedChange).toHaveBeenCalledWith(newCheckedValue) | ||||||||
Comment on lines
+28
to
+29
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It seems like
Suggested change
|
||||||||
}) | ||||||||
}) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// | ||
// This source file is part of the Stanford Biodesign Digital Health Spezi Web Design System open-source project | ||
// | ||
// SPDX-FileCopyrightText: 2024 Stanford University and the project authors (see CONTRIBUTORS.md) | ||
// | ||
// SPDX-License-Identifier: MIT | ||
// | ||
|
||
import * as CheckboxPrimitives from '@radix-ui/react-checkbox' | ||
import { | ||
type ComponentPropsWithoutRef, | ||
type ElementRef, | ||
forwardRef, | ||
} from 'react' | ||
import { cn } from '../../utils/className' | ||
import { CheckIcon } from 'lucide-react' | ||
|
||
export const Checkbox = forwardRef< | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It needs |
||
ElementRef<typeof CheckboxPrimitives.Root>, | ||
ComponentPropsWithoutRef<typeof CheckboxPrimitives.Root> | ||
>(({ className, ...props }, ref) => ( | ||
<CheckboxPrimitives.Root | ||
className={cn( | ||
'hover:bg-violet3 flex size-[25px] appearance-none items-center justify-center rounded bg-white shadow-[0_0_0_2px_black] outline-none', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have not run this yet, but it seems like we need a bit more work here. Components have to rely on the standard Tailwind's design tokens and design system custom color tokens. This enables consumers to modify the theme. Focus states have to be properly handled. |
||
className, | ||
)} | ||
{...props} | ||
ref={ref} | ||
> | ||
<CheckboxPrimitives.Indicator> | ||
<CheckIcon /> | ||
</CheckboxPrimitives.Indicator> | ||
</CheckboxPrimitives.Root> | ||
)) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
// | ||
// This source file is part of the Stanford Biodesign Digital Health Spezi Web Design System open-source project | ||
// | ||
// SPDX-FileCopyrightText: 2024 Stanford University and the project authors (see CONTRIBUTORS.md) | ||
// | ||
// SPDX-License-Identifier: MIT | ||
// | ||
|
||
export * from './Checkbox' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
./components/Checkbox
needs to be added here. It enables consumers of the package to importCheckbox
directly: