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

87 lines
1.9 KiB
TypeScript
Raw Normal View History

import clsx from 'clsx';
2023-09-07 16:30:43 +03:00
import { useMemo } from 'react';
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?: string;
disabled?: boolean;
2023-07-15 17:46:19 +03:00
2023-12-28 14:04:44 +03:00
value: boolean;
setValue?: (newValue: boolean) => void;
2023-09-07 16:30:43 +03:00
}
2024-03-09 16:40:10 +03:00
function Checkbox({
disabled,
label,
title,
titleHtml,
hideTitle,
className,
value,
setValue,
...restProps
}: CheckboxProps) {
2023-12-28 14:04:44 +03:00
const cursor = useMemo(() => {
2023-09-07 16:30:43 +03:00
if (disabled) {
2024-06-18 15:19:19 +03:00
return 'cursor-auto';
2023-09-07 16:30:43 +03:00
} else if (setValue) {
return 'cursor-pointer';
} else {
return '';
2023-09-07 16:30:43 +03:00
}
}, [disabled, setValue]);
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
'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]'>
<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;