ConceptPortal-public/rsconcept/frontend/src/components/ui/Checkbox.tsx

88 lines
2.1 KiB
TypeScript
Raw Normal View History

import clsx from 'clsx';
import { globals } from '@/utils/constants';
2023-12-21 00:12:24 +03:00
import { CheckboxChecked } from '../Icons';
import { CProps } from '../props';
2023-07-15 17:46:19 +03:00
2023-12-28 14:04:44 +03:00
export interface CheckboxProps extends Omit<CProps.Button, 'value' | 'onClick'> {
/** Label to display next to the checkbox. */
2023-12-28 14:04:44 +03:00
label?: string;
/** Indicates whether the checkbox is disabled. */
2023-12-28 14:04:44 +03:00
disabled?: boolean;
2023-07-15 17:46:19 +03:00
/** Current value - `true` or `false`. */
2023-12-28 14:04:44 +03:00
value: boolean;
/** Callback to set the `value`. */
2023-12-28 14:04:44 +03:00
setValue?: (newValue: boolean) => void;
2023-09-07 16:30:43 +03:00
}
/**
2024-10-30 21:35:55 +03:00
* Component that allows toggling a boolean value.
*/
2024-03-09 16:40:10 +03:00
function Checkbox({
disabled,
label,
title,
titleHtml,
hideTitle,
className,
value,
setValue,
...restProps
}: CheckboxProps) {
const cursor = disabled ? 'cursor-arrow' : setValue ? 'cursor-pointer' : '';
function handleClick(event: CProps.EventMouse): void {
event.preventDefault();
2024-05-23 13:36:16 +03:00
event.stopPropagation();
2023-09-07 16:30:43 +03:00
if (disabled || !setValue) {
return;
}
2023-09-07 16:30:43 +03:00
setValue(!value);
2023-09-04 20:37:55 +03:00
}
2023-07-15 17:46:19 +03:00
return (
2023-12-28 14:04:44 +03:00
<button
type='button'
className={clsx(
'flex items-center gap-2', // prettier: split lines
'outline-none',
2024-05-12 13:58:28 +03:00
'focus-frame',
cursor,
className
)}
2023-12-28 14:04:44 +03:00
disabled={disabled}
onClick={handleClick}
data-tooltip-id={!!title || !!titleHtml ? globals.tooltip : undefined}
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
className={clsx(
'max-w-[1rem] min-w-[1rem] h-4', // prettier: split lines
2024-12-17 11:38:00 +03:00
'border rounded-sm ',
'cc-animate-color',
{
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
}
)}
2023-12-28 14:04:44 +03:00
>
{value ? (
<div className='mt-[1px] ml-[1px]'>
<CheckboxChecked />
2023-12-28 14:04:44 +03:00
</div>
) : null}
</div>
{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
}
2023-12-28 14:04:44 +03:00
export default Checkbox;