Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(Checkbox): added indeterminate state #93

Merged
merged 1 commit into from Apr 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/Checkbox/Checkbox.stories.tsx
Expand Up @@ -9,13 +9,18 @@ export default {
component: Checkbox,
} as Meta

export const Template: Story<CheckboxProps> = (args) => {
const Template: Story<CheckboxProps> = (args) => {
return <Checkbox {...args} />
}

export const Default = Template.bind({})
Default.args = {}

export const Indeterminate = Template.bind({})
Indeterminate.args = {
indeterminate: true
}

const FormTemplate: Story<CheckboxProps> = (args) => {
return (
<Form className="shadow bg-base-200 w-64 rounded-lg p-4">
Expand Down
26 changes: 24 additions & 2 deletions src/Checkbox/Checkbox.tsx
@@ -1,4 +1,9 @@
import React, { forwardRef } from 'react'
import React, {
forwardRef,
useEffect,
useImperativeHandle,
useRef
} from 'react'
import clsx from 'clsx'
import { twMerge } from 'tailwind-merge'

Expand All @@ -15,6 +20,7 @@ export type CheckboxProps = Omit<
IComponentBaseProps & {
color?: ComponentBrandColors
size?: ComponentSize
indeterminate?: boolean
}

const Checkbox = forwardRef<HTMLInputElement, CheckboxProps>(
Expand All @@ -24,6 +30,7 @@ const Checkbox = forwardRef<HTMLInputElement, CheckboxProps>(
defaultChecked = false,
color,
size,
indeterminate,
dataTheme,
className,
...props
Expand All @@ -39,10 +46,25 @@ const Checkbox = forwardRef<HTMLInputElement, CheckboxProps>(
})
)

const checkboxRef = useRef<HTMLInputElement>(null)
useImperativeHandle(ref, () => checkboxRef.current as HTMLInputElement)

useEffect(() => {
if (!checkboxRef.current) {
return
}

if (indeterminate) {
checkboxRef.current.indeterminate = true
} else {
checkboxRef.current.indeterminate = false
}
}, [indeterminate])

return (
<input
{...props}
ref={ref}
ref={checkboxRef}
type="checkbox"
checked={checked}
defaultChecked={defaultChecked}
Expand Down