import React, { useRef, useEffect, FunctionComponent } from 'react'; import { GestureResponderEvent, Animated, ListRenderItem, FlatList, } from 'react-native'; import * as Styled from './RadioButton.style'; import type { IRadioButtonPickerProps, IOption } from './RadioButton.types'; import { Typography } from '../../ui'; export const RadioButton: FunctionComponent = ({ label, error, value, options, onChange, ...otherViewProps }) => { const scale = useRef(new Animated.Value(0)).current; const opacity = useRef(new Animated.Value(0)).current; const hasError = !!error; const hasLabel = !!label; const handlePress = async ( event: GestureResponderEvent, key: string, onPress?: (event: GestureResponderEvent) => void ) => { const duration = 200; const useNativeDriver = true; const fadeIn = Animated.timing(opacity, { toValue: 0.5, duration, useNativeDriver, }); const fadeOut = Animated.timing(opacity, { toValue: 0, duration, useNativeDriver, }); const scaleUp = Animated.timing(scale, { toValue: 35, duration, useNativeDriver, }); const scaleDown = Animated.timing(scale, { toValue: 0, duration, useNativeDriver, }); Animated.parallel([fadeIn, scaleUp]).start(() => fadeOut.start(() => scaleDown.start()) ); onChange(key); onPress && onPress(event); }; useEffect(() => { onChange(value); }, []); const renderItem: ListRenderItem = ({ item: { key, value: val, onPress, ...otherTouchableProps }, index, }) => { const isSelected = value === key; return ( handlePress(event, key, onPress)} > {isSelected && ( )} {val} ); }; return ( {hasLabel && ( {label} )} item.key} /> {hasError && ( {error} )} ); };