-
Notifications
You must be signed in to change notification settings - Fork 4.4k
/
Copy pathindex.tsx
138 lines (128 loc) · 3.34 KB
/
index.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/**
* External dependencies
*/
import classnames from 'classnames';
import type { ChangeEvent } from 'react';
/**
* WordPress dependencies
*/
import { useState } from '@wordpress/element';
import { useInstanceId, useRefEffect } from '@wordpress/compose';
import deprecated from '@wordpress/deprecated';
import { Icon, check, reset } from '@wordpress/icons';
/**
* Internal dependencies
*/
import BaseControl from '../base-control';
import type { CheckboxControlProps } from './types';
import type { WordPressComponentProps } from '../ui/context';
/**
* Checkboxes allow the user to select one or more items from a set.
*
* ```jsx
* import { CheckboxControl } from '@wordpress/components';
* import { useState } from '@wordpress/element';
*
* const MyCheckboxControl = () => {
* const [ isChecked, setChecked ] = useState( true );
* return (
* <CheckboxControl
* label="Is author"
* help="Is the user a author or not?"
* checked={ isChecked }
* onChange={ setChecked }
* />
* );
* };
* ```
*/
export function CheckboxControl(
// ref is omitted until we have `WordPressComponentPropsWithoutRef` or add
// ref forwarding to CheckboxControl.
props: Omit<
WordPressComponentProps< CheckboxControlProps, 'input', false >,
'ref'
>
) {
const {
label,
className,
heading,
checked,
indeterminate,
help,
onChange,
...additionalProps
} = props;
if ( heading ) {
deprecated( '`heading` prop in `CheckboxControl`', {
alternative: 'a separate element to implement a heading',
since: '5.8',
} );
}
const [ showCheckedIcon, setShowCheckedIcon ] = useState( false );
const [ showIndeterminateIcon, setShowIndeterminateIcon ] = useState(
false
);
// Run the following callback every time the `ref` (and the additional
// dependencies) change.
const ref = useRefEffect< HTMLInputElement >(
( node ) => {
if ( ! node ) {
return;
}
// It cannot be set using an HTML attribute.
node.indeterminate = !! indeterminate;
setShowCheckedIcon( node.matches( ':checked' ) );
setShowIndeterminateIcon( node.matches( ':indeterminate' ) );
},
[ checked, indeterminate ]
);
const instanceId = useInstanceId( CheckboxControl );
const id = `inspector-checkbox-control-${ instanceId }`;
const onChangeValue = ( event: ChangeEvent< HTMLInputElement > ) =>
onChange( event.target.checked );
return (
<BaseControl
label={ heading }
id={ id }
help={ help }
className={ classnames( 'components-checkbox-control', className ) }
>
<span className="components-checkbox-control__input-container">
<input
ref={ ref }
id={ id }
className="components-checkbox-control__input"
type="checkbox"
value="1"
onChange={ onChangeValue }
checked={ checked }
aria-describedby={ !! help ? id + '__help' : undefined }
{ ...additionalProps }
/>
{ showIndeterminateIcon ? (
<Icon
icon={ reset }
className="components-checkbox-control__indeterminate"
role="presentation"
/>
) : null }
{ showCheckedIcon ? (
<Icon
icon={ check }
className="components-checkbox-control__checked"
role="presentation"
/>
) : null }
</span>
<label
className="components-checkbox-control__label"
htmlFor={ id }
>
{ label }
</label>
</BaseControl>
);
}
export default CheckboxControl;