2023-12-15 17:34:50 +03:00
|
|
|
import clsx from 'clsx';
|
2023-09-07 16:30:43 +03:00
|
|
|
import { useMemo } from 'react';
|
|
|
|
|
2024-03-27 15:32:59 +03:00
|
|
|
import { globals } from '@/utils/constants';
|
2023-12-21 00:12:24 +03:00
|
|
|
|
2024-04-03 21:05:53 +03:00
|
|
|
import { CheckboxChecked, CheckboxNull } from '../Icons';
|
2024-04-07 19:45:07 +03:00
|
|
|
import { CProps } from '../props';
|
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'> {
|
2023-12-28 14:04:44 +03:00
|
|
|
value: boolean | null;
|
|
|
|
setValue?: (newValue: boolean | null) => void;
|
2023-09-07 16:30:43 +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) {
|
2023-12-28 14:04:44 +03:00
|
|
|
const cursor = useMemo(() => {
|
2023-09-07 16:30:43 +03:00
|
|
|
if (disabled) {
|
2024-08-15 23:23:45 +03:00
|
|
|
return 'cursor-arrow';
|
2023-09-07 16:30:43 +03:00
|
|
|
} else if (setValue) {
|
|
|
|
return 'cursor-pointer';
|
|
|
|
} else {
|
2023-12-28 14:04:44 +03:00
|
|
|
return '';
|
2023-09-07 16:30:43 +03:00
|
|
|
}
|
|
|
|
}, [disabled, setValue]);
|
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(
|
2024-03-17 19:24:12 +03:00
|
|
|
'flex items-center gap-2', // prettier: split lines
|
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(
|
|
|
|
'w-4 h-4', // prettier: split lines
|
|
|
|
'border rounded-sm',
|
|
|
|
{
|
|
|
|
'clr-primary': value !== false,
|
|
|
|
'clr-app': value === false
|
|
|
|
}
|
|
|
|
)}
|
2023-12-28 14:04:44 +03:00
|
|
|
>
|
|
|
|
{value ? (
|
|
|
|
<div className='mt-[1px] ml-[1px]'>
|
2024-04-03 21:05:53 +03:00
|
|
|
<CheckboxChecked />
|
2023-12-28 14:04:44 +03:00
|
|
|
</div>
|
|
|
|
) : null}
|
|
|
|
{value == null ? (
|
|
|
|
<div className='mt-[1px] ml-[1px]'>
|
2024-04-03 21:05:53 +03:00
|
|
|
<CheckboxNull />
|
2023-12-28 14:04:44 +03:00
|
|
|
</div>
|
|
|
|
) : null}
|
|
|
|
</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;
|