1
+ import noop = require( 'lodash/noop' ) ;
1
2
import * as React from 'react' ;
2
3
3
4
import { Box } from './Box' ;
4
5
import { Flex } from './Flex' ;
5
6
import { Icon } from './Icon' ;
6
- import { Input } from './Input' ;
7
7
import { styled } from './utils' ;
8
8
9
9
export interface ICheckboxProps {
@@ -16,63 +16,48 @@ export interface ICheckboxProps {
16
16
onChange ?: ( checked : boolean ) => void ;
17
17
}
18
18
19
- export interface ICheckboxState {
20
- checked ?: boolean ;
21
- }
22
-
23
- export class BasicCheckbox extends React . Component < ICheckboxProps , ICheckboxState > {
24
- constructor ( props : ICheckboxProps ) {
25
- super ( props ) ;
26
- this . state = { checked : this . props . checked } ;
27
- }
19
+ export const BasicCheckbox = ( props : ICheckboxProps ) => {
20
+ const { id, className, width, height, disabled, onChange = noop } = props ;
28
21
29
- private onChange = ( event : React . ChangeEvent < HTMLInputElement > ) => {
30
- this . setState ( { checked : event . target . checked } ) ;
22
+ const [ checked , setValue ] = React . useState ( props . checked || false ) ;
23
+ const isChecked = props . hasOwnProperty ( ' checked' ) ? props . checked : checked ;
31
24
32
- if ( this . props . onChange ) {
33
- this . props . onChange ( event . target . checked ) ;
34
- }
25
+ const handleChange = ( event : React . ChangeEvent < HTMLInputElement > ) => {
26
+ setValue ( event . target . checked ) ;
27
+ onChange ( event . target . checked ) ;
35
28
} ;
36
29
37
- public render ( ) {
38
- const { checked : stateChecked } = this . state ;
39
- const { id, className, width, height, disabled, checked : propsChecked } = this . props ;
40
-
41
- const checked = this . props . hasOwnProperty ( 'checked' ) ? propsChecked : stateChecked ;
42
-
43
- return (
44
- < Box as = "label" display = "inline-block" id = { id } className = { className } >
45
- < Input
46
- type = "checkbox"
47
- checked = { checked || false }
48
- disabled = { disabled }
49
- onChange = { this . onChange }
50
- position = "absolute"
51
- css = { { clip : 'rect(1px, 1px, 1px, 1px)' } }
52
- />
53
- < Flex
54
- as = "span"
55
- display = "block"
56
- m = "none"
57
- p = "none"
58
- radius = "md"
59
- items = "center"
60
- justify = "center"
61
- bg = { checked ? 'toggle.checked.bg' : 'toggle.bg' }
62
- cursor = { disabled ? 'not-allowed' : 'pointer' }
63
- width = { width || '20px' }
64
- height = { height || '20px' }
65
- opacity = { disabled ? 0.6 : 1 }
66
- css = { {
67
- fontSize : '14px' ,
68
- transition : 'background-color .15s ease-in-out' ,
69
- } }
70
- >
71
- { checked && < Icon icon = "check" fg = "toggle.checked.fg" /> }
72
- </ Flex >
73
- </ Box >
74
- ) ;
75
- }
76
- }
30
+ return (
31
+ < Box as = "label" display = "inline-block" id = { id } className = { className } >
32
+ < input
33
+ type = "checkbox"
34
+ checked = { isChecked || false }
35
+ disabled = { disabled }
36
+ onChange = { handleChange }
37
+ style = { { position : 'absolute' , clip : 'rect(1px, 1px, 1px, 1px)' } }
38
+ />
39
+ < Flex
40
+ as = "span"
41
+ display = "block"
42
+ m = "none"
43
+ p = "none"
44
+ radius = "md"
45
+ items = "center"
46
+ justify = "center"
47
+ bg = { isChecked ? 'toggle.checked.bg' : 'toggle.bg' }
48
+ cursor = { disabled ? 'not-allowed' : 'pointer' }
49
+ width = { width || '20px' }
50
+ height = { height || '20px' }
51
+ opacity = { disabled ? 0.6 : 1 }
52
+ css = { {
53
+ fontSize : '14px' ,
54
+ transition : 'background-color .15s ease-in-out' ,
55
+ } }
56
+ >
57
+ { isChecked && < Icon icon = "check" fg = "toggle.checked.fg" /> }
58
+ </ Flex >
59
+ </ Box >
60
+ ) ;
61
+ } ;
77
62
78
63
export const Checkbox = styled < ICheckboxProps > ( BasicCheckbox as any ) `` ;
0 commit comments