Skip to content

Commit efbd556

Browse files
author
amoreno
committed
Support animations
1 parent 8774479 commit efbd556

File tree

1 file changed

+49
-19
lines changed

1 file changed

+49
-19
lines changed

lib/MaterialSearchBar.js

Lines changed: 49 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,14 @@ import {
1313
type Props = {
1414
containerStyle: Object | number,
1515
styleInput: Object | number,
16-
searchIcon: Node,
17-
closeIcon: Node,
16+
searchIcon: Object,
17+
closeIcon: Object,
1818
onChangeText: Function
1919
}
2020
type State = {
21-
isEmpty: boolean
21+
isEmpty: boolean,
22+
scale: number,
23+
value: string
2224
}
2325

2426
class MaterialSearchBar extends React.Component<Props, State> {
@@ -28,23 +30,49 @@ class MaterialSearchBar extends React.Component<Props, State> {
2830
constructor(props: Props) {
2931
super(props)
3032
this.state = {
31-
isEmpty: true
33+
isEmpty: true,
34+
scale: new Animated.Value(1),
35+
value: ''
3236
}
3337
}
3438

3539
_onPressButton = () => {
36-
this._textInput.clear()
37-
this.setState({ isEmpty: true }, () => {
40+
this.setState({ isEmpty: true, value: '' }, () => {
3841
this.props.onChangeText('')
3942
})
4043
}
4144

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+
4260
_onChangeText = (value: string) => {
4361
this.props.onChangeText(value)
4462
if (value) {
45-
this.setState({ isEmpty: false })
63+
this.setState({ isEmpty: false, value })
4664
} 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()
4876
}
4977
}
5078

@@ -54,32 +82,34 @@ class MaterialSearchBar extends React.Component<Props, State> {
5482
console.warn('MaterialSearchBar is only available on Android.')
5583
return null
5684
}
57-
const isLowAPI: boolean = Platform.Version < 21
5885
return (
5986
<View style={this.props.containerStyle}>
6087
<TextInput
6188
{...this.props}
6289
ref={(ref: any) => {
6390
this._textInput = ref
6491
}}
92+
value={this.state.value}
6593
style={this.props.styleInput}
6694
onChangeText={this._onChangeText}
6795
/>
6896
<TouchableNativeFeedback
6997
onPress={this._onPressButton}
7098
background={
71-
isLowAPI
99+
Platform.Version < 21
72100
? TouchableNativeFeedback.SelectableBackground()
73101
: TouchableNativeFeedback.SelectableBackgroundBorderless()
74102
}>
75103
<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+
]}>
83113
{isEmpty ? this.props.searchIcon : this.props.closeIcon}
84114
</Animated.View>
85115
</TouchableNativeFeedback>
@@ -110,8 +140,8 @@ MaterialSearchBar.propTypes = {
110140
/** Override the inline-styles of the root element. */
111141
containerStyle: PropTypes.oneOfType([PropTypes.number, PropTypes.object]),
112142
styleInput: PropTypes.oneOfType([PropTypes.number, PropTypes.object]),
113-
searchIcon: PropTypes.node,
114-
closeIcon: PropTypes.node,
143+
searchIcon: PropTypes.element,
144+
closeIcon: PropTypes.element,
115145
onChangeText: PropTypes.func.isRequired
116146
}
117147

0 commit comments

Comments
 (0)