import styles from './BoxSelector.module.css'; import { BoxSelectorItem } from './BoxSelectorItem'; import { BoxSelectorOption, Value } from './types'; interface IsMultiProps { isMulti: true; value: T[]; onChange(value: T[], limitedToBE: boolean): void; } interface SingleProps { isMulti?: never; value: T; onChange(value: T, limitedToBE: boolean): void; } type Union = IsMultiProps | SingleProps; export type Props = Union & { radioName: string; options: ReadonlyArray> | Array>; slim?: boolean; hiddenSpacingCount?: number; }; export function BoxSelector({ radioName, options, slim = false, hiddenSpacingCount, ...props }: Props) { return (
{options .filter((option) => !option.hide) .map((option) => ( ))} {hiddenSpacingCount && Array.from(Array(hiddenSpacingCount)).map((_, index) => (
))}
); function handleSelect(optionValue: T, limitedToBE: boolean) { if (props.isMulti) { const newValue = isSelected(optionValue) ? props.value.filter((v) => v !== optionValue) : [...props.value, optionValue]; props.onChange(newValue, limitedToBE); return; } if (isSelected(optionValue)) { return; } props.onChange(optionValue, limitedToBE); } function isSelected(optionValue: T) { if (props.isMulti) { return props.value.includes(optionValue); } return props.value === optionValue; } }