Skip to content

Commit

Permalink
refactor name and add animation when loading (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
gusgard committed Nov 19, 2017
1 parent 831d10b commit 0a2cd67
Show file tree
Hide file tree
Showing 10 changed files with 85 additions and 39 deletions.
34 changes: 19 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# :godmode: Grid list component
# :foggy: Grid list component

<p align="left">
<a href="https://npmjs.org/package/react-native-grid-list"><img alt="npm version" src="http://img.shields.io/npm/v/react-native-grid-list.svg"></a>
Expand All @@ -22,9 +22,15 @@ npm install react-native-grid-list --save

## Example

### Expo

[Example](https://snack.expo.io/@gusgard/react-native-grid-list)

### Code

```js
import React, { Component } from 'react';
import { StyleSheet, Image } from 'react-native';
import React, { PureComponent } from 'react';
import { View, StyleSheet, Image } from 'react-native';

import GridList from 'react-native-grid-list';

Expand All @@ -43,7 +49,7 @@ export default class App extends PureComponent {
render() {
return (
<View style={styles.container}>
<ListGrid
<GridList
showSeparator
data={items}
numColumns={3}
Expand All @@ -67,19 +73,17 @@ const styles = StyleSheet.create({
});
```

### Expo

[Example QR](https://snack.expo.io/girdList)

## Props

| Prop | Default | Type | Description |
| :------------ | :--------: | :-------------: | :----------------------------------------- |
| numColumns | 3 | `number` | Number of elements in a row |
| data | _required_ | `array` | Data used when render items |
| renderItem | _required_ | `func` | Function that render each item of the grid |
| itemStyle | {} | `ViewPropTypes` | Style for the wrapper of item |
| showSeparator | false | `bool` | Show a separator between items |
| Prop | Default | Type | Description |
| :---------------- | :--------: | :-------------: | :----------------------------------------- |
| numColumns | 3 | `number` | Number of elements in a row |
| data | _required_ | `array` | Data used when render items |
| renderItem | _required_ | `func` | Function that render each item of the grid |
| itemStyle | {} | `ViewPropTypes` | Style for the wrapper of item |
| showSeparator | false | `bool` | Show a separator between items |
| showAnimation | false | `bool` | Show an animation when load item |
| animationDuration | 500 | `number` | Duration of the animation |

## Author

Expand Down
Binary file modified demo.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
15 changes: 10 additions & 5 deletions example/App.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React, { PureComponent } from 'react';
import { Image, StyleSheet, View } from 'react-native';
import ListGrid from 'react-native-grid-list';
import GridList from 'react-native-grid-list';

const newImage = {
0: 'business',
Expand All @@ -23,17 +23,22 @@ const image = index => ({
},
});

const items = Array.from(Array(30)).map((_, index) => image(index));
const items = Array.from(Array(20)).map((_, index) => image(index));

export default class App extends PureComponent {
renderItem = ({ item }) => (
<Image style={styles.image} source={item.thumbnail} />
renderItem = ({ item, stagger }) => (
<Image
style={styles.image}
source={item.thumbnail}
onLoad={() => stagger.start()}
/>
);

render() {
return (
<View style={styles.container}>
<ListGrid
<GridList
showAnimation
showSeparator
data={items}
numColumns={3}
Expand Down
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import { ListGrid } from './src/components';
import { GridList } from './src/components';

export default ListGrid;
export default GridList;
17 changes: 7 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
"url": "git+https://github.com/gusgard/react-native-grid-list.git"
},
"description": "React native grid list component",
"bugs": {
"url": "https://github.com/gusgard/react-native-grid-list/issues"
},
"homepage": "https://github.com/gusgard/react-native-grid-list#readme",
"keywords": [
"grid list",
"grid",
Expand Down Expand Up @@ -58,17 +62,10 @@
"jest": {
"preset": "react-native",
"setupTestFrameworkScriptFile": "./enzyme.js",
"modulePathIgnorePatterns": [
"<rootDir>/example/node_modules/"
],
"snapshotSerializers": [
"./node_modules/enzyme-to-json/serializer"
]
"modulePathIgnorePatterns": ["<rootDir>/example/node_modules/"],
"snapshotSerializers": ["./node_modules/enzyme-to-json/serializer"]
},
"pre-commit": {
"run": [
"lint",
"test"
]
"run": ["lint", "test"]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
exports[`renders correctly 1`] = `
<FlatList
ItemSeparatorComponent={[Function]}
animationDuration={500}
contentContainerStyle={
Object {
"borderBottomWidth": 5,
Expand Down Expand Up @@ -36,6 +37,7 @@ exports[`renders correctly 1`] = `
onEndReachedThreshold={2}
renderItem={[Function]}
scrollEventThrottle={50}
showAnimation={false}
showsVerticalScrollIndicator={false}
updateCellsBatchingPeriod={50}
windowSize={21}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React, { PureComponent } from 'react';
import PropTypes from 'prop-types';
import { ViewPropTypes, View, FlatList } from 'react-native';
import { Animated, ViewPropTypes, View, FlatList } from 'react-native';

import { width } from '../../themes';

Expand All @@ -11,27 +11,53 @@ const calcGridDimension = numColumns => ({
width: width / numColumns,
});

export default class ListGrid extends PureComponent {
export default class GridList extends PureComponent {
static propTypes = {
data: PropTypes.array.isRequired,
numColumns: PropTypes.number.isRequired,
renderItem: PropTypes.func.isRequired,
showSeparator: PropTypes.bool,
showAnimation: PropTypes.bool,
animationDuration: PropTypes.number,
itemStyle: ViewPropTypes.style,
};

static defaultProps = {
numColumns: 3,
itemStyle: {},
showSeparator: false,
showAnimation: false,
animationDuration: 500,
};

componentWillMount() {
if (this.props.showAnimation) {
const { data, numColumns, animationDuration } = this.props;
this.animatedValue = [];

this.stagger = data.map((_, index) => {
this.animatedValue[index] = new Animated.Value(0);
return Animated.stagger(0, [
Animated.timing(this.animatedValue[index], {
toValue: 1,
duration: animationDuration * Math.ceil((index + 1) / numColumns),
}),
]);
});
}
}
itemDimension = calcGridDimension(this.props.numColumns);

_keyExtractor = (item, index) => index;

_renderItem = ({ item, index }) => {
const { showSeparator, renderItem, numColumns, itemStyle } = this.props;
const {
showAnimation,
showSeparator,
renderItem,
numColumns,
itemStyle,
} = this.props;
let style = {};
if (showSeparator) {
const position = index % numColumns;
Expand All @@ -43,7 +69,19 @@ export default class ListGrid extends PureComponent {
style = styles.imageCenter;
}
}
return (

return showAnimation ? (
<Animated.View
style={[
style,
this.itemDimension,
{ opacity: this.animatedValue[index] },
itemStyle,
]}
>
{renderItem({ item, index, stagger: this.stagger[index] })}
</Animated.View>
) : (
<View style={[style, this.itemDimension, itemStyle]}>
{renderItem({ item, index })}
</View>
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ import { shallow } from 'enzyme';
import React from 'react';
import { Image } from 'react-native';

import ListGrid from './index';
import GridList from './index';

const logo = { uri: 'https://...' };
const items = [{ id: 1, thumbnail: logo }, { id: 2, thumbnail: logo }];

it('renders correctly', () => {
const wrapper = shallow(
<ListGrid
<GridList
data={items}
renderItem={({ item }) => <Image source={item.thumbnail} />}
/>,
Expand Down
2 changes: 1 addition & 1 deletion src/components/index.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export { default as ListGrid } from './ListGrid';
export { default as GridList } from './GridList';

0 comments on commit 0a2cd67

Please sign in to comment.