Skip to content

Commit 8982298

Browse files
Support ListEmptyComponent
1 parent 382d8a4 commit 8982298

File tree

3 files changed

+44
-1
lines changed

3 files changed

+44
-1
lines changed

src/MasonryList.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ export type Props = {
4949
>,
5050
getHeightForItem: ({ item: any, index: number }) => number,
5151
ListHeaderComponent?: ?React.ComponentType<any>,
52+
ListEmptyComponent?: ?React.ComponentType<any>,
5253
/**
5354
* Used to extract a unique key for a given item at the specified index. Key is used for caching
5455
* and as the react key to track item re-ordering. The default extractor checks `item.key`, then
@@ -217,6 +218,7 @@ export default class MasonryList extends React.Component<Props, State> {
217218
const {
218219
renderItem,
219220
ListHeaderComponent,
221+
ListEmptyComponent,
220222
keyExtractor,
221223
onEndReached,
222224
...props
@@ -225,6 +227,10 @@ export default class MasonryList extends React.Component<Props, State> {
225227
if (ListHeaderComponent) {
226228
headerElement = <ListHeaderComponent />;
227229
}
230+
let emptyElement;
231+
if (ListEmptyComponent) {
232+
emptyElement = <ListEmptyComponent />;
233+
}
228234

229235
const content = (
230236
<View style={styles.contentContainer}>
@@ -263,7 +269,7 @@ export default class MasonryList extends React.Component<Props, State> {
263269
onMomentumScrollEnd: this._onMomentumScrollEnd,
264270
},
265271
headerElement,
266-
content,
272+
emptyElement && this.props.data.length === 0 ? emptyElement : content,
267273
);
268274

269275
return scrollComponent;

src/__tests__/MasonryList-test.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,18 @@ describe('MasonryList', () => {
5252
.toJSON();
5353
expect(component).toMatchSnapshot();
5454
});
55+
56+
it('should render empty component', () => {
57+
const component = renderer
58+
.create(
59+
<MasonryList
60+
data={[]}
61+
getHeightForItem={() => 1}
62+
ListEmptyComponent={() => <empty />}
63+
renderItem={({ item }) => <item value={item.key} />}
64+
/>,
65+
)
66+
.toJSON();
67+
expect(component).toMatchSnapshot();
68+
});
5569
});

src/__tests__/__snapshots__/MasonryList-test.js.snap

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,3 +183,26 @@ exports[`MasonryList should render a single column list 1`] = `
183183
</View>
184184
</RCTScrollView>
185185
`;
186+
187+
exports[`MasonryList should render empty component 1`] = `
188+
<RCTScrollView
189+
ListEmptyComponent={[Function]}
190+
data={Array []}
191+
getHeightForItem={[Function]}
192+
numColumns={1}
193+
onContentSizeChange={[Function]}
194+
onLayout={[Function]}
195+
onMomentumScrollEnd={[Function]}
196+
onScroll={[Function]}
197+
onScrollBeginDrag={[Function]}
198+
onScrollEndDrag={[Function]}
199+
removeClippedSubviews={false}
200+
renderItem={[Function]}
201+
renderScrollComponent={[Function]}
202+
scrollEventThrottle={50}
203+
>
204+
<View>
205+
<empty />
206+
</View>
207+
</RCTScrollView>
208+
`;

0 commit comments

Comments
 (0)