Skip to content

Instantly share code, notes, and snippets.

@hijamoya
Last active December 5, 2016 07:04
Show Gist options
  • Save hijamoya/ed0a356811368c3789c06f58662f2570 to your computer and use it in GitHub Desktop.
Save hijamoya/ed0a356811368c3789c06f58662f2570 to your computer and use it in GitHub Desktop.
class Expandable extends Component{
constructor(props){
super(props);
this.icons = {
'up' : require('./images/up.png'),
'down' : require('./images/up.png')
};
this.state = {
title : props.title,
expanded : true,
animation : new Animated.Value()
};
}
toggle(){
let initialValue = this.state.expanded? this.state.maxHeight + this.state.minHeight : this.state.minHeight,
finalValue = this.state.expanded? this.state.minHeight : this.state.maxHeight + this.state.minHeight;
this.setState({
expanded : !this.state.expanded
});
this.state.animation.setValue(initialValue);
Animated.spring(
this.state.animation,
{
toValue: finalValue
}
).start();
}
_setMaxHeight(event){
this.setState({
maxHeight : event.nativeEvent.layout.height
});
}
_setMinHeight(event){
this.setState({
minHeight : event.nativeEvent.layout.height
});
}
render(){
let icon = this.icons['down'];
if(this.state.expanded){
icon = this.icons['up'];
}
return (
<Animated.View>
<View onLayout={this._setMinHeight.bind(this)}>
<Text>{this.state.title}</Text>
<TouchableHighlight onPress={this.toggle.bind(this)}>
<Image source={icon}></Image>
</TouchableHighlight>
</View>
<View style={styles.body} onLayout={this._setMaxHeight.bind(this)}>
{this.props.children}
</View>
</Animated.View>
);
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment