/* * @Author: Rogan * @format * * @Description: 搜索栏 * @Date: 2019-05-09 19:24:07 * @LastEditTime: 2019-05-10 18:54:24 */ import * as React from 'react'; import { View, Text, Image, TextInput, StyleSheet, TouchableOpacity } from 'react-native'; import theme from '../../theme'; import baseI18n from '../../utils/i18n' // interface Props { // // 支持TextInput所有属性 // // 编辑框是否支持编辑 // inputStyle?: React.ElementType, // onSearch?: () => void, // 当前 editable 为false是生效 // // 右边组件 // right?: string | Function | React.ElementType | React.Node, // rightStyle?: React.ElementType, // rightTextStyle?: React.ElementType, // rightShowMode: 'never' | 'editing' | 'always', // 显示方式:不显示(默认)、编辑时显示、一直显示 // onRight?: () => void, // } // interface State { // focus: boolean, // 是否正在编辑 // } export default class FCSearchBar extends React.PureComponent { static defaultProps = { placeholderTextColor: '#B6B6BF', placeholder: '', returnKeyType: 'search', right: '', numberOfLines: 1, blurOnSubmit: true, clearButtonMode: 'while-editing', underlineColorAndroid: 'transparent', editable: true, rightShowMode: 'never' }; constructor(props) { super(props); this._onSearch = this._onSearch.bind(this); this._onFocus = this._onFocus.bind(this); this._onBlur = this._onBlur.bind(this); this.renderRight = this.renderRight.bind(this); } state = { focus: false }; render() { const { editable, placeholder, inputStyle, style, ...other } = this.props; return ( { editable ? ( ) : ( {this.props.value || this.props.defaultValue || placeholder} ) } {this.renderRight()} ); } renderRight() { const { right, onRight, rightStyle, rightTextStyle, rightShowMode } = this.props; const { focus } = this.state; let rightComponent = null; if (rightShowMode === 'never') { rightComponent = null; } else if (rightShowMode === 'editing' && !focus) { rightComponent = null; } else if (right === null || right === undefined) { rightComponent = null; } else if (typeof right === 'string') { rightComponent = ( {right ? right : baseI18n.t('取消')} ); } else if (typeof right === 'function') { rightComponent = right(); } else if (typeof right === 'number') { rightComponent = ( ); } else if (typeof right === 'object' && Object.prototype.hasOwnProperty.call(right, 'uri')) { rightComponent = ( ); } else { rightComponent = right; } return rightComponent; } // 点击编辑栏 _onSearch(event) { const { editable, onSearch } = this.props; event.preventDefault(); event.stopPropagation(); !editable && onSearch && onSearch(); } // 获得焦点 _onFocus() { this.setState({ focus: true }); this.props.onFocus && this.props.onFocus(); } // 失去焦点 _onBlur() { this.setState({ focus: false }); this.props.onBlur && this.props.onBlur(); } } const styles = StyleSheet.create({ container: { // flex: 1, height: 60, paddingVertical: 12, paddingHorizontal: 15, // borderTopWidth: 1, // borderBottomWidth: 1, // borderColor: theme.cuttingLineColor, backgroundColor: '#fff', flexDirection: 'row', alignItems: 'center' }, contentContainer: { flex: 1, height: '100%', flexDirection: 'row', alignItems: 'center', backgroundColor: '#F0F0F7', borderRadius: 6 }, editable: { height: 70 }, searchImg: { width: 15, height: 15, resizeMode: 'contain', marginHorizontal: 10 }, content: { flex: 1, height: '100%', textAlign: 'left', color: '#333333' }, placeholder: { color: '#B6B6BF', fontSize: 12 }, rightContainer: { height: '100%', alignItems: 'center', justifyContent: 'center', paddingHorizontal: 10 }, right: { fontSize: 15, color: theme.mainColor }, rightImg: { width: 22, height: 22, resizeMode: 'contain' } });