|
| 1 | +--- |
| 2 | +id: checkbox |
| 3 | +title: Checkbox |
| 4 | +--- |
| 5 | + |
| 6 | +At the heart and soul of every application is at least one checkbox. It might be a terms and conditions agreement, |
| 7 | +"Do not sell" CCPA notice, or something else; whatever it is, `pretty-checkbox-react` (PCR) has your back. |
| 8 | + |
| 9 | +## Shapes, Variants, & States |
| 10 | + |
| 11 | +There's no "one size fits all" for input controls like Checkbox. |
| 12 | +Thankfully, PCR allows you to represent Checkbox in various ways via the `shape` and `variant` prop |
| 13 | +for pre-bundled fun. See the [`shapes`](props/shapes-size) docs for more details. |
| 14 | + |
| 15 | +```jsx live |
| 16 | +<> |
| 17 | + <Checkbox>Regular</Checkbox> |
| 18 | + <Checkbox shape="curve" variant="thick"> |
| 19 | + Thick Curve |
| 20 | + </Checkbox> |
| 21 | + <Checkbox shape="round" variant="fill"> |
| 22 | + Fill Round |
| 23 | + </Checkbox> |
| 24 | +</> |
| 25 | +``` |
| 26 | + |
| 27 | +### Disabled & Locked |
| 28 | + |
| 29 | +Of course `disabled` is supported, but there's also a secondary state called `locked`. |
| 30 | +See the [states](props/states) docs for more details. |
| 31 | + |
| 32 | +```jsx live |
| 33 | +<> |
| 34 | + <Checkbox>Regular</Checkbox> |
| 35 | + <Checkbox disabled>Disabled</Checkbox> |
| 36 | + <Checkbox locked>Locked</Checkbox> |
| 37 | +</> |
| 38 | +``` |
| 39 | + |
| 40 | +### Scalability |
| 41 | + |
| 42 | +Out of the box, PCR offers a `bigger` prop to make all input controls just a tad bit larger. |
| 43 | +For more fine-grained control check out the [size docs](props/shapes-size#size). |
| 44 | + |
| 45 | +```jsx live |
| 46 | +<> |
| 47 | + <Checkbox>Regular</Checkbox> |
| 48 | + <Checkbox bigger>Bigger</Checkbox> |
| 49 | +</> |
| 50 | +``` |
| 51 | + |
| 52 | +## Uncontrolled |
| 53 | + |
| 54 | +`Checkbox` forwards `refs` to the input element :smile:. |
| 55 | +See the [uncontrolled component docs](main-concepts/uncontrolled) for more info. |
| 56 | + |
| 57 | +```jsx live |
| 58 | +function App() { |
| 59 | + const ref = React.useRef(null); |
| 60 | + |
| 61 | + React.useEffect(() => { |
| 62 | + if (ref.current) { |
| 63 | + console.log(ref.current); |
| 64 | + } |
| 65 | + }, []); |
| 66 | + |
| 67 | + return <Checkbox ref={ref}>Uncontrolled Usage</Checkbox>; |
| 68 | +} |
| 69 | +``` |
| 70 | + |
| 71 | +## Controlled |
| 72 | + |
| 73 | +PCR exposes helpful state hooks to make managing state a breeze. For checkbox you should use `useCheckboxState`. |
| 74 | +See the [controlled component docs](main-concepts/controlled) for more info. |
| 75 | + |
| 76 | +### Boolean Checkboxes |
| 77 | + |
| 78 | +For simple yes/no, or true/false usage, you can use the hook as-is without any extra config needed 😲: |
| 79 | + |
| 80 | +```jsx live |
| 81 | +function App() { |
| 82 | + const checkbox = useCheckboxState(); |
| 83 | + |
| 84 | + return <Checkbox {...checkbox}>Checked: {checkbox.state + ''}</Checkbox>; |
| 85 | +} |
| 86 | +``` |
| 87 | + |
| 88 | +### Named Checkbox |
| 89 | + |
| 90 | +PCR supports checkbox controls with a `value` prop too! The resulting state will be an `array` – even if it's a single item. |
| 91 | + |
| 92 | +:::note |
| 93 | +The initial `state` will be `false` unless specified otherwise. |
| 94 | +::: |
| 95 | + |
| 96 | +```jsx live |
| 97 | +function App() { |
| 98 | + const checkbox = useCheckboxState(); |
| 99 | + |
| 100 | + return ( |
| 101 | + <Checkbox value="active" {...checkbox}> |
| 102 | + Checked: {checkbox.state + ''} |
| 103 | + </Checkbox> |
| 104 | + ); |
| 105 | +} |
| 106 | +``` |
| 107 | + |
| 108 | +### Grouped Controls |
| 109 | + |
| 110 | +PCR represents _named_ checkboxes as an array which allows us to use the same state hook for multiple checkboxes! |
| 111 | +Not only does this keep your code clean, but keeps th footprint of PCR extra small :sunglasses: |
| 112 | + |
| 113 | +```jsx live |
| 114 | +function App() { |
| 115 | + const fruits = ['apples', 'oranges', 'bananas']; |
| 116 | + const checkbox = useCheckboxState({ state: [] }); |
| 117 | + |
| 118 | + return ( |
| 119 | + <> |
| 120 | + {fruits.map(fruit => ( |
| 121 | + <Checkbox value={fruit} {...checkbox}> |
| 122 | + {fruit} |
| 123 | + </Checkbox> |
| 124 | + ))} |
| 125 | + <p>Selected items: {checkbox.state.join(', ')}</p> |
| 126 | + </> |
| 127 | + ); |
| 128 | +} |
| 129 | +``` |
| 130 | + |
| 131 | +## Indeterminate |
| 132 | + |
| 133 | +`indeterminate`, or tri-state checkboxes are relative niche, but have their place in the wild. |
| 134 | +PCR supports state-driven tri-state as well as an `indeterminate` prop for uncontrolled usage. |
| 135 | + |
| 136 | +### Uncontrolled Indeterminate |
| 137 | + |
| 138 | +Using the `indeterminate`, PCR sets two things: |
| 139 | + |
| 140 | +1. `aria-checked` to `mixed` |
| 141 | +2. `indeterminate` property on the `input` element to true |
| 142 | + |
| 143 | +:::info |
| 144 | +As of `alpha.2` the second point isn't true. A fixed is needed on pretty-checkbox to |
| 145 | +ensure `:indeterminate` selectors style the checkbo as checked. |
| 146 | +::: |
| 147 | + |
| 148 | +```jsx live |
| 149 | +<Checkbox indeterminate>Indeterminate</Checkbox> |
| 150 | +``` |
| 151 | + |
| 152 | +### Controlled Indeterminate |
| 153 | + |
| 154 | +For controlled usage, you can use the `indeterminate` _or_ use "indeterminate" as a special state keyword: |
| 155 | + |
| 156 | +```jsx live |
| 157 | +function App() { |
| 158 | + const checkbox = useCheckboxState(); |
| 159 | + |
| 160 | + return ( |
| 161 | + <> |
| 162 | + <Checkbox |
| 163 | + icon={ |
| 164 | + <i |
| 165 | + className={`mdi mdi-${ |
| 166 | + checkbox.state === 'indeterminate' ? 'minus' : 'check' |
| 167 | + }`} |
| 168 | + /> |
| 169 | + } |
| 170 | + {...checkbox}> |
| 171 | + Controlled Indeterminate |
| 172 | + </Checkbox> |
| 173 | + <button |
| 174 | + onClick={() => { |
| 175 | + checkbox.setState('indeterminate'); |
| 176 | + }}> |
| 177 | + Indeterminate |
| 178 | + </button> |
| 179 | + </> |
| 180 | + ); |
| 181 | +} |
| 182 | +``` |
| 183 | + |
| 184 | +Controlled indeterminates have a whole host of usages including trees! |
| 185 | + |
| 186 | +<p> |
| 187 | + <small> |
| 188 | + Credit where credit is due. This example is{' '} |
| 189 | + <a href="https://reakit.io/docs/checkbox/#indeterminate-or-mixed-state"> |
| 190 | + adapted from reakit |
| 191 | + </a> |
| 192 | + </small> |
| 193 | +</p> |
| 194 | + |
| 195 | +```jsx live |
| 196 | +(function () { |
| 197 | + function useTreeState({ values }) { |
| 198 | + const group = useCheckboxState(); |
| 199 | + const items = useCheckboxState(); |
| 200 | + |
| 201 | + // updates items when group is toggled |
| 202 | + React.useEffect(() => { |
| 203 | + if (group.state === true) { |
| 204 | + items.setState(values); |
| 205 | + } else if (group.state === false) { |
| 206 | + items.setState([]); |
| 207 | + } |
| 208 | + }, [group.state]); |
| 209 | + |
| 210 | + // updates group when items is toggled |
| 211 | + React.useEffect(() => { |
| 212 | + if (items.state.length === values.length) { |
| 213 | + group.setState(true); |
| 214 | + } else if (items.state.length) { |
| 215 | + group.setState('indeterminate'); |
| 216 | + } else { |
| 217 | + group.setState(false); |
| 218 | + } |
| 219 | + }, [items.state]); |
| 220 | + |
| 221 | + return { group, items }; |
| 222 | + } |
| 223 | + |
| 224 | + function Icon({ state }) { |
| 225 | + return <i className={`icon mdi mdi-${state === 'indeterminate' ? 'minus' : 'check'}`} />; |
| 226 | + } |
| 227 | + |
| 228 | + function App() { |
| 229 | + const values = ['Apple', 'Orange', 'Watermelon']; |
| 230 | + const { group, items } = useTreeState({ values }); |
| 231 | + |
| 232 | + return ( |
| 233 | + <ul> |
| 234 | + <li> |
| 235 | + <label> |
| 236 | + <Checkbox icon={<Icon {...group} />} data-type="icon" {...group}> |
| 237 | + Fruits |
| 238 | + </Checkbox> |
| 239 | + </label> |
| 240 | + </li> |
| 241 | + <ul> |
| 242 | + {values.map((value, i) => ( |
| 243 | + <li key={i}> |
| 244 | + <label> |
| 245 | + <Checkbox icon={<Icon />} data-type="icon" {...items} value={value}> |
| 246 | + {value} |
| 247 | + </Checkbox> |
| 248 | + </label> |
| 249 | + </li> |
| 250 | + ))} |
| 251 | + </ul> |
| 252 | + </ul> |
| 253 | + ); |
| 254 | + } |
| 255 | + |
| 256 | + return App; |
| 257 | +})() |
| 258 | +``` |
0 commit comments