2022-08-10 04:12:20 +00:00
|
|
|
import clsx from 'clsx';
|
|
|
|
import { PropsWithChildren } from 'react';
|
2023-02-07 03:33:57 +00:00
|
|
|
import type { Icon } from 'lucide-react';
|
2022-12-04 20:47:43 +00:00
|
|
|
|
2023-01-26 03:03:44 +00:00
|
|
|
import { TooltipWithChildren } from '@@/Tip/TooltipWithChildren';
|
2022-08-10 04:12:20 +00:00
|
|
|
|
2023-02-07 03:33:57 +00:00
|
|
|
import styles from './BoxOption.module.css';
|
|
|
|
import { BoxSelectorOption, Value } from './types';
|
2022-08-10 04:12:20 +00:00
|
|
|
|
2023-02-07 03:33:57 +00:00
|
|
|
interface Props<T extends Value> {
|
2022-08-10 04:12:20 +00:00
|
|
|
radioName: string;
|
|
|
|
option: BoxSelectorOption<T>;
|
2023-02-07 03:33:57 +00:00
|
|
|
onSelect?(value: T): void;
|
|
|
|
isSelected(value: T): boolean;
|
2022-08-10 04:12:20 +00:00
|
|
|
disabled?: boolean;
|
|
|
|
tooltip?: string;
|
|
|
|
className?: string;
|
|
|
|
type?: 'radio' | 'checkbox';
|
2023-02-07 03:33:57 +00:00
|
|
|
checkIcon: Icon;
|
2022-08-10 04:12:20 +00:00
|
|
|
}
|
|
|
|
|
2023-02-07 03:33:57 +00:00
|
|
|
export function BoxOption<T extends Value>({
|
2022-08-10 04:12:20 +00:00
|
|
|
radioName,
|
|
|
|
option,
|
2023-02-07 03:33:57 +00:00
|
|
|
onSelect = () => {},
|
|
|
|
isSelected,
|
2022-08-10 04:12:20 +00:00
|
|
|
disabled,
|
|
|
|
tooltip,
|
|
|
|
className,
|
|
|
|
type = 'radio',
|
|
|
|
children,
|
2023-02-07 03:33:57 +00:00
|
|
|
checkIcon: Check,
|
2022-08-10 04:12:20 +00:00
|
|
|
}: PropsWithChildren<Props<T>>) {
|
2023-02-07 03:33:57 +00:00
|
|
|
const selected = isSelected(option.value);
|
|
|
|
|
|
|
|
const item = (
|
|
|
|
<div className={clsx(styles.root, className)}>
|
2022-08-10 04:12:20 +00:00
|
|
|
<input
|
|
|
|
type={type}
|
|
|
|
name={radioName}
|
|
|
|
id={option.id}
|
2023-02-07 03:33:57 +00:00
|
|
|
checked={selected}
|
|
|
|
value={option.value.toString()}
|
2022-08-10 04:12:20 +00:00
|
|
|
disabled={disabled}
|
2023-02-07 03:33:57 +00:00
|
|
|
onChange={() => onSelect(option.value)}
|
2022-08-10 04:12:20 +00:00
|
|
|
/>
|
|
|
|
|
|
|
|
<label htmlFor={option.id} data-cy={`${radioName}_${option.value}`}>
|
|
|
|
{children}
|
2023-02-07 03:33:57 +00:00
|
|
|
|
|
|
|
{!disabled && (
|
|
|
|
<div
|
|
|
|
className={clsx(
|
2023-02-12 21:04:24 +00:00
|
|
|
'absolute top-4 right-4 flex h-4 w-4 items-center justify-center rounded-full border border-solid border-blue-8 font-bold text-white',
|
2023-02-07 03:33:57 +00:00
|
|
|
{
|
|
|
|
'bg-white': !selected,
|
|
|
|
'bg-blue-8': selected,
|
|
|
|
}
|
|
|
|
)}
|
|
|
|
>
|
|
|
|
{selected && <Check className="lucide" strokeWidth={3} />}
|
|
|
|
</div>
|
|
|
|
)}
|
2022-08-10 04:12:20 +00:00
|
|
|
</label>
|
|
|
|
</div>
|
|
|
|
);
|
2023-01-26 03:03:44 +00:00
|
|
|
|
|
|
|
if (tooltip) {
|
2023-02-07 03:33:57 +00:00
|
|
|
return <TooltipWithChildren message={tooltip}>{item}</TooltipWithChildren>;
|
2023-01-26 03:03:44 +00:00
|
|
|
}
|
2023-02-07 03:33:57 +00:00
|
|
|
|
|
|
|
return item;
|
2022-08-10 04:12:20 +00:00
|
|
|
}
|