Skip to content

Commit d460ab5

Browse files
committed
Simplifies onChange handling
1 parent e189bd7 commit d460ab5

File tree

2 files changed

+8
-17
lines changed

2 files changed

+8
-17
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@accessible/checkbox",
3-
"version": "3.0.0",
3+
"version": "3.1.0",
44
"homepage": "https://github.com/accessible-ui/checkbox#readme",
55
"repository": "github:accessible-ui/checkbox",
66
"bugs": "https://github.com/accessible-ui/checkbox/issues",

src/index.tsx

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,6 @@ import VisuallyHidden from '@accessible/visually-hidden'
33
import useSwitch from '@react-hook/switch'
44
import clsx from 'clsx'
55

6-
const __DEV__ =
7-
typeof process !== 'undefined' && process.env.NODE_ENV !== 'production'
8-
96
const noop = () => {}
107

118
export const CheckboxContext = React.createContext<CheckboxContextValue>({
@@ -52,9 +49,6 @@ export const Checkbox: React.FC<CheckboxProps> = React.forwardRef<
5249
const [focused, setFocused] = React.useState<boolean>(false)
5350
const checked =
5451
controlledChecked === void 0 ? switchChecked : controlledChecked
55-
const prevChecked = React.useRef<boolean>(checked)
56-
const storedOnChange = React.useRef(onChange)
57-
storedOnChange.current = onChange
5852
const context = React.useMemo(
5953
() => ({
6054
checked: checked as boolean,
@@ -69,27 +63,24 @@ export const Checkbox: React.FC<CheckboxProps> = React.forwardRef<
6963
// @ts-ignore
7064
children = typeof children === 'function' ? children(context) : children
7165

72-
React.useEffect(() => {
73-
prevChecked.current !== switchChecked &&
74-
storedOnChange.current?.(switchChecked)
75-
prevChecked.current = switchChecked
76-
}, [switchChecked])
77-
7866
return (
7967
<CheckboxContext.Provider value={context}>
8068
<VisuallyHidden>
8169
<input
8270
type="checkbox"
8371
checked={checked}
8472
ref={ref}
85-
onChange={() => toggle()}
73+
onChange={(e) => {
74+
toggle()
75+
onChange?.(e.target.checked)
76+
}}
8677
onFocus={(e) => {
87-
onFocus?.(e)
8878
setFocused(true)
79+
onFocus?.(e)
8980
}}
9081
onBlur={(e) => {
91-
onBlur?.(e)
9282
setFocused(false)
83+
onBlur?.(e)
9384
}}
9485
disabled={disabled}
9586
{...props}
@@ -190,7 +181,7 @@ export interface CheckboxControls {
190181
}
191182

192183
/* istanbul ignore next */
193-
if (__DEV__) {
184+
if (typeof process !== 'undefined' && process.env.NODE_ENV !== 'production') {
194185
Checkbox.displayName = 'Checkbox'
195186
Checked.displayName = 'Checked'
196187
Unchecked.displayName = 'Unchecked'

0 commit comments

Comments
 (0)