Skip to content

Commit

Permalink
Add Checkbox component
Browse files Browse the repository at this point in the history
  • Loading branch information
arkadiuszbachorski committed Jan 14, 2025
1 parent 0fed267 commit bff4f55
Show file tree
Hide file tree
Showing 6 changed files with 131 additions and 0 deletions.
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@
"types": "./dist/components/Calendar.d.ts",
"default": "./dist/components/Calendar.js"
},
"./components/Checkbox": {
"types": "./dist/components/Checkbox.d.ts",
"default": "./dist/components/Checkbox.js"
},
"./components/Card": {
"types": "./dist/components/Card.d.ts",
"default": "./dist/components/Card.js"
Expand Down
52 changes: 52 additions & 0 deletions src/components/Checkbox/Checkbox.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
//
// This source file is part of the Stanford Biodesign Digital Health Spezi Web Design System open-source project
//
// SPDX-FileCopyrightText: 2025 Stanford University and the project authors (see CONTRIBUTORS.md)
//
// SPDX-License-Identifier: MIT
//

import { useArgs } from '@storybook/core/preview-api'
import { type Meta, type StoryObj } from '@storybook/react'
import { fn } from '@storybook/test'
import { type ComponentProps } from 'react'
import { SideLabel } from '@/components/SideLabel'
import { Checkbox } from './Checkbox'

const meta: Meta<typeof Checkbox> = {
title: 'Components/Checkbox',
component: Checkbox,
args: {
checked: false,
onCheckedChange: fn(),
},
}

export default meta

type Story = StoryObj<typeof Checkbox>

export const Default: Story = {
render: function Render(args) {
const [, updateArgs] = useArgs<ComponentProps<typeof Checkbox>>()
return (
<Checkbox
{...args}
onCheckedChange={(checked) => updateArgs({ checked })}
/>
)
},
}

export const Checked: Story = {
...Default,
args: {
checked: true,
},
}

export const Labeled = () => (
<SideLabel label="Show unread only">
<Checkbox />
</SideLabel>
)
30 changes: 30 additions & 0 deletions src/components/Checkbox/Checkbox.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//
// This source file is part of the Stanford Biodesign Digital Health Spezi Web Design System open-source project
//
// SPDX-FileCopyrightText: 2025 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 checkbox = screen.getByLabelText('Checkbox')
fireEvent.click(checkbox)

expect(onCheckedChange).toHaveBeenCalledWith(false)
})
})
35 changes: 35 additions & 0 deletions src/components/Checkbox/Checkbox.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
//
// This source file is part of the Stanford Biodesign Digital Health Spezi Web Design System open-source project
//
// SPDX-FileCopyrightText: 2025 Stanford University and the project authors (see CONTRIBUTORS.md)
//
// SPDX-License-Identifier: MIT
//

import * as CheckboxPrimitive from '@radix-ui/react-checkbox'
import {
type ComponentPropsWithoutRef,
type ElementRef,
forwardRef,
} from 'react'
import { cn } from '@/utils/className'

type CheckboxProps = ComponentPropsWithoutRef<typeof CheckboxPrimitive.Root>

export const Checkbox = forwardRef<
ElementRef<typeof CheckboxPrimitive.Root>,
CheckboxProps
>(({ className, ...props }, ref) => (
<CheckboxPrimitive.Root
ref={ref}
className={cn(
'focus-ring flex-center peer size-4 shrink-0 rounded border border-input disabled:cursor-not-allowed disabled:opacity-50',
className,
)}
{...props}
>
<CheckboxPrimitive.Indicator className="flex-center size-2.5 rounded-sm bg-primary" />
</CheckboxPrimitive.Root>
))

Checkbox.displayName = CheckboxPrimitive.Root.displayName
9 changes: 9 additions & 0 deletions src/components/Checkbox/index.tsx
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: 2025 Stanford University and the project authors (see CONTRIBUTORS.md)
//
// SPDX-License-Identifier: MIT
//

export * from './Checkbox'
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export * from './components/Select'
export * from './components/Separator'
export * from './components/SideLabel'
export * from './components/Switch'
export * from './components/Checkbox'

Check warning on line 42 in src/index.ts

View check run for this annotation

Codecov / codecov/patch

src/index.ts#L42

Added line #L42 was not covered by tests
export * from './components/Table'
export * from './components/Tabs'
export * from './components/Textarea'
Expand Down

0 comments on commit bff4f55

Please sign in to comment.