import * as React from 'react'; import { StyleSheet, Text, TextStyle, TouchableWithoutFeedback, View, ViewStyle, } from 'react-native'; import base from '../../_styles/themes/default.basic'; import { check } from '../../_styles/themes/default.components'; import MDIcon from '../icon'; import { IMDOptionValue } from '../types'; export interface IMDCheckProps { styles?: IMDCheckStyle; label: string; disabled?: boolean; checked?: boolean; value?: IMDOptionValue; icon?: React.ReactNode; iconInverse?: React.ReactNode; iconDisabled?: React.ReactNode; onChange?: (checked: boolean, value?: IMDOptionValue) => void; } interface IMDCheckState { checked: boolean; } interface IMDCheckStyle { wrapper?: ViewStyle; label?: TextStyle; } export const MDCheckStyles: IMDCheckStyle = { wrapper: { justifyContent: 'flex-start', alignItems: 'center', flexDirection: 'row', marginVertical: base.gapSize.vSmall, }, label: { color: base.colors.textBase, marginLeft: base.gapSize.hSmall, fontSize: base.fontSize.bodyLarge, }, }; const styles = StyleSheet.create(MDCheckStyles); export default class MDCheck extends React.Component< IMDCheckProps, IMDCheckState > { public static defaultProps = { styles, checked: false, disabled: false, icon: , iconInverse: , iconDisabled: ( ), __name: 'MDCheck' }; constructor (props: IMDCheckProps) { super(props); this.state = { checked: !!props.checked, }; } public componentWillReceiveProps (nextProps: IMDCheckProps) { const { checked } = nextProps; if (checked !== this.props.checked || checked !== this.state.checked) { this.setState({ checked: !!checked, }); } } public render () { const { label, disabled, icon, iconInverse, iconDisabled } = this.props; const _styles = this.props.styles || {}; let _icon = disabled ? iconDisabled : this.state.checked ? icon : iconInverse; if (typeof _icon === 'string') { _icon = ( ); } const _lableStyle = [_styles.label]; if (disabled) { _lableStyle.push({ color: base.colors.textDisabled, }); } return ( {_icon} {label} ); } private onPress () { const { disabled, onChange, value } = this.props; if (disabled) { return; } const { checked } = this.state; this.setState( { checked: !checked, }, () => { onChange && onChange(!checked, value); } ); } }