Skip to content

Commit 0a2cd67

Browse files
authored
refactor name and add animation when loading (#2)
1 parent 831d10b commit 0a2cd67

File tree

10 files changed

+85
-39
lines changed

10 files changed

+85
-39
lines changed

README.md

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# :godmode: Grid list component
1+
# :foggy: Grid list component
22

33
<p align="left">
44
<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>
@@ -22,9 +22,15 @@ npm install react-native-grid-list --save
2222

2323
## Example
2424

25+
### Expo
26+
27+
[Example](https://snack.expo.io/@gusgard/react-native-grid-list)
28+
29+
### Code
30+
2531
```js
26-
import React, { Component } from 'react';
27-
import { StyleSheet, Image } from 'react-native';
32+
import React, { PureComponent } from 'react';
33+
import { View, StyleSheet, Image } from 'react-native';
2834

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

@@ -43,7 +49,7 @@ export default class App extends PureComponent {
4349
render() {
4450
return (
4551
<View style={styles.container}>
46-
<ListGrid
52+
<GridList
4753
showSeparator
4854
data={items}
4955
numColumns={3}
@@ -67,19 +73,17 @@ const styles = StyleSheet.create({
6773
});
6874
```
6975

70-
### Expo
71-
72-
[Example QR](https://snack.expo.io/girdList)
73-
7476
## Props
7577

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

8488
## Author
8589

demo.gif

-1.69 MB
Loading

example/App.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React, { PureComponent } from 'react';
22
import { Image, StyleSheet, View } from 'react-native';
3-
import ListGrid from 'react-native-grid-list';
3+
import GridList from 'react-native-grid-list';
44

55
const newImage = {
66
0: 'business',
@@ -23,17 +23,22 @@ const image = index => ({
2323
},
2424
});
2525

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

2828
export default class App extends PureComponent {
29-
renderItem = ({ item }) => (
30-
<Image style={styles.image} source={item.thumbnail} />
29+
renderItem = ({ item, stagger }) => (
30+
<Image
31+
style={styles.image}
32+
source={item.thumbnail}
33+
onLoad={() => stagger.start()}
34+
/>
3135
);
3236

3337
render() {
3438
return (
3539
<View style={styles.container}>
36-
<ListGrid
40+
<GridList
41+
showAnimation
3742
showSeparator
3843
data={items}
3944
numColumns={3}

index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
import { ListGrid } from './src/components';
1+
import { GridList } from './src/components';
22

3-
export default ListGrid;
3+
export default GridList;

package.json

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99
"url": "git+https://github.com/gusgard/react-native-grid-list.git"
1010
},
1111
"description": "React native grid list component",
12+
"bugs": {
13+
"url": "https://github.com/gusgard/react-native-grid-list/issues"
14+
},
15+
"homepage": "https://github.com/gusgard/react-native-grid-list#readme",
1216
"keywords": [
1317
"grid list",
1418
"grid",
@@ -58,17 +62,10 @@
5862
"jest": {
5963
"preset": "react-native",
6064
"setupTestFrameworkScriptFile": "./enzyme.js",
61-
"modulePathIgnorePatterns": [
62-
"<rootDir>/example/node_modules/"
63-
],
64-
"snapshotSerializers": [
65-
"./node_modules/enzyme-to-json/serializer"
66-
]
65+
"modulePathIgnorePatterns": ["<rootDir>/example/node_modules/"],
66+
"snapshotSerializers": ["./node_modules/enzyme-to-json/serializer"]
6767
},
6868
"pre-commit": {
69-
"run": [
70-
"lint",
71-
"test"
72-
]
69+
"run": ["lint", "test"]
7370
}
7471
}

src/components/ListGrid/__snapshots__/test.js.snap renamed to src/components/GridList/__snapshots__/test.js.snap

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
exports[`renders correctly 1`] = `
44
<FlatList
55
ItemSeparatorComponent={[Function]}
6+
animationDuration={500}
67
contentContainerStyle={
78
Object {
89
"borderBottomWidth": 5,
@@ -36,6 +37,7 @@ exports[`renders correctly 1`] = `
3637
onEndReachedThreshold={2}
3738
renderItem={[Function]}
3839
scrollEventThrottle={50}
40+
showAnimation={false}
3941
showsVerticalScrollIndicator={false}
4042
updateCellsBatchingPeriod={50}
4143
windowSize={21}

src/components/ListGrid/index.js renamed to src/components/GridList/index.js

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React, { PureComponent } from 'react';
22
import PropTypes from 'prop-types';
3-
import { ViewPropTypes, View, FlatList } from 'react-native';
3+
import { Animated, ViewPropTypes, View, FlatList } from 'react-native';
44

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

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

14-
export default class ListGrid extends PureComponent {
14+
export default class GridList extends PureComponent {
1515
static propTypes = {
1616
data: PropTypes.array.isRequired,
1717
numColumns: PropTypes.number.isRequired,
1818
renderItem: PropTypes.func.isRequired,
1919
showSeparator: PropTypes.bool,
20+
showAnimation: PropTypes.bool,
21+
animationDuration: PropTypes.number,
2022
itemStyle: ViewPropTypes.style,
2123
};
2224

2325
static defaultProps = {
2426
numColumns: 3,
2527
itemStyle: {},
2628
showSeparator: false,
29+
showAnimation: false,
30+
animationDuration: 500,
2731
};
2832

33+
componentWillMount() {
34+
if (this.props.showAnimation) {
35+
const { data, numColumns, animationDuration } = this.props;
36+
this.animatedValue = [];
37+
38+
this.stagger = data.map((_, index) => {
39+
this.animatedValue[index] = new Animated.Value(0);
40+
return Animated.stagger(0, [
41+
Animated.timing(this.animatedValue[index], {
42+
toValue: 1,
43+
duration: animationDuration * Math.ceil((index + 1) / numColumns),
44+
}),
45+
]);
46+
});
47+
}
48+
}
2949
itemDimension = calcGridDimension(this.props.numColumns);
3050

3151
_keyExtractor = (item, index) => index;
3252

3353
_renderItem = ({ item, index }) => {
34-
const { showSeparator, renderItem, numColumns, itemStyle } = this.props;
54+
const {
55+
showAnimation,
56+
showSeparator,
57+
renderItem,
58+
numColumns,
59+
itemStyle,
60+
} = this.props;
3561
let style = {};
3662
if (showSeparator) {
3763
const position = index % numColumns;
@@ -43,7 +69,19 @@ export default class ListGrid extends PureComponent {
4369
style = styles.imageCenter;
4470
}
4571
}
46-
return (
72+
73+
return showAnimation ? (
74+
<Animated.View
75+
style={[
76+
style,
77+
this.itemDimension,
78+
{ opacity: this.animatedValue[index] },
79+
itemStyle,
80+
]}
81+
>
82+
{renderItem({ item, index, stagger: this.stagger[index] })}
83+
</Animated.View>
84+
) : (
4785
<View style={[style, this.itemDimension, itemStyle]}>
4886
{renderItem({ item, index })}
4987
</View>
File renamed without changes.

src/components/ListGrid/test.js renamed to src/components/GridList/test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@ import { shallow } from 'enzyme';
22
import React from 'react';
33
import { Image } from 'react-native';
44

5-
import ListGrid from './index';
5+
import GridList from './index';
66

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

1010
it('renders correctly', () => {
1111
const wrapper = shallow(
12-
<ListGrid
12+
<GridList
1313
data={items}
1414
renderItem={({ item }) => <Image source={item.thumbnail} />}
1515
/>,

src/components/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export { default as ListGrid } from './ListGrid';
1+
export { default as GridList } from './GridList';

0 commit comments

Comments
 (0)