import { ComponentProps, Fragment, ReactNode } from "react"; import { Divider } from "../layout"; import { ListItemRadio, ListItemRadioWithAmount } from "../listitems"; export type RadioItem = { id: T; value: string; accessibilityLabel?: string; description?: string | ReactNode; disabled?: boolean; startImage?: ComponentProps["startImage"]; loadingProps?: ComponentProps["loadingProps"]; }; export type RadioItemWithAmount = { id: T; label: string; formattedAmountString: string; } & ( | { isSuggested?: false; } | { isSuggested: true; suggestReason: string; } ); type CommonProps = { selectedItem?: T; onPress: (selected: T) => void; }; type RadioListItemProps = { type: "radioListItem"; items: ReadonlyArray>; } & CommonProps; type RadioListItemWithAmountProps = { type: "radioListItemWithAmount"; items: ReadonlyArray>; } & CommonProps; type Props = RadioListItemProps | RadioListItemWithAmountProps; /** * A list of radio buttons. * The management of the selection is demanded and derived by the `selectedItem` prop. * The item with the `id` equal to the `selectedItem` is the active one. */ const RadioListItem = (props: RadioListItemProps) => ( <> {props.items.map((item, index) => ( props.onPress(item.id)} selected={props.selectedItem === item.id} /> {index < props.items.length - 1 && } ))} ); const RadioListItemWithAmount = ( props: RadioListItemWithAmountProps ) => ( <> {props.items.map((item, index) => ( {item.isSuggested ? ( props.onPress(item.id)} selected={props.selectedItem === item.id} /> ) : ( props.onPress(item.id)} selected={props.selectedItem === item.id} /> )} {index < props.items.length - 1 && } ))} ); export const RadioGroup = (props: Props) => ( <> {props.type === "radioListItem" && RadioListItem(props)} {props.type === "radioListItemWithAmount" && RadioListItemWithAmount(props)} );