@primer/components 32.0.1-rc.44e3df9b → 32.1.0-rc.6f5d2b00

Sign up to get free protection for your applications and to get access to all the features.
Files changed (41) hide show
  1. package/CHANGELOG.md +10 -0
  2. package/dist/browser.esm.js +7 -1
  3. package/dist/browser.esm.js.map +1 -1
  4. package/dist/browser.umd.js +7 -1
  5. package/dist/browser.umd.js.map +1 -1
  6. package/docs/content/Checkbox.md +118 -0
  7. package/docs/src/@primer/gatsby-theme-doctocat/nav.yml +2 -0
  8. package/lib/Checkbox.d.ts +29 -0
  9. package/lib/Checkbox.js +64 -0
  10. package/lib/Overlay.d.ts +5 -3
  11. package/lib/__tests__/Checkbox.test.d.ts +2 -0
  12. package/lib/__tests__/Checkbox.test.js +189 -0
  13. package/lib/__tests__/Overlay.types.test.d.ts +3 -0
  14. package/lib/__tests__/Overlay.types.test.js +38 -0
  15. package/lib/__tests__/SelectPanel.types.test.d.ts +3 -0
  16. package/lib/__tests__/SelectPanel.types.test.js +44 -0
  17. package/lib/index.d.ts +2 -0
  18. package/lib/index.js +8 -0
  19. package/lib/stories/Checkbox.stories.js +227 -0
  20. package/lib-esm/Checkbox.d.ts +29 -0
  21. package/lib-esm/Checkbox.js +44 -0
  22. package/lib-esm/Overlay.d.ts +5 -3
  23. package/lib-esm/__tests__/Checkbox.test.d.ts +2 -0
  24. package/lib-esm/__tests__/Checkbox.test.js +169 -0
  25. package/lib-esm/__tests__/Overlay.types.test.d.ts +3 -0
  26. package/lib-esm/__tests__/Overlay.types.test.js +29 -0
  27. package/lib-esm/__tests__/SelectPanel.types.test.d.ts +3 -0
  28. package/lib-esm/__tests__/SelectPanel.types.test.js +29 -0
  29. package/lib-esm/index.d.ts +2 -0
  30. package/lib-esm/index.js +1 -0
  31. package/lib-esm/stories/Checkbox.stories.js +197 -0
  32. package/package.json +1 -1
  33. package/src/Checkbox.tsx +75 -0
  34. package/src/Overlay.tsx +7 -4
  35. package/src/__tests__/Checkbox.test.tsx +155 -0
  36. package/src/__tests__/Overlay.types.test.tsx +15 -0
  37. package/src/__tests__/SelectPanel.types.test.tsx +31 -0
  38. package/src/__tests__/__snapshots__/Checkbox.test.tsx.snap +16 -0
  39. package/src/index.ts +3 -0
  40. package/src/stories/Checkbox.stories.tsx +164 -0
  41. package/stats.html +1 -1
@@ -0,0 +1,164 @@
1
+ import React, {useLayoutEffect, useRef, useState} from 'react'
2
+ import {Meta} from '@storybook/react'
3
+ import styled from 'styled-components'
4
+
5
+ import {BaseStyles, Box, Checkbox, CheckboxProps, Text, ThemeProvider} from '..'
6
+ import {action} from '@storybook/addon-actions'
7
+ import {COMMON, get} from '../constants'
8
+
9
+ export default {
10
+ title: 'Forms/Checkbox',
11
+ component: Checkbox,
12
+ decorators: [
13
+ Story => {
14
+ return (
15
+ <ThemeProvider>
16
+ <BaseStyles>
17
+ <Box paddingTop={5}>{Story()}</Box>
18
+ </BaseStyles>
19
+ </ThemeProvider>
20
+ )
21
+ }
22
+ ],
23
+ argTypes: {
24
+ sx: {
25
+ table: {
26
+ disable: true
27
+ }
28
+ },
29
+ disabled: {
30
+ name: 'Disabled',
31
+ defaultValue: false,
32
+ control: {
33
+ type: 'boolean'
34
+ }
35
+ }
36
+ }
37
+ } as Meta
38
+
39
+ const StyledLabel = styled.label`
40
+ user-select: none;
41
+ font-weight: 600;
42
+ font-size: 14px;
43
+ line-height: 18px;
44
+ margin-left: 16px;
45
+ ${COMMON}
46
+ `
47
+
48
+ const StyledSubLabel = styled(Text)`
49
+ color: ${get('colors.fg.muted')};
50
+ font-size: 13px;
51
+ ${COMMON}
52
+ `
53
+
54
+ export const Default = (args: CheckboxProps) => {
55
+ const [isChecked, setChecked] = useState<boolean>(false)
56
+
57
+ const handleChange = (event: React.ChangeEvent<HTMLInputElement>) => {
58
+ setChecked(event.target.checked)
59
+ action('Change event triggered')
60
+ }
61
+
62
+ return (
63
+ <>
64
+ <Box as="form" sx={{p: 3, display: 'flex', alignItems: 'flex-start'}}>
65
+ <Checkbox id="controlled-checkbox" onChange={handleChange} checked={isChecked} {...args} />
66
+ <StyledLabel htmlFor="controlled-checkbox">
67
+ <Text sx={{display: 'block'}}>Default checkbox</Text>
68
+ <StyledSubLabel>controlled</StyledSubLabel>
69
+ </StyledLabel>
70
+ </Box>
71
+ <Box as="form" sx={{p: 3, display: 'flex', alignItems: 'flex-start'}}>
72
+ <Checkbox id="always-checked-checkbox" checked {...args} />
73
+ <StyledLabel htmlFor="always-checked-checkbox">
74
+ <Text sx={{display: 'block'}}>Always checked</Text>
75
+ <StyledSubLabel>checked=&quot;true&quot;</StyledSubLabel>
76
+ </StyledLabel>
77
+ </Box>
78
+ <Box as="form" sx={{p: 3, display: 'flex', alignItems: 'flex-start'}}>
79
+ <Checkbox id="always-unchecked-checkbox" checked={false} {...args} />
80
+ <StyledLabel htmlFor="always-unchecked-checkbox">
81
+ <Text sx={{display: 'block'}}>Always unchecked</Text>
82
+ <StyledSubLabel>checked=&quot;false&quot;</StyledSubLabel>
83
+ </StyledLabel>
84
+ </Box>
85
+ <Box as="form" sx={{p: 3, display: 'flex', alignItems: 'flex-start'}}>
86
+ <Checkbox id="disabled-checkbox" disabled checked={false} />
87
+ <StyledLabel htmlFor="disabled-checkbox">
88
+ <Text sx={{display: 'block'}}>Inactive</Text>
89
+ <StyledSubLabel>disabled=&quot;true&quot;</StyledSubLabel>
90
+ </StyledLabel>
91
+ </Box>
92
+ </>
93
+ )
94
+ }
95
+
96
+ export const Uncontrolled = (args: CheckboxProps) => {
97
+ const checkboxRef = useRef<HTMLInputElement | null>(null)
98
+
99
+ useLayoutEffect(() => {
100
+ if (checkboxRef.current) {
101
+ checkboxRef.current.checked = true
102
+ }
103
+ }, [])
104
+
105
+ return (
106
+ <Box as="form" sx={{p: 3, display: 'flex', alignItems: 'flex-start'}}>
107
+ <Checkbox id="uncontrolled-checkbox" ref={checkboxRef} {...args} />
108
+ <StyledLabel htmlFor="uncontrolled-checkbox">
109
+ <Text sx={{display: 'block'}}>Uncontrolled checkbox</Text>
110
+ <StyledSubLabel>Checked by default</StyledSubLabel>
111
+ </StyledLabel>
112
+ </Box>
113
+ )
114
+ }
115
+
116
+ export const Indeterminate = (args: CheckboxProps) => {
117
+ const [checkboxes, setCheckboxes] = useState<boolean[]>([false, false, false, false])
118
+
119
+ const handleChange = (_: React.ChangeEvent<HTMLInputElement>, index: number) => {
120
+ const newCheckboxes = [...checkboxes]
121
+ newCheckboxes[index] = !checkboxes[index]
122
+ setCheckboxes(newCheckboxes)
123
+ }
124
+
125
+ const handleIndeterminateChange = () => {
126
+ if (checkboxes.every(checkbox => checkbox)) {
127
+ return setCheckboxes(checkboxes.map(() => false))
128
+ }
129
+
130
+ const newCheckboxes = checkboxes.map(() => true)
131
+ setCheckboxes(newCheckboxes)
132
+ }
133
+
134
+ return (
135
+ <>
136
+ <Box as="form" sx={{p: 3, display: 'flex', alignItems: 'flex-start'}}>
137
+ <Checkbox
138
+ id="indeterminate-checkbox"
139
+ checked={checkboxes.every(Boolean)}
140
+ onChange={handleIndeterminateChange}
141
+ indeterminate={!checkboxes.every(Boolean)}
142
+ />
143
+ <StyledLabel htmlFor="controlled-checkbox">
144
+ <Text sx={{display: 'block'}}>Default checkbox</Text>
145
+ <StyledSubLabel>controlled</StyledSubLabel>
146
+ </StyledLabel>
147
+ </Box>
148
+
149
+ {checkboxes.map((field, index) => (
150
+ <Box key={`sub-checkbox-${index}`} as="form" sx={{p: 1, pl: 7, display: 'flex', alignItems: 'flex-start'}}>
151
+ <Checkbox
152
+ id={`sub-checkbox-${index}`}
153
+ checked={checkboxes[index]}
154
+ onChange={event => handleChange(event, index)}
155
+ {...args}
156
+ />
157
+ <StyledLabel htmlFor={`sub-checkbox-${index}`}>
158
+ <Text sx={{display: 'block'}}>Checkbox {index + 1}</Text>
159
+ </StyledLabel>
160
+ </Box>
161
+ ))}
162
+ </>
163
+ )
164
+ }