Skip to content

Instantly share code, notes, and snippets.

@talk2MeGooseman
Last active July 20, 2017 17:37
Show Gist options
  • Save talk2MeGooseman/514b0b45fcfde37f9a51036cbac007d8 to your computer and use it in GitHub Desktop.
Save talk2MeGooseman/514b0b45fcfde37f9a51036cbac007d8 to your computer and use it in GitHub Desktop.
React Native Code example to make a panel the expands on touch
import React, { Component, PropTypes } from 'react';
import {
View,
LayoutAnimation,
StyleSheet,
Text,
TouchableOpacity,
} from 'react-native';
const { any, bool, func, string } = PropTypes;
class Panel extends Component {
static propTypes = {
children: any,
title: string,
expanded: bool,
onCollapse: func,
onExpand: func,
style: any,
};
static defaultProps = {
expanded: false,
onCollapse: emptyFn,
onExpand: emptyFn,
};
onToggle = () => {
LayoutAnimation.spring();
this.setState({
height: this.state.height === null ? 0 : null,
})
}
constructor(props) {
super(props);
this.state = {
height: this.props.expanded ? null : 0,
};
}
render() {
const { children, style, title } = this.props;
const { height } = this.state;
return (
<View style={[styles.main, style]}>
<TouchableOpacity onPress={this.onToggle}>
<Text style={styles.title}>
{title}
</Text>
</TouchableOpacity>
<View style={{ height }}>
{children}
</View>
</View>
);
}
}
function emptyFn() {};
const styles = StyleSheet.create({
main: {
backgroundColor: '#fff',
borderRadius: 3,
overflow: 'hidden',
},
title: {
fontWeight: 'bold',
padding: 15,
}
});
export default Panel;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment