2023-12-15 17:34:50 +03:00
|
|
|
import clsx from 'clsx';
|
2023-09-07 16:30:43 +03:00
|
|
|
import { useMemo } from 'react';
|
2023-08-02 18:24:17 +03:00
|
|
|
|
2023-12-21 00:12:24 +03:00
|
|
|
import { globalIDs } from '@/utils/constants';
|
|
|
|
|
2023-09-10 20:17:18 +03:00
|
|
|
import { CheckboxCheckedIcon } from '../Icons';
|
2023-12-18 19:42:27 +03:00
|
|
|
import { CProps } from '../props';
|
2023-07-15 17:46:19 +03:00
|
|
|
import Label from './Label';
|
|
|
|
|
2023-12-28 14:04:44 +03:00
|
|
|
export interface CheckboxProps extends Omit<CProps.Button, 'value' | 'onClick'> {
|
|
|
|
label?: string;
|
|
|
|
disabled?: boolean;
|
2023-07-15 17:46:19 +03:00
|
|
|
|
2023-12-28 14:04:44 +03:00
|
|
|
value: boolean;
|
|
|
|
setValue?: (newValue: boolean) => void;
|
2023-09-07 16:30:43 +03:00
|
|
|
}
|
2023-08-02 18:24:17 +03:00
|
|
|
|
2023-12-28 14:04:44 +03:00
|
|
|
function Checkbox({ id, disabled, label, title, className, value, setValue, ...restProps }: CheckboxProps) {
|
|
|
|
const cursor = useMemo(() => {
|
2023-09-07 16:30:43 +03:00
|
|
|
if (disabled) {
|
|
|
|
return 'cursor-not-allowed';
|
|
|
|
} else if (setValue) {
|
|
|
|
return 'cursor-pointer';
|
|
|
|
} else {
|
2023-12-15 17:34:50 +03:00
|
|
|
return '';
|
2023-09-07 16:30:43 +03:00
|
|
|
}
|
|
|
|
}, [disabled, setValue]);
|
2023-12-15 17:34:50 +03:00
|
|
|
|
2023-08-29 15:17:16 +03:00
|
|
|
function handleClick(event: React.MouseEvent<HTMLButtonElement, MouseEvent>): void {
|
2023-08-02 18:24:17 +03:00
|
|
|
event.preventDefault();
|
2023-09-07 16:30:43 +03:00
|
|
|
if (disabled || !setValue) {
|
|
|
|
return;
|
2023-08-02 18:24:17 +03:00
|
|
|
}
|
2023-09-07 16:30:43 +03:00
|
|
|
setValue(!value);
|
2023-09-04 20:37:55 +03:00
|
|
|
}
|
2023-08-02 18:24:17 +03:00
|
|
|
|
2023-07-15 17:46:19 +03:00
|
|
|
return (
|
2023-12-28 14:04:44 +03:00
|
|
|
<button
|
|
|
|
type='button'
|
|
|
|
id={id}
|
|
|
|
className={clsx('flex items-center gap-2', 'outline-none', 'text-start', cursor, className)}
|
|
|
|
disabled={disabled}
|
|
|
|
onClick={handleClick}
|
|
|
|
data-tooltip-id={title ? globalIDs.tooltip : undefined}
|
|
|
|
data-tooltip-content={title}
|
|
|
|
{...restProps}
|
|
|
|
>
|
|
|
|
<div
|
|
|
|
className={clsx('max-w-[1rem] min-w-[1rem] h-4', 'border rounded-sm', {
|
|
|
|
'clr-primary': value !== false,
|
|
|
|
'clr-app': value === false
|
|
|
|
})}
|
|
|
|
>
|
|
|
|
{value ? (
|
|
|
|
<div className='mt-[1px] ml-[1px]'>
|
|
|
|
<CheckboxCheckedIcon />
|
|
|
|
</div>
|
|
|
|
) : null}
|
|
|
|
</div>
|
|
|
|
<Label className={cursor} text={label} htmlFor={id} />
|
|
|
|
</button>
|
|
|
|
);
|
2023-07-15 17:46:19 +03:00
|
|
|
}
|
|
|
|
|
2023-12-28 14:04:44 +03:00
|
|
|
export default Checkbox;
|