import * as React from 'react'; import { Radio } from '@lightspeed/flame/Radio'; import { useFilteringActions } from './FilterContext'; import { DropdownDivider, DropdownContent } from './FilterDropdown'; type RadioType = { label: React.ReactNode; value: string; }; interface Props { radios: RadioType[]; filterKey: any; children: (props: { applyFilter: (selectedValues: string | ((selectedValues: string) => string)) => void; undoChanges: () => void; }) => React.ReactNode; } const FilterRadioGroup: React.FC = ({ filterKey, radios, children }) => { const { initialValues, applyFilter, deleteFilter } = useFilteringActions(filterKey); const [selectedValues, setSelectedValues] = React.useState(initialValues || []); function onChange(event: React.ChangeEvent) { const { value } = event.target; setSelectedValues([value]); } function nextApplyFilter(cb: ((values: string) => string) | string) { const nextTagLabel = (typeof cb === 'function' && cb(selectedValues)) || cb; if (selectedValues && selectedValues.length) { applyFilter(nextTagLabel as string, selectedValues); } else { deleteFilter(); } } function undoChanges() { setSelectedValues([]); } return ( {radios.map(({ label, value }) => ( ))} {children({ applyFilter: nextApplyFilter, undoChanges })} ); }; export { FilterRadioGroup };