Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 | 1x 1x 1x 1x 1x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 4x 3x 3x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 28x 28x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 7x 7x 7x 34x 34x 34x 34x 34x 8x 8x 6x 6x 6x 6x 6x 6x 6x 8x 8x 2x 2x 2x 2x 2x 2x 8x 8x 34x 34x 34x 34x 1x 1x 1x | import React, { forwardRef, useId } from 'react';
import { CheckboxProps } from '@/types/ui';
export const Checkbox = forwardRef<HTMLInputElement, CheckboxProps>(
(
{
label,
checked,
onChange,
disabled = false,
error,
helperText,
state = 'default',
size = 'md',
className = '',
labelClassName = '',
id,
...props
},
ref
) => {
const generatedId = useId();
// Derive state from error prop if provided
const effectiveState = error ? 'error' : state;
// Size classes
const sizeClasses = {
sm: 'w-3.5 h-3.5',
md: 'w-4 h-4',
lg: 'w-5 h-5'};
// State-based border colors
const stateClasses = {
default: 'border-gray-3',
success: 'border-green',
error: 'border-red',
warning: 'border-yellow'};
// Text size for label
const labelSizeClasses = {
sm: 'text-sm',
md: 'text-base',
lg: 'text-lg'};
// Helper text size
const helperTextSizeClasses = {
sm: 'text-xs',
md: 'text-sm',
lg: 'text-base'};
const handleChange = (e: React.ChangeEvent<HTMLInputElement>) => {
if (!disabled && onChange) {
onChange(e.target.checked);
}
};
const checkboxId = id || `checkbox-${generatedId}`;
return (
<div className="flex flex-col">
<label
htmlFor={checkboxId}
className={`flex items-center gap-2 ${
disabled ? 'cursor-not-allowed opacity-50' : 'cursor-pointer'
} ${labelClassName}`}
>
<input
ref={ref}
type="checkbox"
id={checkboxId}
checked={checked}
onChange={handleChange}
disabled={disabled}
aria-invalid={effectiveState === 'error'}
aria-describedby={
error
? `${checkboxId}-error`
: helperText
? `${checkboxId}-helper`
: undefined
}
className={`
${sizeClasses[size]}
rounded
${stateClasses[effectiveState]}
text-blue
focus:ring-2
focus:ring-blue
focus:ring-offset-1
dark:focus:ring-offset-gray-900
${disabled ? 'cursor-not-allowed bg-gray-2 dark:bg-gray-700' : 'bg-white dark:bg-gray-800 cursor-pointer'}
${className}
`}
{...props}
/>
{label && (
<span className={`text-dark dark:text-gray-100 ${labelSizeClasses[size]} ${disabled ? 'opacity-50' : ''}`}>
{label}
</span>
)}
</label>
{/* Helper text or error message */}
{(helperText || error) && (
<div className="mt-1 ml-6">
{error && (
<p
id={`${checkboxId}-error`}
className={`text-red ${helperTextSizeClasses[size]}`}
role="alert"
>
{error}
</p>
)}
{helperText && !error && (
<p
id={`${checkboxId}-helper`}
className={`text-dark-5 dark:text-gray-400 ${helperTextSizeClasses[size]}`}
>
{helperText}
</p>
)}
</div>
)}
</div>
);
}
);
Checkbox.displayName = 'Checkbox';
|