Portal/rsconcept/frontend/src/components/ui/CheckboxTristate.tsx

87 lines
2.1 KiB
TypeScript
Raw Normal View History

2024-06-07 20:17:03 +03:00
import clsx from 'clsx';
2025-01-28 23:23:03 +03:00
import { CheckboxChecked, CheckboxNull } from '@/components/Icons';
import { CProps } from '@/components/props';
2024-06-07 20:17:03 +03:00
import { globals } from '@/utils/constants';
import { CheckboxProps } from './Checkbox';
export interface CheckboxTristateProps extends Omit<CheckboxProps, 'value' | 'setValue'> {
/** Current value - `null`, `true` or `false`. */
2024-06-07 20:17:03 +03:00
value: boolean | null;
/** Callback to set the `value`. */
2024-06-07 20:17:03 +03:00
setValue?: (newValue: boolean | null) => void;
}
/**
2024-10-30 21:35:43 +03:00
* Component that allows toggling among three states: `true`, `false`, and `null`.
*/
2024-06-07 20:17:03 +03:00
function CheckboxTristate({
disabled,
label,
title,
titleHtml,
hideTitle,
className,
value,
setValue,
...restProps
}: CheckboxTristateProps) {
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;