1- import React from 'react' ;
1+ import React , { useState } from 'react' ;
22import {
33 View ,
44 StyleSheet ,
@@ -47,33 +47,30 @@ interface SearchInputBarState {
4747 showIcon : boolean ;
4848}
4949
50- export default class SearchInputBar extends React . Component < SearchInputBarProps , SearchInputBarState > {
51- public inputRef = React . createRef < TextInput > ( ) ;
50+ function SearchInputBar ( props : SearchInputBarProps ) {
51+ const inputRef = React . createRef < TextInput > ( ) ;
5252
53- constructor ( props : SearchInputBarProps ) {
54- super ( props ) ;
55- this . state = {
56- showIcon : false ,
57- } ;
58- }
53+ const [ state , setState ] = useState < SearchInputBarState > ( {
54+ showIcon : false ,
55+ } ) ;
5956
60- needFocus = ( type : 'search' | 'close' | 'actived' ) => {
57+ const needFocus = ( type : 'search' | 'close' | 'actived' ) => {
6158 if ( type === 'close' ) {
62- this . props . onClear ?.( ) ;
59+ props . onClear ?.( ) ;
6360 } else if ( type === 'search' ) {
64- this . props . onSearch ?.( ) ;
61+ props . onSearch ?.( ) ;
6562 return ;
6663 }
6764 if ( type === 'actived' ) {
68- this . setState ( { showIcon : true } ) ;
65+ setState ( { showIcon : true } ) ;
6966 }
70- this . inputRef . current && this . inputRef . current . focus ( ) ;
67+ inputRef . current && inputRef . current . focus ( ) ;
7168 } ;
7269
7370 // 右侧搜索
74- renderSearch = ( ) => {
75- const { showActionButton, searchRender, touchProps, actionName = '搜索' } = this . props ;
76- const { showIcon } = this . state ;
71+ const renderSearch = ( ) => {
72+ const { showActionButton, searchRender, touchProps, actionName = '搜索' } = props ;
73+ const { showIcon } = state ;
7774 if ( showActionButton || showIcon ) {
7875 return searchRender ? (
7976 searchRender
@@ -86,58 +83,58 @@ export default class SearchInputBar extends React.Component<SearchInputBarProps,
8683 return null ;
8784 } ;
8885
89- render ( ) {
90- const {
91- value,
92- onChangeText,
93- showActionButton,
94- inputStyle,
95- containerStyle,
96- searchIcon,
97- closeIcon,
98- loading = false ,
99- ...other
100- } = this . props ;
86+ const {
87+ value,
88+ onChangeText,
89+ showActionButton,
90+ inputStyle,
91+ containerStyle,
92+ searchIcon,
93+ closeIcon,
94+ loading = false ,
95+ ...other
96+ } = props ;
10197
102- return (
103- < Loader loading = { loading } rounded = { 5 } maskColor = "transparent" >
104- < View style = { [ styles . centerFlex ] } >
105- < View style = { StyleSheet . flatten ( [ styles . searchContainer , styles . centerFlex , containerStyle ] ) } >
106- < TouchableOpacity onPress = { ( ) => this . needFocus ( 'search' ) } >
107- < Icon name = "search" size = { 14 } color = { colors . colorsPalette . grey40 } height = { '100%' } { ...searchIcon } />
98+ return (
99+ < Loader loading = { loading } rounded = { 5 } maskColor = "transparent" >
100+ < View style = { [ styles . centerFlex ] } >
101+ < View style = { StyleSheet . flatten ( [ styles . searchContainer , styles . centerFlex , containerStyle ] ) } >
102+ < TouchableOpacity onPress = { ( ) => needFocus ( 'search' ) } >
103+ < Icon name = "search" size = { 14 } color = { colors . colorsPalette . grey40 } height = { '100%' } { ...searchIcon } />
104+ </ TouchableOpacity >
105+ < TextInput
106+ { ...other }
107+ value = { value }
108+ onChangeText = { onChangeText }
109+ ref = { inputRef }
110+ style = { [ styles . textInput , inputStyle ] }
111+ onFocus = { ( e : NativeSyntheticEvent < TextInputFocusEventData > ) => {
112+ if ( showActionButton !== null ) {
113+ setState ( { showIcon : true } ) ;
114+ }
115+ other ?. onFocus ?.( e ) ;
116+ } }
117+ onBlur = { ( e : NativeSyntheticEvent < TextInputFocusEventData > ) => {
118+ if ( showActionButton !== null && ! value && ! loading ) {
119+ setState ( { showIcon : false } ) ;
120+ }
121+ other ?. onBlur ?.( e ) ;
122+ } }
123+ />
124+ { Boolean ( value ) && (
125+ < TouchableOpacity style = { { } } onPress = { ( ) => needFocus ( 'close' ) } >
126+ < Icon name = "close" size = { 14 } color = { colors . colorsPalette . grey40 } height = { '100%' } { ...closeIcon } />
108127 </ TouchableOpacity >
109- < TextInput
110- { ...other }
111- value = { value }
112- onChangeText = { onChangeText }
113- ref = { this . inputRef }
114- style = { [ styles . textInput , inputStyle ] }
115- onFocus = { ( e : NativeSyntheticEvent < TextInputFocusEventData > ) => {
116- if ( showActionButton !== null ) {
117- this . setState ( { showIcon : true } ) ;
118- }
119- other ?. onFocus ?.( e ) ;
120- } }
121- onBlur = { ( e : NativeSyntheticEvent < TextInputFocusEventData > ) => {
122- if ( showActionButton !== null && ! value && ! loading ) {
123- this . setState ( { showIcon : false } ) ;
124- }
125- other ?. onBlur ?.( e ) ;
126- } }
127- />
128- { Boolean ( value ) && (
129- < TouchableOpacity style = { { } } onPress = { ( ) => this . needFocus ( 'close' ) } >
130- < Icon name = "close" size = { 14 } color = { colors . colorsPalette . grey40 } height = { '100%' } { ...closeIcon } />
131- </ TouchableOpacity >
132- ) }
133- </ View >
134- { this . renderSearch ( ) }
128+ ) }
135129 </ View >
136- </ Loader >
137- ) ;
138- }
130+ { renderSearch ( ) }
131+ </ View >
132+ </ Loader >
133+ ) ;
139134}
140135
136+ export default React . forwardRef ( SearchInputBar ) ;
137+
141138const styles = StyleSheet . create ( {
142139 centerFlex : {
143140 flexDirection : 'row' ,
0 commit comments