Files
yaak/src-web/components/core/RadioDropdown.tsx
2023-03-20 13:49:21 -07:00

36 lines
855 B
TypeScript

import { memo, useMemo } from 'react';
import type { DropdownProps } from './Dropdown';
import { Dropdown } from './Dropdown';
import { Icon } from './Icon';
export interface RadioDropdownItem {
label: string;
value: string | null;
}
export interface RadioDropdownProps {
value: string | null;
onChange: (value: string | null) => void;
items: RadioDropdownItem[];
children: DropdownProps['children'];
}
export const RadioDropdown = memo(function RadioDropdown({
value,
items,
onChange,
children,
}: RadioDropdownProps) {
const dropdownItems = useMemo(
() =>
items.map(({ label, value: v }) => ({
label,
onSelect: () => onChange(v),
leftSlot: <Icon icon={value === v ? 'check' : 'empty'} />,
})),
[value, items],
);
return <Dropdown items={dropdownItems}>{children}</Dropdown>;
});