import * as React from 'react'; import { StyleSheet, Text, TextStyle, TouchableOpacity, View, ViewStyle, } from 'react-native'; import MDIcon from '../icon'; import { fieldItem } from '../../_styles/themes/default.components'; import base from '../../_styles/themes/default.basic'; export interface IMDFieldItemProps { styles?: IMDFieldItemStyle; title?: string; left?: React.ReactNode; placeholder?: string; content?: string; addon?: string; arrow?: boolean; solid?: boolean; alignRight?: boolean; disabled?: boolean; onPress?: (params: any) => void; } export interface IMDFieldItemStyle { wrapper?: ViewStyle; itemContent?: ViewStyle; itemLeft?: ViewStyle; itemRight?: ViewStyle; itemControl?: ViewStyle; itemTitle?: TextStyle; itemText?: TextStyle; addonText?: TextStyle; itemPlaceholder?: TextStyle; solidTitle?: TextStyle; childrenText?: TextStyle; disableText?: TextStyle; rightText?: TextStyle; } export const MDFieldItemStyles: IMDFieldItemStyle = { wrapper: { position: 'relative', }, itemContent: { position: 'relative', flexDirection: 'row', alignItems: 'center', justifyContent: 'space-between', minHeight: fieldItem.minHeight, // paddingTop: fieldItem.paddingVertical, // paddingBottom: fieldItem.paddingVertical, width: '100%', borderBottomWidth: 1, borderBottomColor: fieldItem.borderColor, }, itemTitle: { flexShrink: 0, marginRight: fieldItem.titleGap, fontSize: fieldItem.fontSize, lineHeight: fieldItem.fontSize, }, solidTitle: { width: fieldItem.titleWidth, }, itemLeft: { flexShrink: 0, marginRight: base.gapSize.hSmall, alignItems: 'center', justifyContent: 'flex-start', }, itemControl: { position: 'relative', flex: 1, }, itemText: { color: fieldItem.color, fontSize: fieldItem.fontSize, fontWeight: fieldItem.fontWeight as TextStyle['fontWeight'], lineHeight: fieldItem.fontSize, }, itemPlaceholder: { color: fieldItem.placeholderColor, fontWeight: base.fontWeight.normal as TextStyle['fontWeight'], }, itemRight: { position: 'relative', flexShrink: 0, marginLeft: base.gapSize.hSmall, alignItems: 'center', justifyContent: 'flex-end', }, addonText: { color: fieldItem.addonColor, fontSize: fieldItem.addonFontSize, }, childrenText: { fontSize: fieldItem.childrenFontSize, }, disableText: { color: base.colors.textDisabled, }, rightText: { textAlign: 'right', }, }; const styles = StyleSheet.create(MDFieldItemStyles); export default class MDFieldItem extends React.Component { public static defaultProps = { styles, }; public render () { const sty = this.props.styles || {}; const { title, left, placeholder, content, addon, arrow, solid, alignRight, disabled, onPress, children, } = this.props; const _control = this.renderControl( sty, content, placeholder, disabled, alignRight ); const _right = this.renderRight(sty, arrow, addon); const _content = ( {title} {left ? {left} : null} {_control} {_right} ); if (!disabled && onPress) { return ( {_content} {children} ); } else { return ( {_content} {children} ); } } private renderControl ( sty: IMDFieldItemStyle, content?: string, placeholder?: string, disabled?: boolean, alignRight?: boolean ) { return ( {content || placeholder} ); } private renderRight (sty: IMDFieldItemStyle, arrow?: boolean, addon?: string) { const _icon = !!arrow ? ( ) : null; return ( {addon ? {addon} : null} {_icon} ); } }