import React, { Component, ReactElement } from 'react' import { View, Text, TouchableOpacity, Animated, ViewStyle } from 'react-native' import styles from './styles' import variables from '../../common/styles/variables' import styleUtils from '../../common/styles/utils' import { FadeAnimated } from '../../common/animations' interface RadioItemProps { testID?: string style?: ViewStyle label?: string value: any disabled?: boolean checked?: boolean iconPosition?: 'left' | 'right' onChange: Function checkedIcon?: ReactElement uncheckedIcon?: ReactElement renderItem?: Function } export default class RadioItem extends Component { static displayName = 'RadioItem' static defaultProps = { label: '选项', value: null, disabled: false, checked: false, iconPosition: 'right' } private animated: any constructor (props) { super(props) if (variables.radioEnableAnimated) { this.animated = new FadeAnimated({}) } } componentDidMount () { this.animated && this.animated.toIn() } componentDidUpdate (prevProps) { if (prevProps.checked !== this.props.checked) { this.animated && this.animated.toIn() } } handlePress = () => { if (this.props.disabled) { return } const value = this.props.value let checked = this.props.checked // 已经选中了就直接返回 if (checked === true) { return } this.animated && this.animated.toIn() this.props.onChange && this.props.onChange(value) } renderIcon = (checked, iconPosition) => { const iconContainerStyle = { marginRight: iconPosition === 'left' ? 6 : null } const iconView = checked ? this.props.checkedIcon : this.props.uncheckedIcon let animatedStyle: any = {} if (variables.radioEnableAnimated) { animatedStyle = { transform: [{ scale: this.animated.getState().scale }], opacity: this.animated.getState().opacity } } return ( {iconView} ) } renderLabel = (checked) => { return ( {this.props.label} ) } render () { const { testID, disabled, checked, iconPosition, style, renderItem } = this.props return ( { typeof renderItem === 'function' ? renderItem(checked) : {this.renderIcon(checked, iconPosition)} {this.renderLabel(checked)} } ) } }