From 006e201d9422f0b27dce8dc92a6a5bf0926cf914 Mon Sep 17 00:00:00 2001 From: Naoufal Kadhom Date: Thu, 21 May 2015 22:04:08 -0400 Subject: [PATCH] initial commit --- .gitignore | 3 ++ README.md | 85 ++++++++++++++++++++++++++++++++++++++++++++++ index.ios.js | 1 + package.json | 36 ++++++++++++++++++++ src/index.js | 96 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 221 insertions(+) create mode 100644 .gitignore create mode 100644 README.md create mode 100644 index.ios.js create mode 100644 package.json create mode 100644 src/index.js diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b0e3907 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +node_modules +npm-debug.log +.DS_Store diff --git a/README.md b/README.md new file mode 100644 index 0000000..2d165b4 --- /dev/null +++ b/README.md @@ -0,0 +1,85 @@ +# react-native-accordion +[![npm](https://img.shields.io/npm/v/react-native-accordion.svg?style=flat-square)](https://www.npmjs.com/package/react-native-accordion) + +__`react-native-accordion`__ is an easy to use Accordion component for [React Native](https://facebook.github.io/react-native/) app. + +![accordion](https://cloud.githubusercontent.com/assets/1627824/7762243/801c1e46-ffff-11e4-9a36-2183704b6ec6.gif) + +## Install +```shell +npm i --save react-native-accordion +``` + +## Usage +Using an Accordion in your app will usually look like this: +```js +var Accordion = require('react-native-accordion'); + +var YourComponent = React.createClass({ + getInitialState() { + var ds = new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2}); + return { + dataSource: ds.cloneWithRows(_.range(25)), + }; + }, + + render() { + return ( + + ); + }, + + _renderRow() { + var header = ( + + Click to Expand + + ); + + var content = ( + + This content is hidden in the accordion + + ); + + return ( + + ); + } +}); +``` + +## Examples +Here are a few examples of how you can use an accordion in your app: + +|Transit App|Tweetbot| +|---|---| +|[![accordion-transit](https://cloud.githubusercontent.com/assets/1627824/7757509/ffee4358-ffd0-11e4-9fc5-13c8d6f09ad0.gif)](https://itunes.apple.com/ca/app/transit-app-real-time-bus/id498151501)|[![accordion-tweetbot](https://cloud.githubusercontent.com/assets/1627824/7757570/6b391106-ffd1-11e4-9191-e501e81ca506.gif)](https://itunes.apple.com/ca/app/tweetbot-3-for-twitter.-elegant/id722294701)| + +## Props +The following props can be used to modify the Accordion's style and/or behaviour: + +| Prop | Type | Opt/Required | Default | Note | +|---|---|---|---|---| +|__`activeOpacity`__|_Number_|Optional|`1`|The active opacity of the [TouchableHighlight](https://facebook.github.io/react-native/docs/touchablehighlight.html). +|__`animationDuration`__|_Number_|Optional|`300`|The duration of the animation. +|__`content`__|_Element_|Required|`N/A`|The content you want hidden in the collapse accordion. +|__`easing`__|_String_|Optional|`linear`| A tweening function from [tween-functions](https://github.com/chenglou/tween-functions). +|__`header`__|_Element_|Required|`N/A`|The element that will expand the accordion when pressed. +|__`underlayColor`__|_String_|Optional|`#000`|The underlay color of the [TouchableHighlight](https://facebook.github.io/react-native/docs/touchablehighlight.html). +|__`style`__|_Object_|Optional|`{}`|The styles you want to set on the accordion element. + + +## License +Copyright (c) 2015, Naoufal Kadhom + +Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies. + +THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. diff --git a/index.ios.js b/index.ios.js new file mode 100644 index 0000000..f472c54 --- /dev/null +++ b/index.ios.js @@ -0,0 +1 @@ +module.exports = require('./src/index'); diff --git a/package.json b/package.json new file mode 100644 index 0000000..cf6b324 --- /dev/null +++ b/package.json @@ -0,0 +1,36 @@ +{ + "name": "react-native-accordion", + "version": "0.1.0", + "description": "An Accordion Component for React Native", + "main": "index.ios.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "repository": { + "type": "git", + "url": "https://github.com/naoufal/react-native-accordion.git" + }, + "keywords": [ + "react", + "react-native", + "native", + "accordion", + "react-component" + ], + "author": "Naoufal Kadhom (https://github.com/naoufal)", + "license": "ISC", + "bugs": { + "url": "https://github.com/naoufal/react-native-accordion/issues" + }, + "homepage": "https://github.com/naoufal/react-native-accordion", + "dependencies": { + "react-tween-state": "0.0.5" + }, + "peerDependencies": { + "react-native": "^0.4.0" + }, + "jshintConfig": { + "esnext": true, + "node": true + } +} diff --git a/src/index.js b/src/index.js new file mode 100644 index 0000000..6022d6c --- /dev/null +++ b/src/index.js @@ -0,0 +1,96 @@ +'use strict'; + +var React = require('react-native'); +var tweenState = require('react-tween-state'); + +var { + StyleSheet, + TouchableHighlight, + View, + Text +} = React; + +var Accordion = React.createClass({ + mixins: [tweenState.Mixin], + + propTypes: { + activeOpacity: React.PropTypes.number, + animationDuration: React.PropTypes.number, + content: React.PropTypes.element.isRequired, + easing: React.PropTypes.string, + header: React.PropTypes.element.isRequired, + underlayColor: React.PropTypes.string, + style: React.PropTypes.object + }, + + getDefaultProps() { + return { + activeOpacity: 1, + animationDuration: 300, + easing: 'linear', + underlayColor: '#000', + style: {} + }; + }, + + getInitialState() { + return { + is_visible: false, + height: 0, + content_height: 0 + }; + }, + + toggleAccordion() { + this.state.is_visible = !this.state.is_visible; + + this.tweenState('height', { + easing: tweenState.easingTypes[this.props.easing], + duration: this.props.animationDuration, + endValue: this.state.height === 0 ? this.state.content_height : 0 + }); + }, + + getContentHeight() { + this.refs.AccordionContent.measure((ox, oy, width, height, px, py) => { + // Sets content height in state + this.setState({content_height: height}); + }); + }, + + componentDidMount() { + // Gets content height when component mounts + // without setTimeout, measure returns 0 for every value. + // See https://github.com/facebook/react-native/issues/953 + setTimeout(this.getContentHeight); + }, + + render() { + return ( + /*jshint ignore:start */ + + + {this.props.header} + + + + {this.props.content} + + + + /*jshint ignore:end */ + ); + } +}); + +module.exports = Accordion;