Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
naoufal committed May 22, 2015
0 parents commit 006e201
Show file tree
Hide file tree
Showing 5 changed files with 221 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules
npm-debug.log
.DS_Store
85 changes: 85 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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 (
<ListView
dataSource={this.state.dataSource}
renderRow={this._renderRow}
/>
);
},

_renderRow() {
var header = (
<View style={...}>
<Text>Click to Expand</Text>
</View>
);

var content = (
<View style={...}>
<Text>This content is hidden in the accordion</Text>
</View>
);

return (
<Accordion
header={header}
content={content}
easing="easeOutCubic"
/>
);
}
});
```

## 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.
1 change: 1 addition & 0 deletions index.ios.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('./src/index');
36 changes: 36 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -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 <[email protected]> (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
}
}
96 changes: 96 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -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 */
<View>
<TouchableHighlight
ref="AccordionHeader"
onPress={this.toggleAccordion}
underlayColor={this.props.underlayColor}
style={this.props.style}
>
{this.props.header}
</TouchableHighlight>
<View
ref="AccordionContentWrapper"
style={{
height: this.getTweeningValue('height')
}}
>
<View ref="AccordionContent">
{this.props.content}
</View>
</View>
</View>
/*jshint ignore:end */
);
}
});

module.exports = Accordion;

0 comments on commit 006e201

Please sign in to comment.