1
+ import React , { useState } from 'react'
2
+ import PropTypes from 'prop-types' ;
3
+ import { SIZES } from './constants' ;
4
+ import './index.css' ;
5
+
6
+ export const Toggle = ( props ) => {
7
+ const { className, onChange, isChecked, ariaLabel } = props ;
8
+ const [ isCheckedFromState , setIsCheckedFromState ] = useState ( isChecked ) ;
9
+
10
+ function onChangeLocal ( ) {
11
+ setIsCheckedFromState ( ! isCheckedFromState ) ;
12
+ onChange ( e ) ;
13
+ }
14
+
15
+ return < label className = { `switch ${ className } ` } aria-label = { ariaLabel } >
16
+ < input type = "checkbox" checked = { isCheckedFromState } onChange = { onChangeLocal } />
17
+ < span className = "rounded-3xl" > </ span >
18
+ </ label >
19
+ } ;
20
+
21
+ Toggle . defaultProps = {
22
+ isChecked : false ,
23
+ onChange : ( ) => { } ,
24
+ ariaLabel : "toggle" ,
25
+ } ;
26
+
27
+ Toggle . propType = {
28
+ onChange : PropTypes . func ,
29
+ isChecked : PropTypes . bool ,
30
+ className : PropTypes . string ,
31
+ ariaLabel : PropTypes . string ,
32
+ size : PropTypes . oneOf ( SIZES ) ,
33
+ } ;
34
+
35
+ /* class Toggle extends React.PureComponent {
36
+ static defaultProps = {
37
+ isChecked: false,
38
+ onChange: () => { },
39
+ }
40
+
41
+ static propType = {
42
+ onChange: PropTypes.func,
43
+ isChecked: PropTypes.bool,
44
+ className: PropTypes.string,
45
+ ariaLabel: PropTypes.string,
46
+ size: PropTypes.oneOf(SIZES),
47
+ }
48
+
49
+ constructor(props) {
50
+ super(props);
51
+ this.state = {
52
+ isChecked: props.isChecked,
53
+ }
54
+ }
55
+
56
+ onChange = (e) => {
57
+ const { onChange } = this.props;
58
+ this.setState({ isChecked: !this.state.isChecked });
59
+ onChange(e);
60
+ }
61
+
62
+ render() {
63
+ const { className, ariaLabel } = this.props;
64
+ const { isChecked } = this.state;
65
+
66
+ return (
67
+ <label className={`switch ${className}`} aria-label={ariaLabel}>
68
+ <input type="checkbox" checked={isChecked} onChange={this.onChange} />
69
+ <span className="rounded-3xl"></span>
70
+ </label>
71
+ )
72
+ }
73
+ } */
0 commit comments