'use client' import { forwardRef } from 'react' import * as React from 'react' import { CheckBoldIcon, HyphenBoldIcon } from '@channel.io/bezier-icons' import * as CheckboxPrimitive from '@radix-ui/react-checkbox' import classNames from 'classnames' import useId from '~/src/hooks/useId' import { getFormFieldSizeClassName } from '~/src/types/props-helpers' import { BaseButton } from '~/src/components/BaseButton' import { useFormFieldProps } from '~/src/components/FormControl' import { Icon } from '~/src/components/Icon' import { Text } from '~/src/components/Text' import { type CheckboxProps, type CheckedState } from './Checkbox.types' import styles from './Checkbox.module.scss' interface CheckIconProps { style: React.CSSProperties 'data-state': 'checked' | 'unchecked' | 'indeterminate' 'data-disabled': boolean | undefined } /* NOTE: Props are injected at runtime by `CheckboxPrimitive.Indicator`. */ const CheckIcon = forwardRef( function CheckIcon(props, forwardedRef) { // eslint-disable-next-line react/destructuring-assignment const state = props['data-state'] const isUnchecked = state === 'unchecked' const isIndeterminate = state === 'indeterminate' return ( ) } ) function CheckboxImpl( { children, className, checked, size = 'm', id: idProp, ...rest }: CheckboxProps, forwardedRef: React.Ref ) { const { id: formFieldId, hasError, ...formFieldProps } = useFormFieldProps(rest) const id = useId(idProp ?? formFieldId, 'bezier-checkbox') const iconSize = size === 's' ? 'xxs' : 'xs' return (
{/* @ts-expect-error */} {children && ( {children} )}
) } /* NOTE: This is a workaround to avoid infinite type recursion when directly using `ReturnType` */ type ReturnTypeOfCheckboxImpl = ReturnType< typeof CheckboxImpl > /** * `Checkbox` is a control that allows the user to toggle between checked and not checked. * It can be used with labels or standalone. * @example * * ```tsx * const [checked, setChecked] = useState(false) * // Controlled / With label * * Label * * // Controlled / Standalone * * // Uncontrolled * * Label * * ``` */ export const Checkbox = forwardRef(CheckboxImpl) as < Checked extends CheckedState, >( props: CheckboxProps & { ref?: React.ForwardedRef } ) => ReturnTypeOfCheckboxImpl