5
5
*
6
6
* Copyright Oxide Computer Company
7
7
*/
8
- import {
9
- useController ,
10
- type Control ,
11
- type FieldPath ,
12
- type FieldValues ,
13
- } from 'react-hook-form'
8
+ import { Controller , type Control , type FieldPath , type FieldValues } from 'react-hook-form'
14
9
15
10
import { Checkbox , type CheckboxProps } from '~/ui/lib/Checkbox'
16
11
@@ -29,34 +24,35 @@ export const CheckboxField = <
29
24
control,
30
25
name,
31
26
...props
32
- } : CheckboxFieldProps < TFieldValues , TName > ) => {
33
- const {
34
- field : { onChange, value } ,
35
- } = useController ( { name, control } )
36
- return (
37
- < Checkbox
38
- { ...props }
39
- // If value is an array, we're dealing with a set of checkboxes that
40
- // have the same name and different `value` attrs, and are therefore
41
- // supposed to produce an array of the values that are checked. `value`
42
- // is the value in form state, which can be a bool or array.
43
- // `props.value` is the value string of the current checkbox, which is
44
- // only relevant in the array case
45
- onChange = { ( e ) => {
46
- if ( Array . isArray ( value ) && props . value ) {
47
- // it's one of a set of checkboxes. if it was just checked, we're
48
- // adding it to the array, otherwise we're removing it
49
- const valueArray = value as string [ ]
50
- const newValue = e . target . checked
51
- ? [ ...valueArray , props . value ]
52
- : valueArray . filter ( ( x ) => x !== props . value )
53
- onChange ( newValue )
54
- } else {
55
- // it's a single checkbox
56
- onChange ( e . target . checked )
57
- }
58
- } }
59
- checked = { Array . isArray ( value ) ? value . includes ( props . value ) : value }
60
- />
61
- )
62
- }
27
+ } : CheckboxFieldProps < TFieldValues , TName > ) => (
28
+ < Controller
29
+ name = { name }
30
+ control = { control }
31
+ render = { ( { field : { onChange, value } } ) => (
32
+ < Checkbox
33
+ { ...props }
34
+ // If value is an array, we're dealing with a set of checkboxes that
35
+ // have the same name and different `value` attrs, and are therefore
36
+ // supposed to produce an array of the values that are checked. `value`
37
+ // is the value in form state, which can be a bool or array.
38
+ // `props.value` is the value string of the current checkbox, which is
39
+ // only relevant in the array case
40
+ onChange = { ( e ) => {
41
+ if ( Array . isArray ( value ) && props . value ) {
42
+ // it's one of a set of checkboxes. if it was just checked, we're
43
+ // adding it to the array, otherwise we're removing it
44
+ const valueArray = value as string [ ]
45
+ const newValue = e . target . checked
46
+ ? [ ...valueArray , props . value ]
47
+ : valueArray . filter ( ( x ) => x !== props . value )
48
+ onChange ( newValue )
49
+ } else {
50
+ // it's a single checkbox
51
+ onChange ( e . target . checked )
52
+ }
53
+ } }
54
+ checked = { Array . isArray ( value ) ? value . includes ( props . value ) : value }
55
+ />
56
+ ) }
57
+ />
58
+ )
0 commit comments