|
| 1 | +import React, { useState, useEffect } from 'react'; |
| 2 | +import { |
| 3 | + View, |
| 4 | + Text, |
| 5 | + TextInput, |
| 6 | + SafeAreaView, |
| 7 | + StyleSheet, |
| 8 | + TouchableWithoutFeedback, |
| 9 | + ActivityIndicator, |
| 10 | + Pressable, |
| 11 | +} from 'react-native'; |
| 12 | +import MaskLayer from '../MaskLayer'; |
| 13 | +import List from '../List' |
| 14 | +import { down } from './svg'; |
| 15 | +import Icon from '../Icon'; |
| 16 | + |
| 17 | +interface SearchBarProps { |
| 18 | + onChangeText?: (value: string) => void; |
| 19 | + options?: Array<OptionsState>; |
| 20 | + onChange?: (value: string) => void; |
| 21 | + onFocus?: (e: any | string) => void; |
| 22 | + labelInValue?: Boolean; |
| 23 | + disabled?: Boolean; |
| 24 | + value?: String; |
| 25 | + loading?: Boolean; |
| 26 | + placeholder?: String; |
| 27 | + extra?: JSX.Element; |
| 28 | +} |
| 29 | + |
| 30 | +interface OptionsState { |
| 31 | + label: string; |
| 32 | + value: string | number; |
| 33 | +} |
| 34 | + |
| 35 | +// 搜索组件 |
| 36 | +export default function SearchBar({ |
| 37 | + onChangeText, |
| 38 | + options = [], |
| 39 | + onChange, |
| 40 | + labelInValue, |
| 41 | + disabled, |
| 42 | + value, |
| 43 | + onFocus, |
| 44 | + loading, |
| 45 | + placeholder, |
| 46 | + extra, |
| 47 | +}: SearchBarProps) { |
| 48 | + const onHandleChangeText = (val: string) => { |
| 49 | + onChangeText && onChangeText(val); |
| 50 | + } |
| 51 | + const [curValue, setCurValue] = useState<any>(value); |
| 52 | + const [visible, setVisivble] = useState(false); |
| 53 | + useEffect(() => { |
| 54 | + setCurValue(value); |
| 55 | + }, [value]); |
| 56 | + |
| 57 | + useEffect(() => { |
| 58 | + visible && onFocus; |
| 59 | + }, [visible]); |
| 60 | + |
| 61 | + const showSearchBar = () => { |
| 62 | + if (disabled) { |
| 63 | + return; |
| 64 | + } |
| 65 | + setVisivble(true); |
| 66 | + }; |
| 67 | + const textValue = labelInValue ? curValue && curValue.label : curValue; |
| 68 | + return !visible ? ( |
| 69 | + <Pressable onPress={showSearchBar}> |
| 70 | + <View style={[styles.content]}> |
| 71 | + <Text style={styles.contentTitle}> |
| 72 | + {textValue ? textValue : placeholder} |
| 73 | + </Text> |
| 74 | + {React.isValidElement(extra) ? extra : <Icon xml={down} size={18} />} |
| 75 | + </View> |
| 76 | + </Pressable> |
| 77 | + ) : ( |
| 78 | + <MaskLayer visible={visible}> |
| 79 | + <SafeAreaView style={styles.container}> |
| 80 | + <View style={styles.inputBox}> |
| 81 | + <View style={styles.leftBox}> |
| 82 | + <View style={styles.icon}> |
| 83 | + <Icon name="search" color="#ccc" size={20} /> |
| 84 | + </View> |
| 85 | + <TextInput |
| 86 | + placeholderTextColor="#000" |
| 87 | + onFocus={onFocus && onFocus} |
| 88 | + style={styles.input} |
| 89 | + placeholder="输入搜索..." |
| 90 | + onChangeText={onHandleChangeText} |
| 91 | + /> |
| 92 | + </View> |
| 93 | + |
| 94 | + <TouchableWithoutFeedback |
| 95 | + onPress={() => { |
| 96 | + setVisivble(false); |
| 97 | + }}> |
| 98 | + <View style={styles.cancel}> |
| 99 | + <Text>取消</Text> |
| 100 | + </View> |
| 101 | + </TouchableWithoutFeedback> |
| 102 | + </View> |
| 103 | + {loading ? ( |
| 104 | + <ActivityIndicator |
| 105 | + color="#0A0258" |
| 106 | + size="large" |
| 107 | + style={styles.loading} |
| 108 | + /> |
| 109 | + ) : ( |
| 110 | + <List style={styles.list}> |
| 111 | + {options.map(itm => ( |
| 112 | + <List.Item |
| 113 | + key={itm.value} |
| 114 | + onPress={() => { |
| 115 | + const selectValue: any | { |
| 116 | + key: string; |
| 117 | + label: string; |
| 118 | + } = labelInValue |
| 119 | + ? { key: itm.value, label: itm.label } |
| 120 | + : itm.value; |
| 121 | + onChange && onChange(selectValue); |
| 122 | + setCurValue(selectValue); |
| 123 | + setVisivble(false); |
| 124 | + }}> |
| 125 | + <Text style={{ fontSize: 16 }}>{itm.label}</Text> |
| 126 | + </List.Item> |
| 127 | + ))} |
| 128 | + </List> |
| 129 | + )} |
| 130 | + </SafeAreaView> |
| 131 | + </MaskLayer> |
| 132 | + ); |
| 133 | +} |
| 134 | + |
| 135 | +const styles = StyleSheet.create({ |
| 136 | + container: { |
| 137 | + flex: 1, |
| 138 | + backgroundColor: '#F5F5F5', |
| 139 | + }, |
| 140 | + contentTitle: { |
| 141 | + fontSize: 16, |
| 142 | + color: 'black', |
| 143 | + }, |
| 144 | + inputBox: { |
| 145 | + paddingLeft: 10, |
| 146 | + paddingRight: 10, |
| 147 | + marginTop: 5, |
| 148 | + flexDirection: 'row', |
| 149 | + alignItems: 'center', |
| 150 | + height: 40, |
| 151 | + }, |
| 152 | + leftBox: { |
| 153 | + flex: 1, |
| 154 | + flexDirection: 'row', |
| 155 | + backgroundColor: '#fff', |
| 156 | + alignItems: 'center', |
| 157 | + paddingTop: 8, |
| 158 | + paddingBottom: 8, |
| 159 | + }, |
| 160 | + icon: { |
| 161 | + backgroundColor: '#fff', |
| 162 | + paddingLeft: 10, |
| 163 | + justifyContent: 'center', |
| 164 | + }, |
| 165 | + input: { |
| 166 | + backgroundColor: '#fff', |
| 167 | + fontSize: 16, |
| 168 | + flex: 1, |
| 169 | + paddingLeft: 10, |
| 170 | + color: '#7C7D7E', |
| 171 | + paddingBottom: 0, |
| 172 | + paddingTop: 0, |
| 173 | + }, |
| 174 | + cancel: { |
| 175 | + color: '#7C7D7E', |
| 176 | + paddingLeft: 10, |
| 177 | + justifyContent: 'center', |
| 178 | + }, |
| 179 | + list: { |
| 180 | + marginLeft: 10, |
| 181 | + marginTop: 10, |
| 182 | + marginRight: 10, |
| 183 | + }, |
| 184 | + loading: { |
| 185 | + position: 'absolute', |
| 186 | + top: '20%', |
| 187 | + left: '45%', |
| 188 | + }, |
| 189 | + content: { |
| 190 | + flexDirection: 'row', |
| 191 | + height: 35, |
| 192 | + alignItems: 'center', |
| 193 | + justifyContent: 'space-between', |
| 194 | + paddingRight: 5, |
| 195 | + backgroundColor:"#fff", |
| 196 | + paddingHorizontal:16 |
| 197 | + }, |
| 198 | + disabled: { |
| 199 | + backgroundColor: '#f5f5f5', |
| 200 | + }, |
| 201 | +}); |
0 commit comments