Skip to content

Commit f4c8a89

Browse files
committed
Add Checkbox component
see #81
1 parent b6cc121 commit f4c8a89

File tree

11 files changed

+399
-8
lines changed

11 files changed

+399
-8
lines changed

package-lock.json

Lines changed: 130 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,9 @@
7777
},
7878
"rules": {
7979
"import/default": "off",
80-
"import/no-default-export": "off"
80+
"import/no-default-export": "off",
81+
"react/display-name": "off",
82+
"import/no-named-as-default-member": "warn"
8183
},
8284
"ignorePatterns": [
8385
"dist",
@@ -141,6 +143,7 @@
141143
"dependencies": {
142144
"@radix-ui/react-accordion": "^0.0.14",
143145
"@radix-ui/react-avatar": "^0.0.13",
146+
"@radix-ui/react-checkbox": "^0.0.15",
144147
"@radix-ui/react-polymorphic": "^0.0.11",
145148
"@stitches/react": "^0.1.9"
146149
}

src/components/Card/Card.stories.tsx

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
import { action } from '@storybook/addon-actions'
2+
import { Meta } from '@storybook/react'
13
import React from 'react'
24
import { styled } from 'stitches.config'
3-
import { Story, Meta } from '@storybook/react'
45
import { Card } from '.'
56
import { Row } from '../'
67

@@ -90,12 +91,7 @@ export const Ghost = () => {
9091
Lorem Ipsum is simply dummy text...
9192
</Text>
9293
</Card>
93-
<Card
94-
as="button"
95-
interactive
96-
variant="ghost"
97-
onClick={() => alert('Action')}
98-
>
94+
<Card as="button" interactive variant="ghost" onClick={action('Action')}>
9995
<Text css={{ lineHeight: '23px', fontWeight: 500 }}>Ghost</Text>
10096
<Text css={{ color: '$grey900', lineHeight: '23px' }}>
10197
Lorem Ipsum is simply dummy text...
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
import React, { useState, ChangeEventHandler } from 'react'
2+
import { Story, Meta } from '@storybook/react'
3+
import { action } from '@storybook/addon-actions'
4+
import { Checkbox } from '.'
5+
import { Variants } from '../../docs/util'
6+
import { Flex } from '../'
7+
8+
export default {
9+
title: 'Components/Checkbox',
10+
component: Checkbox,
11+
} as Meta
12+
const click = action('clicked')
13+
14+
export const Default = () => <Checkbox onCheckedChange={action('checked')} />
15+
16+
/**
17+
* A primary version for if the check is the main action.
18+
* (This probably doesn't happen very often.)
19+
*
20+
* Example with state - __Note__ the use of `onCheckedChange` to get the change notification from all triggering actions.
21+
*/
22+
export const Primary = () => {
23+
const [checked, setChecked] = useState(false)
24+
return (
25+
<Checkbox
26+
css={{ m: '$3' }}
27+
checked={checked}
28+
variant="primary"
29+
onCheckedChange={(event) => {
30+
click(event)
31+
setChecked(!checked)
32+
}}
33+
/>
34+
)
35+
}
36+
37+
export const Destructive = () => {
38+
return (
39+
<Flex>
40+
<Checkbox css={{ m: '$3' }} variant="primary" destructive />
41+
<Checkbox css={{ m: '$3' }} destructive />
42+
</Flex>
43+
)
44+
}
45+
46+
/**
47+
* Checkbox also support a `indeterminate` checked state.
48+
* This can only be used in a controlled behaviour.
49+
*/
50+
export const Indeterminate = () => {
51+
const [checked, setChecked] = useState<boolean | 'indeterminate'>(
52+
'indeterminate'
53+
)
54+
55+
const rotate: ChangeEventHandler<HTMLInputElement> = (e) => {
56+
click(e)
57+
if (checked === 'indeterminate') {
58+
setChecked(true)
59+
} else if (checked) {
60+
setChecked(false)
61+
} else {
62+
setChecked('indeterminate')
63+
}
64+
}
65+
return (
66+
<Flex>
67+
<Checkbox
68+
css={{ m: '$3' }}
69+
checked={checked}
70+
variant="primary"
71+
onCheckedChange={rotate}
72+
/>
73+
<Checkbox css={{ m: '$3' }} checked={checked} onCheckedChange={rotate} />
74+
</Flex>
75+
)
76+
}
77+
78+
export const All = () => (
79+
<Variants
80+
component={Checkbox}
81+
css={{ m: '$3' }}
82+
variant={['primary', 'secondary']}
83+
destructive={[false, true]}
84+
disabled={[false, true]}
85+
checked={[false, true, 'indeterminate']}
86+
/>
87+
)
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import React from 'react'
2+
import { renderLight, renderDark } from 'test-utils'
3+
import { Default } from './Checkbox.stories'
4+
5+
it('renders light without error', () => {
6+
const { asFragment } = renderLight(<Default />)
7+
expect(asFragment()).toBeDefined()
8+
})
9+
10+
it('renders dark without error', () => {
11+
const { asFragment } = renderDark(<Default />)
12+
expect(asFragment()).toBeDefined()
13+
})

0 commit comments

Comments
 (0)