Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Removed the ; after view #2

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 25 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Optionally, you can add a prop `renderEndComponent`. It expects a function that
# Demo
![Demo](https://raw.githubusercontent.com/jjingrong/react-native-flatlist-with-end/master/demo.png)

# Example
# Example 1
```
<FlatListWithEnd
scrollEnabled
Expand All @@ -42,12 +42,35 @@ Optionally, you can add a prop `renderEndComponent`. It expects a function that
<Text style={{ textAlign: 'center' }}>
No more items, check back later!
</Text>
</View>;
</View>
);
}}
/>
```

# Example 2

```
<FlatListWithEnd
scrollEnabled
renderItem={({ item, index }) => this._renderItem(item, index)}
data={this.state.addToCartList}
renderEndComponent={() => {
return (
<View>
<View style={{ paddingVertical: 15 }}>
<Text style={{ textAlign: "center" }}>
No more items, check back later!
</Text>
</View>
{this.returnBillView()}
</View>
);
}}
keyExtractor={(item, index) => index.toString()}
/>
```

# API


Expand Down
78 changes: 38 additions & 40 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,58 +1,56 @@
import React from 'react';
import PropTypes from 'prop-types';
import {
View,
Text,
FlatList,
} from 'react-native';
import React from "react";
import PropTypes from "prop-types";
import { View, Text, FlatList } from "react-native";

// Hack: To detect that it is at the end.
const LastElementHash = { hash: 'XwPp9xazJ0ku5CZnlmgAx2Dld8SHkAeT' };
const LastElementHash = { hash: "XwPp9xazJ0ku5CZnlmgAx2Dld8SHkAeT" };

// Just a place holder element
const DefaultRenderEndComponent = () => {
return <View style={{ paddingVertical: 15 }}><Text style={{ textAlign: 'center' }}>End of list</Text></View>;
};

const FlatListWithEnd = (props) => {
// Clone the array
const data = props.data.slice(0);
data.push(LastElementHash);
/**
* A new renderItem function to replace the default
* Basically, we check if the item is the last element we manually pushed in earlier
* If so, we render the EndComponent
*
* @param {any} {item} Same as from FlatList
* @returns {Function} A function which returns a React Component.
*/
const renderItem = ({ item }) => {
if (item.hash === LastElementHash.hash) {
return props.renderEndComponent();
}
return props.renderItem({ item });
};
const mutatedProps = {
...props,
data,
renderItem,
};

return (
<FlatList
{...mutatedProps}
/>
<View style={{ paddingVertical: 15 }}>
<Text style={{ textAlign: "center" }}>End of list</Text>
</View>
);
};

class FlatListWithEnd extends React.Component {
render() {
// Clone the array
const data = this.props.data.slice(0);
data.push(LastElementHash);

/**
* A new renderItem function to replace the default
* Basically, we check if the item is the last element we manually pushed in earlier
* If so, we render the EndComponent
*
* @param {any} {item} Same as from FlatList
* @returns {Function} A function which returns a React Component.
*/
const renderItem = ({ item, index }) => {
if (item.hash === LastElementHash.hash) {
return this.props.renderEndComponent();
}
return this.props.renderItem({ item, index });
};
const mutatedProps = {
data,
renderItem
};

return <FlatList {...mutatedProps} />;
}
}

FlatListWithEnd.propTypes = {
data: PropTypes.oneOfType([PropTypes.array, PropTypes.object]).isRequired,
renderItem: PropTypes.func.isRequired,
renderEndComponent: PropTypes.func,
renderEndComponent: PropTypes.func
};

FlatListWithEnd.defaultProps = {
renderEndComponent: DefaultRenderEndComponent,
renderEndComponent: DefaultRenderEndComponent
};

export default FlatListWithEnd;