UNPKG

@uiw/react-native

Version:
113 lines (112 loc) 3.16 kB
import React, { useState } from 'react'; import { View, StyleSheet, TouchableOpacity, TextInput, Text } from 'react-native'; import Icon from '../Icon'; import Loader from '../Loader'; import { colors } from '../utils'; function SearchInputBar(props) { const inputRef = React.createRef(); const [state, setState] = useState({ showIcon: false }); const needFocus = type => { if (type === 'close') { props.onClear?.(); } else if (type === 'search') { props.onSearch?.(); return; } if (type === 'actived') { setState({ showIcon: true }); } inputRef.current && inputRef.current.focus(); }; // 右侧搜索 const renderSearch = () => { const { showActionButton, searchRender, touchProps, actionName = '搜索' } = props; const { showIcon } = state; if (showActionButton || showIcon) { return searchRender ? searchRender : <TouchableOpacity {...touchProps} style={[styles.search, { paddingHorizontal: 10 }]}> <Text>{actionName}</Text> </TouchableOpacity>; } return null; }; const { value, onChangeText, showActionButton, inputStyle, containerStyle, searchIcon, closeIcon, loading = false, ...other } = props; return <Loader loading={loading} rounded={5} maskColor="transparent"> <View style={[styles.centerFlex]}> <View style={StyleSheet.flatten([styles.searchContainer, styles.centerFlex, containerStyle])}> <TouchableOpacity onPress={() => needFocus('search')}> <Icon name="search" size={14} color={colors.colorsPalette.grey40} height={'100%'} {...searchIcon} /> </TouchableOpacity> <TextInput {...other} value={value} onChangeText={onChangeText} ref={inputRef} style={[styles.textInput, inputStyle]} onFocus={e => { if (showActionButton !== null) { setState({ showIcon: true }); } other?.onFocus?.(e); }} onBlur={e => { if (showActionButton !== null && !value && !loading) { setState({ showIcon: false }); } other?.onBlur?.(e); }} /> {Boolean(value) && <TouchableOpacity style={{}} onPress={() => needFocus('close')}> <Icon name="close" size={14} color={colors.colorsPalette.grey40} height={'100%'} {...closeIcon} /> </TouchableOpacity>} </View> {renderSearch()} </View> </Loader>; } export default React.forwardRef(SearchInputBar); const styles = StyleSheet.create({ centerFlex: { flexDirection: 'row', justifyContent: 'center', alignContent: 'center' }, searchContainer: { height: 40, borderRadius: 5, flex: 1, paddingHorizontal: 15, backgroundColor: colors.colorsPalette.grey70 }, textInput: { paddingVertical: 0, height: '100%', flex: 1, fontSize: 14, fontWeight: '400', paddingLeft: 10, color: colors.colorsPalette.grey40 }, search: { justifyContent: 'center', borderWidth: 0 } });