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

87 lines
2.0 KiB
TypeScript
Raw Normal View History

2024-06-07 20:17:03 +03:00
import clsx from 'clsx';
import { globals } from '@/utils/constants';
import { CheckboxChecked } from '../Icons';
import { CProps } from '../props';
export interface CheckboxProps extends Omit<CProps.Button, 'value' | 'onClick'> {
/** Label to display next to the checkbox. */
2024-06-07 20:17:03 +03:00
label?: string;
/** Indicates whether the checkbox is disabled. */
2024-06-07 20:17:03 +03:00
disabled?: boolean;
/** Current value - `true` or `false`. */
2024-06-07 20:17:03 +03:00
value: boolean;
/** Callback to set the `value`. */
2024-06-07 20:17:03 +03:00
setValue?: (newValue: boolean) => void;
}
/**
2024-10-30 21:35:43 +03:00
* Component that allows toggling a boolean value.
*/
2024-06-07 20:17:03 +03:00
function Checkbox({
disabled,
label,
title,
titleHtml,
hideTitle,
className,
value,
setValue,
...restProps
}: CheckboxProps) {
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;
}
setValue(!value);
}
return (
<button
type='button'
className={clsx(
'flex items-center gap-2', // prettier: split lines
'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(
'max-w-[1rem] min-w-[1rem] h-4', // prettier: split lines
'border rounded-sm',
{
'clr-primary': value !== false,
'clr-app': value === false
}
)}
>
{value ? (
<div className='mt-[1px] ml-[1px]'>
<CheckboxChecked />
</div>
) : null}
</div>
{label ? <span className={clsx('text-start text-sm whitespace-nowrap select-text', cursor)}>{label}</span> : null}
</button>
);
}
export default Checkbox;