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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Section headers #34

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 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
82 changes: 73 additions & 9 deletions src/ImmutableVirtualizedList/ImmutableVirtualizedList.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import Immutable from 'immutable';
import PropTypes from 'prop-types';
import React, { PureComponent } from 'react';
import { Text, VirtualizedList } from 'react-native';
Expand Down Expand Up @@ -26,14 +25,18 @@ class ImmutableVirtualizedList extends PureComponent {
immutableData: (props, propName, componentName) => {
// Note: It's not enough to simply validate PropTypes.instanceOf(Immutable.Iterable),
// because different imports of Immutable.js across files have different class prototypes.
// TODO: Add support for Immutable.Map, etc.
if (Immutable.Map.isMap(props[propName])) {
return new Error(`Invalid prop ${propName} supplied to ${componentName}: Support for Immutable.Map is coming soon. For now, try an Immutable List, Set, or Range.`);
} else if (!utils.isImmutableIterable(props[propName])) {
if (!utils.isImmutableIterable(props[propName])) {
return new Error(`Invalid prop ${propName} supplied to ${componentName}: Must be instance of Immutable.Iterable.`);
}
},

/**
* A function that returns some {@link PropTypes.element}
* to be rendered as a section header. It will be passed a List
* or Map of the section's items, and the section key.
*/
renderSectionHeader: PropTypes.func,

/**
* A plain string, or a function that returns some {@link PropTypes.element}
* to be rendered in place of a `VirtualizedList` when there are no items in the list.
Expand Down Expand Up @@ -62,10 +65,42 @@ class ImmutableVirtualizedList extends PureComponent {
renderEmptyInList: 'No data.',
};

state = {
flattenedData: this.props.renderSectionHeader
? utils.flattenMap(this.props.immutableData)
: undefined,
stickyHeaderIndices: this.props.renderSectionHeader
? utils.getStickyHeaderIndices(this.props.immutableData)
: undefined,
};

componentWillReceiveProps(nextProps) {
const { immutableData } = this.props;
const { nextRenderSectionHeader, nextImmutableData } = nextProps;

if (nextRenderSectionHeader && immutableData !== nextImmutableData) {
this.setState({
flattenedData: utils.flattenMap(nextImmutableData),
stickyHeaderIndices: utils.getStickyHeaderIndices(nextImmutableData),
});
}
}

getVirtualizedList() {
return this.virtualizedListRef;
}

keyExtractor = (item, index) => {
if (this.props.renderSectionHeader) {
return this.state.flattenedData
.keySeq()
.skip(index)
.first();
}

return String(index);
};

scrollToEnd = (...args) =>
this.virtualizedListRef && this.virtualizedListRef.scrollToEnd(...args);

Expand All @@ -81,6 +116,20 @@ class ImmutableVirtualizedList extends PureComponent {
recordInteraction = (...args) =>
this.virtualizedListRef && this.virtualizedListRef.recordInteraction(...args);

renderItem = (info) => {
const { renderSectionHeader, renderItem } = this.props;

if (renderSectionHeader) {
const renderMethod = this.state.stickyHeaderIndices.includes(info.item)
? renderSectionHeader
: renderItem;

return renderMethod(info.item, this.keyExtractor(info.item, info.index));
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok the problem stems from this line. In your example app, you’re (understandably!) assuming that renderItem() will be called with the whole info object (as it is if we are dealing with a flat list without section headers, 3 lines below this comment). Will push a fix asap!

}

return renderItem(info);
};

renderEmpty() {
const {
immutableData, renderEmpty, renderEmptyInList, contentContainerStyle,
Expand All @@ -107,15 +156,30 @@ class ImmutableVirtualizedList extends PureComponent {
}

render() {
const { immutableData, renderEmpty, renderEmptyInList, ...passThroughProps } = this.props;
const {
immutableData,
renderEmpty,
renderEmptyInList,
renderSectionHeader,
renderItem,
...passThroughProps
} = this.props;

const { flattenedData, stickyHeaderIndices } = this.state;

return this.renderEmpty() || (
<VirtualizedList
ref={(component) => { this.virtualizedListRef = component; }}
data={immutableData}
getItem={(items, index) => utils.getValueFromKey(index, items)}
data={renderSectionHeader ? flattenedData : immutableData}
getItem={(items, index) => (
renderSectionHeader
? items.skip(index).first()
: utils.getValueFromKey(index, items)
)}
getItemCount={(items) => (items.size || 0)}
keyExtractor={(item, index) => String(index)}
keyExtractor={this.keyExtractor}
stickyHeaderIndices={renderSectionHeader ? stickyHeaderIndices : undefined}
renderItem={this.renderItem}
{...passThroughProps}
/>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,3 +122,45 @@ describe('ImmutableVirtualizedList with renderEmptyInList', () => {
expect(tree.toJSON()).toMatchSnapshot();
});
});

describe('ImmutableVirtualizedList with section headers', () => {
describe('Map of Maps', () => {
const tree = renderer.create(
<ImmutableVirtualizedList
immutableData={data.MAP_DATA_MAP_ROWS}
renderItem={renderers.renderRow}
renderSectionHeader={renderers.renderSectionHeader}
/>,
);

it('renders basic Map of Maps', () => {
expect(tree.toJSON()).toMatchSnapshot();
});

it('flattens the data as expected', () => {
const { flattenedData } = tree.getInstance().state;
expect(flattenedData).toBeDefined();
expect(flattenedData.size).toBe(4);
});
});

describe('Map of Lists', () => {
const tree = renderer.create(
<ImmutableVirtualizedList
immutableData={data.MAP_DATA_LIST_ROWS}
renderItem={renderers.renderRow}
renderSectionHeader={renderers.renderSectionHeader}
/>,
);

it('renders basic Map of Lists', () => {
expect(tree.toJSON()).toMatchSnapshot();
});

it('flattens the data as expected', () => {
const { flattenedData } = tree.getInstance().state;
expect(flattenedData).toBeDefined();
expect(flattenedData.size).toBe(9);
});
});
});
Loading