Skip to content

Commit

Permalink
Support ListEmptyComponent
Browse files Browse the repository at this point in the history
  • Loading branch information
janicduplessis committed Oct 26, 2017
1 parent 382d8a4 commit 8982298
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/MasonryList.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export type Props = {
>,
getHeightForItem: ({ item: any, index: number }) => number,
ListHeaderComponent?: ?React.ComponentType<any>,
ListEmptyComponent?: ?React.ComponentType<any>,
/**
* Used to extract a unique key for a given item at the specified index. Key is used for caching
* and as the react key to track item re-ordering. The default extractor checks `item.key`, then
Expand Down Expand Up @@ -217,6 +218,7 @@ export default class MasonryList extends React.Component<Props, State> {
const {
renderItem,
ListHeaderComponent,
ListEmptyComponent,
keyExtractor,
onEndReached,
...props
Expand All @@ -225,6 +227,10 @@ export default class MasonryList extends React.Component<Props, State> {
if (ListHeaderComponent) {
headerElement = <ListHeaderComponent />;
}
let emptyElement;
if (ListEmptyComponent) {
emptyElement = <ListEmptyComponent />;
}

const content = (
<View style={styles.contentContainer}>
Expand Down Expand Up @@ -263,7 +269,7 @@ export default class MasonryList extends React.Component<Props, State> {
onMomentumScrollEnd: this._onMomentumScrollEnd,
},
headerElement,
content,
emptyElement && this.props.data.length === 0 ? emptyElement : content,
);

return scrollComponent;
Expand Down
14 changes: 14 additions & 0 deletions src/__tests__/MasonryList-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,18 @@ describe('MasonryList', () => {
.toJSON();
expect(component).toMatchSnapshot();
});

it('should render empty component', () => {
const component = renderer
.create(
<MasonryList
data={[]}
getHeightForItem={() => 1}
ListEmptyComponent={() => <empty />}
renderItem={({ item }) => <item value={item.key} />}
/>,
)
.toJSON();
expect(component).toMatchSnapshot();
});
});
23 changes: 23 additions & 0 deletions src/__tests__/__snapshots__/MasonryList-test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -183,3 +183,26 @@ exports[`MasonryList should render a single column list 1`] = `
</View>
</RCTScrollView>
`;

exports[`MasonryList should render empty component 1`] = `
<RCTScrollView
ListEmptyComponent={[Function]}
data={Array []}
getHeightForItem={[Function]}
numColumns={1}
onContentSizeChange={[Function]}
onLayout={[Function]}
onMomentumScrollEnd={[Function]}
onScroll={[Function]}
onScrollBeginDrag={[Function]}
onScrollEndDrag={[Function]}
removeClippedSubviews={false}
renderItem={[Function]}
renderScrollComponent={[Function]}
scrollEventThrottle={50}
>
<View>
<empty />
</View>
</RCTScrollView>
`;

0 comments on commit 8982298

Please sign in to comment.