import type { ReactElement } from 'react'; import React from 'react'; import type { StyleProp, ViewStyle } from 'react-native'; import { View } from 'react-native'; import Radio from './Radio'; import { Spacer } from './StyledRadio'; import type { OptionType } from './types'; export interface RadioGroupProps { /** * An array of radio options to be selected. */ options: OptionType[]; /** * Radio input value. */ value: T; /** * Press event handler receiving selected radio's value. */ onPress: (value: T) => void; /** * Used to extract a unique key for a given option at the specified index. Key is used for caching and as the react key to track item re-ordering. * The default extractor checks option.key, and then falls back to using the index, like React does. */ keyExtractor?: (option: OptionType, index?: number) => string; /** * Additional style. */ style?: StyleProp; /** * Testing id of the component. */ testID?: string; /** * Overrides the text that's read by the screen reader when the user interacts with the element. */ accessible?: boolean; /** * Idle background color of the Radio. */ inactiveIntent?: 'light' | 'dark'; } function getKey( option: OptionType, index: number, keyExtractor?: (opt: OptionType, i?: number) => string ) { let key: React.Key = ''; if (keyExtractor !== undefined) { key = keyExtractor(option, index); } else if (option.key !== undefined) { key = option.key; } else { key = index; } return key; } const RadioGroup = ({ value, onPress, options, keyExtractor, style, testID, accessible, inactiveIntent = 'light', }: RadioGroupProps): ReactElement => ( {options.map((option, index) => ( {index !== 0 && } onPress(option.value)} inactiveIntent={inactiveIntent} prefix={option.prefix} > {option.children} ))} ); export default RadioGroup;