import React, { Component, ReactElement } from 'react' import { View, Text, StyleSheet, TouchableOpacity, ViewStyle, Animated } from 'react-native' import checkboxItemStyle from './styles' import variables from '../../common/styles/variables' import { FadeAnimated } from '../../common/animations' const styles = StyleSheet.create(checkboxItemStyle) enum ICON_POSITION { LEFT = 'left', RIGHT = 'right' } export interface CheckboxItemProps { style?: ViewStyle label?: string value?: any | null | undefined disabled?: boolean checked?: boolean iconPosition?: 'left' | 'right' onChange?: Function checkedIcon?: ReactElement uncheckedIcon?: ReactElement renderItem?: Function } export class CheckboxItem extends Component { static displayName = 'CheckboxItem' static defaultProps = { style: {}, label: '选项', value: null, disabled: false, checked: false, iconPosition: ICON_POSITION.LEFT, checkedIcon: null } animated: any constructor (props) { super(props) this.state = { } if (variables.checkboxEnableAnimated) { this.animated = new FadeAnimated({}) } } componentDidMount () { this.animated && this.animated.toIn() } componentDidUpdate (prevProps) { if (prevProps.checked !== this.props.checked) { this.animated && this.animated.toIn() } } handlePress = () => { const { disabled, checked, value } = this.props if (this.props.disabled) { return } this.animated && this.animated.toIn() this.props.onChange && this.props.onChange(value, !checked) } renderIcon = () => { const { checked, iconPosition, checkedIcon, uncheckedIcon } = this.props const styleArray: any[] = [] if (iconPosition === ICON_POSITION.LEFT) { styleArray.push(styles.iconLeftPosition) } const iconView = checked ? checkedIcon : uncheckedIcon let animatedStyle: any = {} if (variables.radioEnableAnimated) { animatedStyle = { transform: [{ scale: this.animated.getState().scale }], opacity: this.animated.getState().opacity } } return ( {iconView} ) } renderLabel () { const { label, checked } = this.props return ( {label} ) } render () { const { style, disabled, iconPosition, checked, renderItem } = this.props return ( { typeof renderItem === 'function' ? renderItem(checked) : {this.renderIcon()} {this.renderLabel()} } ) } }