2023-12-15 17:34:50 +03:00
|
|
|
import clsx from 'clsx';
|
2023-08-02 18:24:17 +03:00
|
|
|
|
2025-02-20 18:10:53 +03:00
|
|
|
import { globalIDs } from '@/utils/constants';
|
2023-12-21 00:12:24 +03:00
|
|
|
|
2025-02-10 01:32:55 +03:00
|
|
|
import { CheckboxChecked } from '../Icons';
|
|
|
|
import { CProps } from '../props';
|
|
|
|
|
2025-02-04 20:35:55 +03:00
|
|
|
export interface CheckboxProps extends Omit<CProps.Button, 'value' | 'onClick' | 'onChange'> {
|
2024-09-12 16:42:02 +03:00
|
|
|
/** Label to display next to the checkbox. */
|
2023-12-28 14:04:44 +03:00
|
|
|
label?: string;
|
2024-09-12 16:42:02 +03:00
|
|
|
|
|
|
|
/** Indicates whether the checkbox is disabled. */
|
2023-12-28 14:04:44 +03:00
|
|
|
disabled?: boolean;
|
2023-07-15 17:46:19 +03:00
|
|
|
|
2024-09-12 16:42:02 +03:00
|
|
|
/** Current value - `true` or `false`. */
|
2025-02-03 13:14:39 +03:00
|
|
|
value?: boolean;
|
2024-09-12 16:42:02 +03:00
|
|
|
|
|
|
|
/** Callback to set the `value`. */
|
2025-02-04 20:35:55 +03:00
|
|
|
onChange?: (newValue: boolean) => void;
|
2023-09-07 16:30:43 +03:00
|
|
|
}
|
2023-08-02 18:24:17 +03:00
|
|
|
|
2024-09-12 16:42:02 +03:00
|
|
|
/**
|
2024-10-30 21:35:55 +03:00
|
|
|
* Component that allows toggling a boolean value.
|
2024-09-12 16:42:02 +03:00
|
|
|
*/
|
2025-02-07 10:54:47 +03:00
|
|
|
export function Checkbox({
|
2024-03-09 16:40:10 +03:00
|
|
|
disabled,
|
|
|
|
label,
|
|
|
|
title,
|
|
|
|
titleHtml,
|
|
|
|
hideTitle,
|
|
|
|
className,
|
|
|
|
value,
|
2025-02-04 20:35:55 +03:00
|
|
|
onChange,
|
2024-03-09 16:40:10 +03:00
|
|
|
...restProps
|
|
|
|
}: CheckboxProps) {
|
2025-02-04 20:35:55 +03:00
|
|
|
const cursor = disabled ? 'cursor-arrow' : onChange ? 'cursor-pointer' : '';
|
2023-12-15 17:34:50 +03:00
|
|
|
|
2024-04-07 19:45:07 +03:00
|
|
|
function handleClick(event: CProps.EventMouse): void {
|
2023-08-02 18:24:17 +03:00
|
|
|
event.preventDefault();
|
2024-05-23 13:36:16 +03:00
|
|
|
event.stopPropagation();
|
2025-02-04 20:35:55 +03:00
|
|
|
if (disabled || !onChange) {
|
2023-09-07 16:30:43 +03:00
|
|
|
return;
|
2023-08-02 18:24:17 +03:00
|
|
|
}
|
2025-02-04 20:35:55 +03:00
|
|
|
onChange(!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'
|
2024-01-06 03:15:02 +03:00
|
|
|
className={clsx(
|
2025-01-27 15:03:48 +03:00
|
|
|
'flex items-center gap-2', //
|
2024-01-06 03:15:02 +03:00
|
|
|
'outline-none',
|
2024-05-12 13:58:28 +03:00
|
|
|
'focus-frame',
|
2024-01-06 03:15:02 +03:00
|
|
|
cursor,
|
|
|
|
className
|
|
|
|
)}
|
2023-12-28 14:04:44 +03:00
|
|
|
disabled={disabled}
|
|
|
|
onClick={handleClick}
|
2025-02-20 18:10:53 +03:00
|
|
|
data-tooltip-id={!!title || !!titleHtml ? globalIDs.tooltip : undefined}
|
2024-03-08 18:39:08 +03:00
|
|
|
data-tooltip-html={titleHtml}
|
2023-12-28 14:04:44 +03:00
|
|
|
data-tooltip-content={title}
|
2024-03-09 16:40:10 +03:00
|
|
|
data-tooltip-hidden={hideTitle}
|
2023-12-28 14:04:44 +03:00
|
|
|
{...restProps}
|
|
|
|
>
|
|
|
|
<div
|
2024-01-06 03:15:02 +03:00
|
|
|
className={clsx(
|
2025-01-27 15:03:48 +03:00
|
|
|
'max-w-[1rem] min-w-[1rem] h-4', //
|
2025-02-05 12:43:16 +03:00
|
|
|
'pt-[0.05rem] pl-[0.05rem]',
|
2024-12-20 13:36:42 +03:00
|
|
|
'border rounded-sm',
|
2024-12-17 11:38:00 +03:00
|
|
|
'cc-animate-color',
|
2024-01-06 03:15:02 +03:00
|
|
|
{
|
2024-12-17 11:38:00 +03:00
|
|
|
'bg-sec-600 text-sec-0': value !== false,
|
2024-12-17 10:53:01 +03:00
|
|
|
'bg-prim-100': value === false
|
2024-01-06 03:15:02 +03:00
|
|
|
}
|
|
|
|
)}
|
2023-12-28 14:04:44 +03:00
|
|
|
>
|
2024-12-20 13:36:42 +03:00
|
|
|
{value ? <CheckboxChecked /> : null}
|
2023-12-28 14:04:44 +03:00
|
|
|
</div>
|
2024-03-19 19:19:08 +03:00
|
|
|
{label ? <span className={clsx('text-start text-sm whitespace-nowrap select-text', cursor)}>{label}</span> : null}
|
2023-12-28 14:04:44 +03:00
|
|
|
</button>
|
|
|
|
);
|
2023-07-15 17:46:19 +03:00
|
|
|
}
|