|
| 1 | +import * as React from 'react'; |
| 2 | +import clsx from 'clsx'; |
| 3 | +import { useToggle } from '../../../Hooks'; |
| 4 | +import { useFormContext } from '../FormControl'; |
| 5 | +import { callAll } from '../../Utils/AllFunctionsCall'; |
| 6 | +import './CheckBox.scss'; |
| 7 | + |
| 8 | +/** |
| 9 | + * Props for the CheckBox component. |
| 10 | + * |
| 11 | + * Extends standard HTML `<input type="checkbox">` props and includes |
| 12 | + * support for size-based styling and form context integration. |
| 13 | + * |
| 14 | + * @property boxSize - Defines the visual size of the checkbox. Options are `'small'`, `'medium'`, or `'large'`. |
| 15 | + */ |
| 16 | +export interface CheckBoxProps extends React.ComponentPropsWithoutRef<'input'> { |
| 17 | + /** Defines the visual size of the checkbox. */ |
| 18 | + boxSize?: 'small' | 'medium' | 'large'; |
| 19 | +} |
| 20 | + |
| 21 | +/** |
| 22 | + * A reusable checkbox component that supports both controlled and uncontrolled behavior. |
| 23 | + * It also integrates with the `FormControl` context to support validation, error states, |
| 24 | + * and form-level change tracking. |
| 25 | + * |
| 26 | + * @example |
| 27 | + * // Controlled usage |
| 28 | + * <CheckBox checked={isChecked} onChange={(e) => setChecked(e.target.checked)} /> |
| 29 | + * |
| 30 | + * @example |
| 31 | + * // Uncontrolled usage |
| 32 | + * <CheckBox defaultChecked /> |
| 33 | + * |
| 34 | + */ |
| 35 | +function CheckBox({ |
| 36 | + checked, |
| 37 | + boxSize = 'medium', |
| 38 | + className, |
| 39 | + ...props |
| 40 | +}: CheckBoxProps) { |
| 41 | + const { value: checkValue, toggleValue: toggleCheck } = useToggle(checked); |
| 42 | + const { isAlert, inputChange } = useFormContext(); |
| 43 | + |
| 44 | + const HandleKeyDown = (e: React.KeyboardEvent<HTMLInputElement>) => { |
| 45 | + if (e.key === 'Enter') { |
| 46 | + toggleCheck(); |
| 47 | + |
| 48 | + // Manually fire synthetic change for FormControl context |
| 49 | + const syntheticEvent = { |
| 50 | + target: { value: String(!checkValue) }, |
| 51 | + } as React.ChangeEvent<HTMLInputElement>; |
| 52 | + |
| 53 | + if (inputChange) inputChange(syntheticEvent); |
| 54 | + } |
| 55 | + }; |
| 56 | + |
| 57 | + const HandleOnClick = (e: React.MouseEvent<HTMLInputElement>) => { |
| 58 | + toggleCheck(); |
| 59 | + }; |
| 60 | + |
| 61 | + const { onKeyDown, onClick, onChange, ...resetProps } = props; |
| 62 | + |
| 63 | + return ( |
| 64 | + <input |
| 65 | + className={clsx(`checkbox checkbox-${boxSize}`, className, { |
| 66 | + 'checkbox-alert': isAlert, |
| 67 | + })} |
| 68 | + type="checkbox" |
| 69 | + tabIndex={0} |
| 70 | + onKeyDown={callAll(onKeyDown, HandleKeyDown)} |
| 71 | + onClick={callAll(onClick, HandleOnClick)} |
| 72 | + onChange={callAll(onChange, inputChange)} |
| 73 | + value={String(checkValue)} |
| 74 | + checked={checkValue} |
| 75 | + {...resetProps} |
| 76 | + /> |
| 77 | + ); |
| 78 | +} |
| 79 | + |
| 80 | +export default CheckBox; |
0 commit comments