@@ -3,9 +3,6 @@ import VisuallyHidden from '@accessible/visually-hidden'
3
3
import useSwitch from '@react-hook/switch'
4
4
import clsx from 'clsx'
5
5
6
- const __DEV__ =
7
- typeof process !== 'undefined' && process . env . NODE_ENV !== 'production'
8
-
9
6
const noop = ( ) => { }
10
7
11
8
export const CheckboxContext = React . createContext < CheckboxContextValue > ( {
@@ -52,9 +49,6 @@ export const Checkbox: React.FC<CheckboxProps> = React.forwardRef<
52
49
const [ focused , setFocused ] = React . useState < boolean > ( false )
53
50
const checked =
54
51
controlledChecked === void 0 ? switchChecked : controlledChecked
55
- const prevChecked = React . useRef < boolean > ( checked )
56
- const storedOnChange = React . useRef ( onChange )
57
- storedOnChange . current = onChange
58
52
const context = React . useMemo (
59
53
( ) => ( {
60
54
checked : checked as boolean ,
@@ -69,27 +63,24 @@ export const Checkbox: React.FC<CheckboxProps> = React.forwardRef<
69
63
// @ts -ignore
70
64
children = typeof children === 'function' ? children ( context ) : children
71
65
72
- React . useEffect ( ( ) => {
73
- prevChecked . current !== switchChecked &&
74
- storedOnChange . current ?.( switchChecked )
75
- prevChecked . current = switchChecked
76
- } , [ switchChecked ] )
77
-
78
66
return (
79
67
< CheckboxContext . Provider value = { context } >
80
68
< VisuallyHidden >
81
69
< input
82
70
type = "checkbox"
83
71
checked = { checked }
84
72
ref = { ref }
85
- onChange = { ( ) => toggle ( ) }
73
+ onChange = { ( e ) => {
74
+ toggle ( )
75
+ onChange ?.( e . target . checked )
76
+ } }
86
77
onFocus = { ( e ) => {
87
- onFocus ?.( e )
88
78
setFocused ( true )
79
+ onFocus ?.( e )
89
80
} }
90
81
onBlur = { ( e ) => {
91
- onBlur ?.( e )
92
82
setFocused ( false )
83
+ onBlur ?.( e )
93
84
} }
94
85
disabled = { disabled }
95
86
{ ...props }
@@ -190,7 +181,7 @@ export interface CheckboxControls {
190
181
}
191
182
192
183
/* istanbul ignore next */
193
- if ( __DEV__ ) {
184
+ if ( typeof process !== 'undefined' && process . env . NODE_ENV !== 'production' ) {
194
185
Checkbox . displayName = 'Checkbox'
195
186
Checked . displayName = 'Checked'
196
187
Unchecked . displayName = 'Unchecked'
0 commit comments