2022-08-10 04:12:20 +00:00
|
|
|
import clsx from 'clsx';
|
|
|
|
import { PropsWithChildren } from '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
|
|
|
|
|
|
|
import './BoxSelectorItem.css';
|
|
|
|
|
|
|
|
import { BoxSelectorOption } from './types';
|
|
|
|
|
|
|
|
interface Props<T extends number | string> {
|
|
|
|
radioName: string;
|
|
|
|
option: BoxSelectorOption<T>;
|
|
|
|
onChange?(value: T): void;
|
|
|
|
selectedValue: T;
|
|
|
|
disabled?: boolean;
|
|
|
|
tooltip?: string;
|
|
|
|
className?: string;
|
|
|
|
type?: 'radio' | 'checkbox';
|
|
|
|
}
|
|
|
|
|
|
|
|
export function BoxOption<T extends number | string>({
|
|
|
|
radioName,
|
|
|
|
option,
|
|
|
|
onChange = () => {},
|
|
|
|
selectedValue,
|
|
|
|
disabled,
|
|
|
|
tooltip,
|
|
|
|
className,
|
|
|
|
type = 'radio',
|
|
|
|
children,
|
|
|
|
}: PropsWithChildren<Props<T>>) {
|
2023-01-26 03:03:44 +00:00
|
|
|
const BoxOption = (
|
2022-12-04 20:47:43 +00:00
|
|
|
<div className={clsx('box-selector-item', className)}>
|
2022-08-10 04:12:20 +00:00
|
|
|
<input
|
|
|
|
type={type}
|
|
|
|
name={radioName}
|
|
|
|
id={option.id}
|
|
|
|
checked={option.value === selectedValue}
|
|
|
|
value={option.value}
|
|
|
|
disabled={disabled}
|
|
|
|
onChange={() => onChange(option.value)}
|
|
|
|
/>
|
|
|
|
|
|
|
|
<label htmlFor={option.id} data-cy={`${radioName}_${option.value}`}>
|
|
|
|
{children}
|
|
|
|
</label>
|
|
|
|
</div>
|
|
|
|
);
|
2023-01-26 03:03:44 +00:00
|
|
|
|
|
|
|
if (tooltip) {
|
|
|
|
return (
|
|
|
|
<TooltipWithChildren message={tooltip}>{BoxOption}</TooltipWithChildren>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return BoxOption;
|
2022-08-10 04:12:20 +00:00
|
|
|
}
|