1- import React , { useState , useEffect } from 'react' ;
1+ import React , { useState , useEffect , memo , useCallback } from 'react' ;
22import {
33 View ,
44 Text ,
5- TextInput ,
65 SafeAreaView ,
76 StyleSheet ,
87 TouchableWithoutFeedback ,
98 ActivityIndicator ,
109 Pressable ,
1110} from 'react-native' ;
1211import MaskLayer from '../MaskLayer' ;
12+ import SearchInputBar from '../SearchInputBar' ;
1313import List from '../List' ;
1414import { down } from './svg' ;
1515import Icon from '../Icon' ;
@@ -19,6 +19,7 @@ interface SearchBarProps {
1919 options ?: Array < OptionsState > ;
2020 onChange ?: ( value : string | null ) => void ;
2121 onFocus ?: ( e : any | string ) => void ;
22+ onBlur ?: ( e : any | string ) => void ;
2223 labelInValue ?: Boolean ;
2324 disabled ?: Boolean ;
2425 value ?: String ;
@@ -34,24 +35,24 @@ interface OptionsState {
3435}
3536
3637// 搜索组件
37- export default function SearchBar ( {
38+ function SearchBar ( {
3839 onChangeText,
3940 options = [ ] ,
4041 onChange,
4142 labelInValue,
4243 disabled,
4344 value,
4445 onFocus,
46+ onBlur,
4547 loading,
4648 placeholder,
4749 extra,
4850 showClear = true ,
4951} : SearchBarProps ) {
50- const onHandleChangeText = ( val : string ) => {
51- onChangeText && onChangeText ( val ) ;
52- } ;
5352 const [ curValue , setCurValue ] = useState < any > ( value ) ;
5453 const [ visible , setVisivble ] = useState ( false ) ;
54+ const textValue = labelInValue ? curValue && curValue . label : curValue ;
55+
5556 useEffect ( ( ) => {
5657 setCurValue ( value ) ;
5758 } , [ value ] ) ;
@@ -60,13 +61,19 @@ export default function SearchBar({
6061 visible && onFocus ;
6162 } , [ visible ] ) ;
6263
64+ // 搜索
65+ const onHandleChangeText = ( val : string ) => {
66+ onChangeText && onChangeText ( val ) ;
67+ } ;
68+
69+ // 点击打开遮罩层
6370 const showSearchBar = ( ) => {
6471 if ( disabled ) {
6572 return ;
6673 }
6774 setVisivble ( true ) ;
6875 } ;
69- const textValue = labelInValue ? curValue && curValue . label : curValue ;
76+
7077 return ! visible ? (
7178 < Pressable onPress = { showSearchBar } >
7279 < View style = { [ styles . content ] } >
@@ -91,30 +98,27 @@ export default function SearchBar({
9198 ) : (
9299 < MaskLayer visible = { visible } >
93100 < SafeAreaView style = { styles . container } >
94- < View style = { styles . inputBox } >
95- < View style = { styles . leftBox } >
96- < View style = { styles . icon } >
97- < Icon name = "search" color = "#ccc" size = { 20 } />
98- </ View >
99- < TextInput
100- placeholderTextColor = "#000"
101- onFocus = { onFocus && onFocus }
102- style = { styles . input }
103- placeholder = "输入搜索..."
104- onChangeText = { onHandleChangeText }
105- />
106- </ View >
107-
108- < TouchableWithoutFeedback
109- onPress = { ( ) => {
110- setVisivble ( false ) ;
111- } }
112- >
113- < View style = { styles . cancel } >
114- < Text > 取消</ Text >
115- </ View >
116- </ TouchableWithoutFeedback >
117- </ View >
101+ < SearchInputBar
102+ loading = { loading }
103+ containerStyle = { { backgroundColor : '#fff' , marginHorizontal : 10 } }
104+ autoFocus
105+ showActionButton
106+ placeholder = "输入搜索..."
107+ onChangeText = { onHandleChangeText }
108+ onFocus = { onFocus && onFocus }
109+ onBlur = { onBlur && onBlur }
110+ searchRender = {
111+ < TouchableWithoutFeedback
112+ onPress = { ( ) => {
113+ setVisivble ( false ) ;
114+ } }
115+ >
116+ < View style = { styles . cancel } >
117+ < Text > 取消</ Text >
118+ </ View >
119+ </ TouchableWithoutFeedback >
120+ }
121+ />
118122 { loading ? (
119123 < ActivityIndicator color = "#0A0258" size = "large" style = { styles . loading } />
120124 ) : (
@@ -144,6 +148,8 @@ export default function SearchBar({
144148 ) ;
145149}
146150
151+ export default memo ( SearchBar ) ;
152+
147153const styles = StyleSheet . create ( {
148154 container : {
149155 flex : 1 ,
@@ -153,39 +159,14 @@ const styles = StyleSheet.create({
153159 fontSize : 16 ,
154160 color : 'black' ,
155161 } ,
156- inputBox : {
157- paddingLeft : 10 ,
158- paddingRight : 10 ,
159- marginTop : 5 ,
160- flexDirection : 'row' ,
161- alignItems : 'center' ,
162- height : 40 ,
163- } ,
164- leftBox : {
165- flex : 1 ,
166- flexDirection : 'row' ,
167- backgroundColor : '#fff' ,
168- alignItems : 'center' ,
169- paddingTop : 8 ,
170- paddingBottom : 8 ,
171- } ,
172162 icon : {
173163 backgroundColor : '#fff' ,
174164 paddingLeft : 10 ,
175165 justifyContent : 'center' ,
176166 } ,
177- input : {
178- backgroundColor : '#fff' ,
179- fontSize : 16 ,
180- flex : 1 ,
181- paddingLeft : 10 ,
182- color : '#7C7D7E' ,
183- paddingBottom : 0 ,
184- paddingTop : 0 ,
185- } ,
186167 cancel : {
187168 color : '#7C7D7E' ,
188- paddingLeft : 10 ,
169+ paddingRight : 10 ,
189170 justifyContent : 'center' ,
190171 } ,
191172 list : {
0 commit comments