Skip to content
This repository has been archived by the owner on Nov 29, 2023. It is now read-only.

Commit

Permalink
BUG: Finish the correction of Checkbox (sc2328)
Browse files Browse the repository at this point in the history
  • Loading branch information
guerrato committed Jan 25, 2022
1 parent 1d0b9f4 commit 6b701af
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions src/lib/components/inputs/Checkbox/Checkbox.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import _ from 'lodash'
import React, {
ChangeEvent,
ChangeEventHandler,
forwardRef,
ReactNode,
useEffect,
useImperativeHandle,
useState
} from 'react'
Expand All @@ -16,7 +16,7 @@ export type CheckboxProps = {
id?: string
label?: string
checked?: boolean
onChange?: (checked: boolean) => void
onChange?: ChangeEventHandler<HTMLInputElement>
checkedContent?: ReactNode
uncheckedContent?: ReactNode
typography?: TypographyStyle
Expand All @@ -42,7 +42,7 @@ export const Checkbox = forwardRef(
}: CheckboxProps,
ref
) => {
const [isChecked, setIsChecked] = useState(checked)
const [isChecked, setIsChecked] = useState<boolean>(checked)

useImperativeHandle(ref, () => {
return {
Expand All @@ -51,9 +51,10 @@ export const Checkbox = forwardRef(
}
})

useEffect(() => {
props.onChange && props.onChange.call(isChecked)
}, [isChecked])
const handleChange = (e: ChangeEvent<HTMLInputElement>) => {
setIsChecked(e.target && e.target.checked)
props.onChange && props.onChange(e)
}

const theme = useTheme()
const styles: SpanProps = {
Expand All @@ -75,11 +76,11 @@ export const Checkbox = forwardRef(
<CheckInput
id={id}
className={props.className}
checked={checked}
onChange={(e) => setIsChecked(e.target.checked)}
checked={isChecked}
onChange={handleChange}
{...styles}
/>
<SpanEl {...styles}>{checked ? checkedContent : uncheckedContent}</SpanEl>
<SpanEl {...styles}>{isChecked ? checkedContent : uncheckedContent}</SpanEl>
</Wrapper>
)
}
Expand Down

0 comments on commit 6b701af

Please sign in to comment.