@@ -13,12 +13,14 @@ import {
13
13
type Props = {
14
14
containerStyle : Object | number ,
15
15
styleInput : Object | number ,
16
- searchIcon : Node ,
17
- closeIcon : Node ,
16
+ searchIcon : Object ,
17
+ closeIcon : Object ,
18
18
onChangeText : Function
19
19
}
20
20
type State = {
21
- isEmpty : boolean
21
+ isEmpty : boolean ,
22
+ scale : number ,
23
+ value : string
22
24
}
23
25
24
26
class MaterialSearchBar extends React . Component < Props , State > {
@@ -28,23 +30,49 @@ class MaterialSearchBar extends React.Component<Props, State> {
28
30
constructor ( props : Props ) {
29
31
super ( props )
30
32
this . state = {
31
- isEmpty : true
33
+ isEmpty : true ,
34
+ scale : new Animated . Value ( 1 ) ,
35
+ value : ''
32
36
}
33
37
}
34
38
35
39
_onPressButton = ( ) = > {
36
- this . _textInput . clear ( )
37
- this . setState ( { isEmpty : true } , ( ) => {
40
+ this . setState ( { isEmpty : true , value : '' } , ( ) => {
38
41
this . props . onChangeText ( '' )
39
42
} )
40
43
}
41
44
45
+ doAnimation = ( ) = > {
46
+ Animated . sequence ( [
47
+ Animated . timing ( this . state . scale , {
48
+ toValue : 1.2 ,
49
+ duration : 100 ,
50
+ useNativeDriver : true
51
+ } ) ,
52
+ Animated . timing ( this . state . scale , {
53
+ toValue : 1 ,
54
+ duration : 100 ,
55
+ useNativeDriver : true
56
+ } )
57
+ ] ) . start ( )
58
+ }
59
+
42
60
_onChangeText = ( value : string ) = > {
43
61
this . props . onChangeText ( value )
44
62
if ( value ) {
45
- this . setState ( { isEmpty : false } )
63
+ this . setState ( { isEmpty : false , value } )
46
64
} else {
47
- this . setState ( { isEmpty : true } )
65
+ this . setState ( { isEmpty : true , value } )
66
+ }
67
+
68
+ if ( this . state . value . length === 0 ) {
69
+ this . doAnimation ( )
70
+ }
71
+ }
72
+
73
+ componentWillUpdate ( nextProps : Props , nextState : State ) {
74
+ if ( nextState . value . length === 0 ) {
75
+ this . doAnimation ( )
48
76
}
49
77
}
50
78
@@ -54,32 +82,34 @@ class MaterialSearchBar extends React.Component<Props, State> {
54
82
console . warn ( 'MaterialSearchBar is only available on Android.' )
55
83
return null
56
84
}
57
- const isLowAPI : boolean = Platform . Version < 21
58
85
return (
59
86
< View style = { this . props . containerStyle } >
60
87
< TextInput
61
88
{ ...this . props }
62
89
ref = { ( ref : any ) => {
63
90
this . _textInput = ref
64
91
} }
92
+ value = { this . state . value }
65
93
style = { this . props . styleInput }
66
94
onChangeText = { this . _onChangeText }
67
95
/>
68
96
< TouchableNativeFeedback
69
97
onPress = { this . _onPressButton }
70
98
background = {
71
- isLowAPI
99
+ Platform . Version < 21
72
100
? TouchableNativeFeedback . SelectableBackground ( )
73
101
: TouchableNativeFeedback . SelectableBackgroundBorderless ( )
74
102
} >
75
103
< Animated . View
76
- // style={{
77
- // transform: [
78
- // { scale: this.state.scale },
79
- // { perspective: 1000 } // without this line this Animation will not render on Android while working fine on iOS
80
- // ]
81
- // }}
82
- >
104
+ style = { [
105
+ {
106
+ transform : [
107
+ {
108
+ scale : this . state . scale
109
+ }
110
+ ]
111
+ }
112
+ ] } >
83
113
{ isEmpty ? this . props . searchIcon : this . props . closeIcon }
84
114
</ Animated . View >
85
115
</ TouchableNativeFeedback >
@@ -110,8 +140,8 @@ MaterialSearchBar.propTypes = {
110
140
/** Override the inline-styles of the root element. */
111
141
containerStyle : PropTypes . oneOfType ( [ PropTypes . number , PropTypes . object ] ) ,
112
142
styleInput : PropTypes . oneOfType ( [ PropTypes . number , PropTypes . object ] ) ,
113
- searchIcon : PropTypes . node ,
114
- closeIcon : PropTypes . node ,
143
+ searchIcon : PropTypes . element ,
144
+ closeIcon : PropTypes . element ,
115
145
onChangeText : PropTypes . func . isRequired
116
146
}
117
147
0 commit comments