-
Notifications
You must be signed in to change notification settings - Fork 30
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
base: master
Are you sure you want to change the base?
Section headers #34
Changes from 1 commit
f5d14ca
d83fa3c
cba8549
39e81fd
073c262
6394033
53ac8fb
090b2d4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,14 +26,31 @@ 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.`); | ||
} | ||
}, | ||
|
||
/** | ||
* Whether to treat immutableData as a sectioned list, | ||
* applying section headers. | ||
*/ | ||
treatAsSections: PropTypes.bool, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can assume they want to render sections if There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good point! |
||
|
||
/** | ||
* 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 function that returns some {@link PropTypes.element} | ||
* to be rendered as a section row. It will be passed the item, | ||
* and the item's key. | ||
*/ | ||
renderRow: PropTypes.func, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we just leave it as There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yep I think so. |
||
|
||
/** | ||
* 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. | ||
|
@@ -58,14 +75,40 @@ class ImmutableVirtualizedList extends PureComponent { | |
|
||
static defaultProps = { | ||
...VirtualizedList.defaultProps, | ||
|
||
treatAsSections: false, | ||
renderEmptyInList: 'No data.', | ||
}; | ||
|
||
state = { | ||
flattenedData: this.props.treatAsSections | ||
? utils.flattenMap(this.props.immutableData) | ||
: null, | ||
} | ||
|
||
componentDidUpdate(prevProps) { | ||
const { treatAsSections, immutableData } = this.props; | ||
if (treatAsSections && prevProps.immutableData !== immutableData) { | ||
const flattenedData = utils.flattenMap(immutableData); | ||
this.setState({ | ||
flattenedData, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @cooperka I chose to hold There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. One alternative, to avoid using There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think using state is fine, but if you put it in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I chose There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry, |
||
stickyHeaderIndices: utils.getStickyHeaderIndices(flattenedData), | ||
}); | ||
} | ||
} | ||
|
||
getVirtualizedList() { | ||
return this.virtualizedListRef; | ||
} | ||
|
||
keyExtractor = (item, index) => ( | ||
this.props.treatAsSections | ||
? this.state.flattenedData | ||
.keySeq() | ||
.skip(index) | ||
.first() | ||
: String(index) | ||
) | ||
|
||
scrollToEnd = (...args) => | ||
this.virtualizedListRef && this.virtualizedListRef.scrollToEnd(...args); | ||
|
||
|
@@ -81,6 +124,20 @@ class ImmutableVirtualizedList extends PureComponent { | |
recordInteraction = (...args) => | ||
this.virtualizedListRef && this.virtualizedListRef.recordInteraction(...args); | ||
|
||
renderItem = (info) => ( | ||
this.props.treatAsSections | ||
? utils.isSectionHeader(info.item) | ||
? this.renderSectionHeader( | ||
info.item, | ||
this.keyExtractor(info.item, info.index), | ||
) | ||
: this.renderRow( | ||
info.item, | ||
this.keyExtractor(info.item, info.index), | ||
) | ||
: this.props.renderItem(info) | ||
) | ||
|
||
renderEmpty() { | ||
const { | ||
immutableData, renderEmpty, renderEmptyInList, contentContainerStyle, | ||
|
@@ -107,15 +164,29 @@ class ImmutableVirtualizedList extends PureComponent { | |
} | ||
|
||
render() { | ||
const { immutableData, renderEmpty, renderEmptyInList, ...passThroughProps } = this.props; | ||
const { | ||
immutableData, | ||
treatAsSections, | ||
renderEmpty, | ||
renderEmptyInList, | ||
...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={treatAsSections ? flattenedData : immutableData} | ||
getItem={(items, index) => ( | ||
treatAsSections | ||
? items.skip(index).first() | ||
: utils.getValueFromKey(index, items) | ||
)} | ||
getItemCount={(items) => (items.size || 0)} | ||
keyExtractor={(item, index) => String(index)} | ||
keyExtractor={this.keyExtractor} | ||
stickyHeaderIndices={treatAsSections ? stickyHeaderIndices : null} | ||
renderItem={this.renderItem} | ||
{...passThroughProps} | ||
/> | ||
); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -71,6 +71,28 @@ const utils = { | |
return immutableData.every((item) => !item || item.isEmpty()); | ||
}, | ||
|
||
flattenMap(data) { | ||
return data.reduce( | ||
(flattened, section, key) => flattened.set(key, section).merge(section), | ||
Immutable.OrderedMap().asMutable(), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think I understand but please clarify -- is There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes exactly, it's a little trick I learned after months of using immutablejs. Within our |
||
).asImmutable(); | ||
}, | ||
|
||
isSectionHeader(item) { | ||
return Immutable.Map.isMap(item) || Immutable.List.isList(item) | ||
}, | ||
|
||
getStickyHeaderIndices(items) { | ||
return items | ||
.valueSeq() | ||
.reduce((arr, item, i) => { | ||
if (utils.isSectionHeader(item)) { | ||
arr.push(i); | ||
} | ||
return arr; | ||
}, []); | ||
}, | ||
|
||
}; | ||
|
||
export default utils; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See comment below for how to avoid this. And just a general tip: it's preferable to type
// eslint-disable-next-line react/no-did-update-set-state
above your code instead of setting a global rule 👍