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

69 lines
1.6 KiB
TypeScript
Raw Normal View History

2023-09-07 16:30:43 +03:00
import { useMemo } from 'react';
2023-09-10 20:17:18 +03:00
import { CheckboxCheckedIcon } from '../Icons';
2023-07-15 17:46:19 +03:00
import Label from './Label';
export interface CheckboxProps
extends Omit<React.ButtonHTMLAttributes<HTMLButtonElement>, 'className' | 'children' | 'title' | 'value' | 'onClick' > {
2023-07-20 17:11:03 +03:00
label?: string
2023-07-15 17:46:19 +03:00
disabled?: boolean
dimensions?: string
tooltip?: string
2023-07-15 17:46:19 +03:00
2023-09-07 16:30:43 +03:00
value: boolean
setValue?: (newValue: boolean) => void
}
function Checkbox({
2023-10-14 23:46:36 +03:00
id, disabled, tooltip, label,
dimensions = 'w-fit', value, setValue, ...restProps
}: CheckboxProps) {
2023-09-07 16:30:43 +03:00
const cursor = useMemo(
() => {
if (disabled) {
return 'cursor-not-allowed';
} else if (setValue) {
return 'cursor-pointer';
} else {
return ''
}
}, [disabled, setValue]);
const bgColor = useMemo(
() => {
return value !== false ? 'clr-primary' : 'clr-app'
}, [value]);
function handleClick(event: React.MouseEvent<HTMLButtonElement, MouseEvent>): void {
event.preventDefault();
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 (
<button type='button' id={id}
2023-12-07 23:08:49 +03:00
className={`flex items-center outline-none ${dimensions}`}
title={tooltip}
disabled={disabled}
onClick={handleClick}
{...restProps}
>
<div className={`max-w-[1rem] min-w-[1rem] h-4 mt-0.5 border rounded-sm ${bgColor} ${cursor}`} >
{value ?
<div className='mt-[1px] ml-[1px]'>
<CheckboxCheckedIcon />
</div> : null}
</div>
{label ?
<Label
2023-09-07 16:30:43 +03:00
className={`${cursor} px-2 text-start`}
2023-07-15 17:46:19 +03:00
text={label}
htmlFor={id}
/> : null}
</button>
2023-07-15 17:46:19 +03:00
);
}
2023-07-25 20:27:29 +03:00
export default Checkbox;