-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathindex.js
64 lines (55 loc) · 1.24 KB
/
index.js
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
/**
* External dependencies
*/
import { isFunction } from 'lodash';
/**
* WordPress dependencies
*/
import { Component } from '@wordpress/element';
import { withInstanceId } from '@wordpress/compose';
/**
* Internal dependencies
*/
import FormToggle from '../form-toggle';
import BaseControl from '../base-control';
class ToggleControl extends Component {
constructor() {
super( ...arguments );
this.onChange = this.onChange.bind( this );
}
onChange( event ) {
if ( this.props.onChange ) {
this.props.onChange( event.target.checked );
}
}
render() {
const { label, checked, help, instanceId } = this.props;
const id = `inspector-toggle-control-${ instanceId }`;
let describedBy, helpLabel;
if ( help ) {
describedBy = id + '__help';
helpLabel = isFunction( help ) ? help( checked ) : help;
}
return (
<BaseControl
id={ id }
help={ helpLabel }
className="components-toggle-control"
>
<FormToggle
id={ id }
checked={ checked }
onChange={ this.onChange }
aria-describedby={ describedBy }
/>
<label
htmlFor={ id }
className="components-toggle-control__label"
>
{ label }
</label>
</BaseControl>
);
}
}
export default withInstanceId( ToggleControl );