-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Expand file tree
/
Copy pathindex.tsx
More file actions
149 lines (139 loc) · 3.55 KB
/
index.tsx
File metadata and controls
149 lines (139 loc) · 3.55 KB
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
139
140
141
142
143
144
145
146
147
148
149
/**
* 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 { HStack } from '../h-stack';
import type { CheckboxControlProps } from './types';
import type { WordPressComponentProps } from '../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(
props: WordPressComponentProps< CheckboxControlProps, 'input', false >
) {
const {
__nextHasNoMarginBottom,
label,
className,
heading,
checked,
indeterminate,
help,
id: idProp,
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 id = useInstanceId(
CheckboxControl,
'inspector-checkbox-control',
idProp
);
const onChangeValue = ( event: ChangeEvent< HTMLInputElement > ) =>
onChange( event.target.checked );
return (
<BaseControl
__nextHasNoMarginBottom={ __nextHasNoMarginBottom }
label={ heading }
id={ id }
help={
help && (
<span className="components-checkbox-control__help">
{ help }
</span>
)
}
className={ classnames( 'components-checkbox-control', className ) }
>
<HStack spacing={ 0 } justify="start" alignment="top">
<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 && (
<label
className="components-checkbox-control__label"
htmlFor={ id }
>
{ label }
</label>
) }
</HStack>
</BaseControl>
);
}
export default CheckboxControl;