2024-06-07 20:17:03 +03:00
|
|
|
import clsx from 'clsx';
|
|
|
|
|
|
|
|
|
|
import { globals } from '@/utils/constants';
|
|
|
|
|
|
|
|
|
|
import { CheckboxChecked, CheckboxNull } from '../Icons';
|
|
|
|
|
import { CProps } from '../props';
|
|
|
|
|
import { CheckboxProps } from './Checkbox';
|
|
|
|
|
|
|
|
|
|
export interface CheckboxTristateProps extends Omit<CheckboxProps, 'value' | 'setValue'> {
|
2024-09-12 16:41:48 +03:00
|
|
|
/** Current value - `null`, `true` or `false`. */
|
2024-06-07 20:17:03 +03:00
|
|
|
value: boolean | null;
|
2024-09-12 16:41:48 +03:00
|
|
|
|
|
|
|
|
/** Callback to set the `value`. */
|
2024-06-07 20:17:03 +03:00
|
|
|
setValue?: (newValue: boolean | null) => void;
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-12 16:41:48 +03:00
|
|
|
/**
|
2024-10-30 21:35:43 +03:00
|
|
|
* Component that allows toggling among three states: `true`, `false`, and `null`.
|
2024-09-12 16:41:48 +03:00
|
|
|
*/
|
2024-06-07 20:17:03 +03:00
|
|
|
function CheckboxTristate({
|
|
|
|
|
disabled,
|
|
|
|
|
label,
|
|
|
|
|
title,
|
|
|
|
|
titleHtml,
|
|
|
|
|
hideTitle,
|
|
|
|
|
className,
|
|
|
|
|
value,
|
|
|
|
|
setValue,
|
|
|
|
|
...restProps
|
|
|
|
|
}: CheckboxTristateProps) {
|
2024-12-13 21:30:49 +03:00
|
|
|
const cursor = disabled ? 'cursor-arrow' : setValue ? 'cursor-pointer' : '';
|
2024-06-07 20:17:03 +03:00
|
|
|
|
|
|
|
|
function handleClick(event: CProps.EventMouse): void {
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
event.stopPropagation();
|
|
|
|
|
if (disabled || !setValue) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (value === false) {
|
|
|
|
|
setValue(null);
|
|
|
|
|
} else if (value === null) {
|
|
|
|
|
setValue(true);
|
|
|
|
|
} else {
|
|
|
|
|
setValue(false);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<button
|
|
|
|
|
type='button'
|
|
|
|
|
className={clsx(
|
2025-01-23 19:41:31 +03:00
|
|
|
'flex items-center gap-2', //
|
2024-06-07 20:17:03 +03:00
|
|
|
'outline-none',
|
|
|
|
|
'focus-frame',
|
|
|
|
|
cursor,
|
|
|
|
|
className
|
|
|
|
|
)}
|
|
|
|
|
disabled={disabled}
|
|
|
|
|
onClick={handleClick}
|
|
|
|
|
data-tooltip-id={!!title || !!titleHtml ? globals.tooltip : undefined}
|
|
|
|
|
data-tooltip-html={titleHtml}
|
|
|
|
|
data-tooltip-content={title}
|
|
|
|
|
data-tooltip-hidden={hideTitle}
|
|
|
|
|
{...restProps}
|
|
|
|
|
>
|
|
|
|
|
<div
|
|
|
|
|
className={clsx(
|
2025-01-23 19:41:31 +03:00
|
|
|
'w-4 h-4', //
|
2024-12-20 13:36:31 +03:00
|
|
|
'pt-[0.1rem] pl-[0.1rem]',
|
2024-06-07 20:17:03 +03:00
|
|
|
'border rounded-sm',
|
2024-12-17 11:37:42 +03:00
|
|
|
'cc-animate-color',
|
2024-06-07 20:17:03 +03:00
|
|
|
{
|
2024-12-17 11:37:42 +03:00
|
|
|
'bg-sec-600 text-sec-0': value !== false,
|
2024-12-17 10:52:36 +03:00
|
|
|
'bg-prim-100': value === false
|
2024-06-07 20:17:03 +03:00
|
|
|
}
|
|
|
|
|
)}
|
|
|
|
|
>
|
2024-12-20 13:36:31 +03:00
|
|
|
{value ? <CheckboxChecked /> : null}
|
|
|
|
|
{value == null ? <CheckboxNull /> : null}
|
2024-06-07 20:17:03 +03:00
|
|
|
</div>
|
|
|
|
|
{label ? <span className={clsx('text-start text-sm whitespace-nowrap select-text', cursor)}>{label}</span> : null}
|
|
|
|
|
</button>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default CheckboxTristate;
|