├── .gitignore ├── .npmignore ├── LICENSE ├── README.md ├── example ├── img │ ├── down.png │ └── up.png └── index.js ├── images └── demo.gif ├── package.json └── src └── index.js /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | *.log 3 | node_modules 4 | .vscode 5 | .DS_Store -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | *.log 2 | node_modules 3 | example 4 | images -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 San Pyae Lin 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # react-native-collapse-view 2 | 3 | 4 | 5 | 6 | 7 | ## Install package 8 | ```bash 9 | $ npm i react-native-collapse-view 10 | ``` 11 | 12 | ## Usage 13 | 14 | Import the component: 15 | ```js 16 | import CollapseView from 'react-native-collapse-view'; 17 | 18 | 19 | 23 | ``` 24 | ## Example 25 | 26 | Check full example in the [example](https://github.com/sanpyaelin/react-native-collapse-view/blob/master/example/index.js) folder. 27 | 28 | ## Properties 29 | 30 | Prop | Type | Default 31 | -----|------|--------- 32 | collapse | bool | false 33 | tension| number | 10 34 | renderView| func | 35 | renderCollapseView | func | 36 | 37 | ## License 38 | 39 | [MIT License](http://opensource.org/licenses/mit-license.html). © 2018 San Pyae Lin 40 | -------------------------------------------------------------------------------- /example/img/down.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sanpyaelin/react-native-collapse-view/33b2d1a6f2498588e3ee55096a2c0993f7767aca/example/img/down.png -------------------------------------------------------------------------------- /example/img/up.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sanpyaelin/react-native-collapse-view/33b2d1a6f2498588e3ee55096a2c0993f7767aca/example/img/up.png -------------------------------------------------------------------------------- /example/index.js: -------------------------------------------------------------------------------- 1 | import React, { Component } from "react"; 2 | import { Text, View, StyleSheet, Image } from "react-native"; 3 | import CollapseView from "react-native-collapse-view"; 4 | 5 | class Test extends Component { 6 | _renderView = (collapse) => { 7 | return( 8 | 9 | Toggle {collapse? 'on': 'off'} 10 | 11 | ) 12 | } 13 | 14 | _renderTensionView = (collapse) => { 15 | return( 16 | 17 | With tension effect 18 | 19 | ) 20 | } 21 | 22 | _renderIconView = (collapse) => { 23 | return( 24 | 25 | Toggle {collapse? 'on': 'off'} 26 | 27 | { 28 | collapse? 29 | : 30 | 31 | } 32 | 33 | 34 | ) 35 | } 36 | 37 | _renderCollapseView = (collapse) => { 38 | return( 39 | 40 | Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industrys standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum. 41 | 42 | ) 43 | } 44 | 45 | render() { 46 | return ( 47 | 48 | 53 | 57 | 61 | 62 | ); 63 | } 64 | } 65 | const styles = StyleSheet.create({ 66 | container: { 67 | flex: 1, 68 | backgroundColor: '#009688', 69 | }, 70 | view: { 71 | height:50, 72 | padding: 20, 73 | justifyContent:'center', 74 | backgroundColor:'#ffffff', 75 | }, 76 | collapseView: { 77 | padding: 20 78 | }, 79 | iconView: { 80 | padding: 20, 81 | alignItems: 'center', 82 | flexDirection: 'row', 83 | justifyContent: 'space-between', 84 | backgroundColor:'#ffffff', 85 | } 86 | }); 87 | export default Test; 88 | -------------------------------------------------------------------------------- /images/demo.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sanpyaelin/react-native-collapse-view/33b2d1a6f2498588e3ee55096a2c0993f7767aca/images/demo.gif -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "react-native-collapse-view", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "./src", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "keywords": [ 10 | "react", 11 | "native", 12 | "react-native", 13 | "collapse", 14 | "collapsible", 15 | "toggle", 16 | "accordion", 17 | "view" 18 | ], 19 | "repository": { 20 | "type": "git", 21 | "url": "git+https://github.com/sanpyaelin/react-native-collapse-view.git" 22 | }, 23 | "author": "San Pyae Lin", 24 | "license": "MIT", 25 | "bugs": { 26 | "url": "https://github.com/sanpyaelin/react-native-collapse-view/issues" 27 | }, 28 | "dependencies": { 29 | "prop-types": "^15.5.10" 30 | }, 31 | "homepage": "https://github.com/sanpyaelin/react-native-collapse-view#readme" 32 | } 33 | -------------------------------------------------------------------------------- /src/index.js: -------------------------------------------------------------------------------- 1 | import PropTypes from 'prop-types'; 2 | import React, { Component } from 'react'; 3 | import { View, StyleSheet, Animated, TouchableOpacity } from "react-native"; 4 | 5 | const propTypes = { 6 | renderView: PropTypes.func.isRequired, 7 | renderCollapseView: PropTypes.func.isRequired, 8 | collapse: PropTypes.bool, 9 | tension: PropTypes.number, 10 | } 11 | const defaultProps = { 12 | collapse: false, 13 | tension: 10 14 | }; 15 | 16 | class CollapseView extends Component { 17 | constructor(props) { 18 | super(props); 19 | this.state = { 20 | collapse: this.props.collapse, 21 | animation: new Animated.Value(), 22 | }; 23 | } 24 | 25 | collapse = () => { 26 | const { startpoint, endpoint, animation, collapse } = this.state; 27 | let startAnim = collapse? endpoint + startpoint : startpoint; 28 | let endAnim = collapse? startpoint : startpoint + endpoint; 29 | this.setState({ 30 | collapse: !this.state.collapse 31 | }) 32 | 33 | animation.setValue(startAnim); 34 | Animated.spring( 35 | this.state.animation, 36 | { 37 | toValue: endAnim, 38 | tension: this.props.tension, 39 | } 40 | ).start(); 41 | } 42 | 43 | startpoint = (layout) => { 44 | if(!this.state.collapse) this.setState({animation: new Animated.Value(layout.nativeEvent.layout.height)}); 45 | this.setState({ 46 | startpoint: layout.nativeEvent.layout.height 47 | }) 48 | } 49 | 50 | endpoint = (layout) => { 51 | if(this.state.collapse) this.setState({animation: new Animated.Value(layout.nativeEvent.layout.height)}); 52 | this.setState({ 53 | endpoint: layout.nativeEvent.layout.height, 54 | }) 55 | } 56 | 57 | render() { 58 | const { startpoint, endpoint, animation, collapse } = this.state; 59 | return ( 60 | 61 | 62 | {this.props.renderView(this.state.collapse)} 63 | 64 | 65 | {this.props.renderCollapseView(this.state.collapse)} 66 | 67 | 68 | ); 69 | } 70 | } 71 | 72 | CollapseView.propTypes = propTypes; 73 | CollapseView.defaultProps = defaultProps; 74 | export default CollapseView; --------------------------------------------------------------------------------