import * as React from 'react'; import cx from 'classnames'; import Checkbox from '../form-elements/checkbox/Checkbox'; import generateRandomString from '../../js/generateRandomString'; export interface CardCheckboxPropsType extends Omit, 'onChange'> { /** * Optional string. Variant of the card. Default is 'outline'. */ variant?: 'solid' | 'outline'; color?: 'light' | 'dark'; /** * Optional string. Additional class names. */ className?: string; /** * Optional React.ReactNode. Children of the card. This is the place where label should be used and connected to the card. * @example Card content */ children?: React.ReactNode; /** * Optional string. Width of the card. * @example **/ width?: React.CSSProperties['width']; /** * Optional string. Height of the card. * @example */ height?: React.CSSProperties['height']; /** * Optional object. Inline styles. * @example */ style?: React.CSSProperties; /** * Optional boolean. Whether the checkbox is checked. */ checked?: boolean; /** * Optional boolean. Whether the checkbox is checked by default. Only works when `checked` is not defined. */ defaultChecked?: boolean; /** * Optional boolean. Whether the checkbox is disabled. */ disabled?: boolean; /** * Optional string. ID of the checkbox. */ id?: string; /** * Sets whether the checkbox is displayed as indeterminate. Note: this prop doesn't modify the `checked` property. * @example * @default false */ indeterminate?: boolean; /** * Optional boolean. Whether the checkbox is invalid. * @default */ invalid?: boolean; /** * Optional boolean. Whether the checkbox is required. * @default */ required?: boolean; /** * Value of the Card.Checkbox input. * @example */ value?: string; /** * Name of the Card.Checkbox input. * @example */ name?: string; /** * Function called whenever the state of the checkbox changes. */ onChange?: (e: React.ChangeEvent) => void; /** * Optional string. ID of the element that labels the Radio. * @example **/ 'aria-labelledby'?: string; /** * Optional string. ID of the element that describes the Radio. * @example **/ 'aria-describedby'?: string; } type CardCheckboxContextType = { checked: boolean; disabled: boolean; indeterminate: boolean; }; export const CardCheckboxContext = React.createContext( { checked: false, disabled: false, indeterminate: false, } ); export const CardCheckboxRoot = React.forwardRef< HTMLInputElement, CardCheckboxPropsType >( ( { variant = 'outline', color = 'dark', className, children, width, height, style, // checkbox related props checked, defaultChecked = false, disabled, id, indeterminate, invalid = false, required = false, value, name, onChange, 'aria-labelledby': ariaLabelledBy, 'aria-describedby': ariaDescribedBy, ...props }: CardCheckboxPropsType, ref ) => { const isControlled = checked !== undefined; const [isChecked, setIsChecked] = React.useState( isControlled ? checked : defaultChecked ); const cardId = React.useMemo(() => id || generateRandomString(), [id]); const cssVariables = { '--card-width': width, '--card-height': height, }; const onInputChange = React.useCallback( // @ts-ignore TS7006 e => { if (!isControlled) { setIsChecked(val => !val); } if (onChange) onChange(e); }, [onChange, isControlled] ); React.useEffect(() => { if (isControlled) { setIsChecked(checked); } }, [checked, isControlled]); return ( ); } ); export interface CardCheckboxIndicatorPropsType { slot?: | 'top-left' | 'center-left' | 'bottom-left' | 'top-right' | 'center-right' | 'bottom-right'; style?: React.CSSProperties; className?: string; } export const CardCheckboxIndicator = ({ slot = 'top-left', style, className, }: CardCheckboxIndicatorPropsType) => { const {checked, disabled, indeterminate} = React.useContext(CardCheckboxContext); return (
); }; const CardCheckbox = Object.assign(CardCheckboxRoot, { Indicator: CardCheckboxIndicator, }); CardCheckbox.displayName = 'CardCheckbox'; CardCheckboxIndicator.displayName = 'CardCheckbox.Indicator'; export default CardCheckbox;