import * as React from 'react'; import { StyleSheet, View } from 'react-native'; import color from 'color'; import { RadioButtonContext, RadioButtonContextType } from './RadioButtonGroup'; import { handlePress, isChecked } from './utils'; import MaterialCommunityIcon from '../MaterialCommunityIcon'; import TouchableRipple from '../TouchableRipple/TouchableRipple'; import { withTheme } from '../../core/theming'; import type { $RemoveChildren } from '../../types'; type Props = $RemoveChildren & { /** * Value of the radio button */ value: string; /** * Status of radio button. */ status?: 'checked' | 'unchecked'; /** * Whether radio is disabled. */ disabled?: boolean; /** * Function to execute on press. */ onPress?: () => void; /** * Custom color for radio. */ color?: string; /** * @optional */ theme: ReactNativePaper.Theme; /** * testID to be used on tests. */ testID?: string; }; /** * Radio buttons allow the selection a single option from a set. * This component follows platform guidelines for iOS, but can be used * on any platform. * *
*
* *
Enabled
*
*
* *
Disabled
*
*
*/ const RadioButtonIOS = ({ disabled, onPress, theme, status, value, testID, ...rest }: Props) => { const checkedColor = disabled ? theme.colors.disabled : rest.color || theme.colors.accent; let rippleColor: string; if (disabled) { rippleColor = color(theme.colors.text).alpha(0.16).rgb().string(); } else { rippleColor = color(checkedColor).fade(0.32).rgb().string(); } return ( {(context?: RadioButtonContextType) => { const checked = isChecked({ contextValue: context?.value, status, value, }) === 'checked'; return ( { handlePress({ onPress, value, onValueChange: context?.onValueChange, }); } } // @ts-expect-error We keep old a11y props for backwards compat with old RN versions accessibilityTraits={disabled ? ['button', 'disabled'] : 'button'} accessibilityComponentType={ checked ? 'radiobutton_checked' : 'radiobutton_unchecked' } accessibilityRole="radio" accessibilityState={{ disabled, checked }} accessibilityLiveRegion="polite" style={styles.container} testID={testID} > ); }} ); }; RadioButtonIOS.displayName = 'RadioButton.IOS'; const styles = StyleSheet.create({ container: { borderRadius: 18, padding: 6, }, }); export default withTheme(RadioButtonIOS); // @component-docs ignore-next-line const RadioButtonIOSWithTheme = withTheme(RadioButtonIOS); // @component-docs ignore-next-line export { RadioButtonIOSWithTheme as RadioButtonIOS };