2023-12-15 17:34:50 +03:00
|
|
|
import clsx from 'clsx';
|
2023-09-07 16:30:43 +03:00
|
|
|
|
2025-01-28 23:23:42 +03:00
|
|
|
import { CheckboxChecked, CheckboxNull } from '@/components/Icons';
|
|
|
|
import { CProps } from '@/components/props';
|
2024-03-27 15:32:59 +03:00
|
|
|
import { globals } from '@/utils/constants';
|
2023-12-21 00:12:24 +03:00
|
|
|
|
2023-09-07 16:30:43 +03:00
|
|
|
import { CheckboxProps } from './Checkbox';
|
|
|
|
|
2024-01-04 19:30:10 +03:00
|
|
|
export interface CheckboxTristateProps extends Omit<CheckboxProps, 'value' | 'setValue'> {
|
2024-09-12 16:42:02 +03:00
|
|
|
/** Current value - `null`, `true` or `false`. */
|
2023-12-28 14:04:44 +03:00
|
|
|
value: boolean | null;
|
2024-09-12 16:42:02 +03:00
|
|
|
|
|
|
|
/** Callback to set the `value`. */
|
2023-12-28 14:04:44 +03:00
|
|
|
setValue?: (newValue: boolean | null) => void;
|
2023-09-07 16:30:43 +03:00
|
|
|
}
|
|
|
|
|
2024-09-12 16:42:02 +03:00
|
|
|
/**
|
2024-10-30 21:35:55 +03:00
|
|
|
* Component that allows toggling among three states: `true`, `false`, and `null`.
|
2024-09-12 16:42:02 +03:00
|
|
|
*/
|
2024-01-04 19:30:10 +03:00
|
|
|
function CheckboxTristate({
|
|
|
|
disabled,
|
|
|
|
label,
|
|
|
|
title,
|
2024-03-08 18:39:08 +03:00
|
|
|
titleHtml,
|
2024-03-09 16:40:10 +03:00
|
|
|
hideTitle,
|
2024-01-04 19:30:10 +03:00
|
|
|
className,
|
|
|
|
value,
|
|
|
|
setValue,
|
|
|
|
...restProps
|
|
|
|
}: CheckboxTristateProps) {
|
2024-12-13 21:31:09 +03:00
|
|
|
const cursor = disabled ? 'cursor-arrow' : setValue ? '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-09-07 16:30:43 +03:00
|
|
|
event.preventDefault();
|
2024-05-23 13:36:16 +03:00
|
|
|
event.stopPropagation();
|
2023-09-07 16:30:43 +03:00
|
|
|
if (disabled || !setValue) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (value === false) {
|
2023-12-28 14:04:44 +03:00
|
|
|
setValue(null);
|
2023-09-09 20:36:55 +03:00
|
|
|
} else if (value === null) {
|
|
|
|
setValue(true);
|
2023-09-07 16:30:43 +03:00
|
|
|
} else {
|
2023-09-09 20:36:55 +03:00
|
|
|
setValue(false);
|
2023-09-07 16:30:43 +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}
|
2024-03-27 15:32:59 +03:00
|
|
|
data-tooltip-id={!!title || !!titleHtml ? globals.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
|
|
|
'w-4 h-4', //
|
2024-12-20 13:36:42 +03:00
|
|
|
'pt-[0.1rem] pl-[0.1rem]',
|
2024-01-06 03:15:02 +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}
|
|
|
|
{value == null ? <CheckboxNull /> : 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-09-07 16:30:43 +03:00
|
|
|
}
|
|
|
|
|
2024-01-04 19:30:10 +03:00
|
|
|
export default CheckboxTristate;
|