Clean up separator stuff

Reviewed By: bvaughn

Differential Revision: D4561262

fbshipit-source-id: 990f338c197851252bea4902fa86249d6e1c975b
This commit is contained in:
Spencer Ahrens 2017-02-16 18:59:57 -08:00 committed by Facebook Github Bot
parent 63d3ea17a7
commit 5042bae259
1 changed files with 13 additions and 23 deletions

View File

@ -219,36 +219,33 @@ class VirtualizedSectionList<SectionT: SectionBase>
return <this.props.SectionHeaderComponent section={info.section} />; return <this.props.SectionHeaderComponent section={info.section} />;
} else { } else {
const ItemComponent = info.section.ItemComponent || this.props.ItemComponent; const ItemComponent = info.section.ItemComponent || this.props.ItemComponent;
const SeparatorComponent = info.section.SeparatorComponent || this.props.SeparatorComponent; const SeparatorComponent = this._getSeparatorComponent(index, info);
const {SectionSeparatorComponent} = this.props;
const [shouldRenderSectionSeparator, shouldRenderItemSeparator] =
this._shouldRenderSeparators(index, info);
return ( return (
<View> <View>
<ItemComponent item={item} index={info.index} /> <ItemComponent item={item} index={info.index} />
{shouldRenderItemSeparator ? <SeparatorComponent /> : null} {SeparatorComponent && <SeparatorComponent />}
{shouldRenderSectionSeparator ? <SectionSeparatorComponent /> : null}
</View> </View>
); );
} }
}; };
_shouldRenderSeparators(index: number, info?: ?Object): [boolean, boolean] { _getSeparatorComponent(index: number, info?: ?Object): ?ReactClass<*> {
info = info || this._subExtractor(index); info = info || this._subExtractor(index);
if (!info) { if (!info) {
return [false, false]; return null;
} }
const SeparatorComponent = info.section.SeparatorComponent || this.props.SeparatorComponent; const SeparatorComponent = info.section.SeparatorComponent || this.props.SeparatorComponent;
const {SectionSeparatorComponent} = this.props; const {SectionSeparatorComponent} = this.props;
const lastItemIndex = this.state.childProps.getItemCount() - 1; const lastItemIndex = this.state.childProps.getItemCount() - 1;
let shouldRenderSectionSeparator = false; if (SectionSeparatorComponent &&
if (SectionSeparatorComponent) { info.index === info.section.data.length - 1 &&
shouldRenderSectionSeparator = index < lastItemIndex) {
info.index === info.section.data.length - 1 && index < lastItemIndex; return SectionSeparatorComponent;
} }
const shouldRenderItemSeparator = SeparatorComponent && !shouldRenderSectionSeparator && if (SeparatorComponent && index < lastItemIndex) {
index < lastItemIndex; return SeparatorComponent;
return [shouldRenderSectionSeparator, shouldRenderItemSeparator]; }
return null;
} }
_shouldItemUpdate = (prev, next) => { _shouldItemUpdate = (prev, next) => {
@ -256,14 +253,7 @@ class VirtualizedSectionList<SectionT: SectionBase>
if (!shouldItemUpdate || shouldItemUpdate(prev, next)) { if (!shouldItemUpdate || shouldItemUpdate(prev, next)) {
return true; return true;
} }
if (prev.index !== next.index) { return this._getSeparatorComponent(prev.index) !== this._getSeparatorComponent(next.index);
const [secSepPrev, itSepPrev] = this._shouldRenderSeparators(prev.index);
const [secSepNext, itSepNext] = this._shouldRenderSeparators(next.index);
if (secSepPrev !== secSepNext || itSepPrev !== itSepNext) {
return true;
}
}
return false;
} }
_computeState(props: Props<SectionT>): State { _computeState(props: Props<SectionT>): State {