Skip to content

Commit 419662a

Browse files
author
Marco Cesarato
committed
refactor: improve code of inverted property and add horizontal cases
Refs: #31
1 parent 114a831 commit 419662a

File tree

2 files changed

+45
-33
lines changed

2 files changed

+45
-33
lines changed

lib/BigList.jsx

Lines changed: 44 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -606,7 +606,6 @@ class BigList extends PureComponent {
606606
renderItems() {
607607
const {
608608
keyExtractor,
609-
inverted,
610609
numColumns,
611610
hideMarginalsOnEmpty,
612611
hideHeaderOnEmpty,
@@ -629,15 +628,10 @@ class BigList extends PureComponent {
629628
} = this.props;
630629
const { items = [] } = this.state;
631630

632-
const defaultInvertedItemStyle = inverted
633-
? { transform: [{ scaleY: -1 }] }
634-
: {};
635-
636-
const defaultFullItemStyle = inverted
637-
? mergeViewStyle(defaultInvertedItemStyle, { width: "100%" })
638-
: { width: "100%" };
639-
640-
const defaultItemStyle = inverted ? defaultInvertedItemStyle : {};
631+
const itemStyle = this.getBaseStyle();
632+
const fullItemStyle = mergeViewStyle(itemStyle, {
633+
width: "100%",
634+
});
641635

642636
// On empty list
643637
const isEmptyList = this.isEmpty();
@@ -692,34 +686,28 @@ class BigList extends PureComponent {
692686
case BigListItemType.HEADER:
693687
if (ListHeaderComponent != null) {
694688
child = createElement(ListHeaderComponent);
695-
style = mergeViewStyle(
696-
defaultFullItemStyle,
697-
ListHeaderComponentStyle,
698-
);
689+
style = mergeViewStyle(fullItemStyle, ListHeaderComponentStyle);
699690
} else {
700691
child = renderHeader();
701-
style = defaultFullItemStyle;
692+
style = fullItemStyle;
702693
}
703694
// falls through
704695
case BigListItemType.FOOTER:
705696
if (type === BigListItemType.FOOTER) {
706697
if (ListFooterComponent != null) {
707698
child = createElement(ListFooterComponent);
708-
style = mergeViewStyle(
709-
defaultFullItemStyle,
710-
ListFooterComponentStyle,
711-
);
699+
style = mergeViewStyle(fullItemStyle, ListFooterComponentStyle);
712700
} else {
713701
child = renderFooter();
714-
style = defaultFullItemStyle;
702+
style = fullItemStyle;
715703
}
716704
}
717705
// falls through
718706
case BigListItemType.SECTION_FOOTER:
719707
if (type === BigListItemType.SECTION_FOOTER) {
720708
height = isEmptyList ? 0 : height; // Hide section footer on empty
721709
child = renderSectionFooter(section);
722-
style = defaultFullItemStyle;
710+
style = fullItemStyle;
723711
}
724712
// falls through
725713
case BigListItemType.ITEM:
@@ -730,8 +718,8 @@ class BigList extends PureComponent {
730718
: uniqueKey;
731719
style =
732720
numColumns > 1
733-
? mergeViewStyle(defaultItemStyle, columnWrapperStyle || {})
734-
: defaultItemStyle;
721+
? mergeViewStyle(itemStyle, columnWrapperStyle || {})
722+
: itemStyle;
735723
if (this.hasSections()) {
736724
child = renderItem({ item, section, index });
737725
} else {
@@ -777,7 +765,7 @@ class BigList extends PureComponent {
777765
children.push(
778766
<BigListSection
779767
key={itemKey}
780-
style={defaultFullItemStyle}
768+
style={fullItemStyle}
781769
height={height}
782770
position={position}
783771
nextSectionPosition={sectionPositions[0]}
@@ -832,6 +820,26 @@ class BigList extends PureComponent {
832820
}
833821
}
834822

823+
/**
824+
* Get base style.
825+
* @return {{transform: [{scaleX: number}]}|{transform: [{scaleY: number}]}}
826+
*/
827+
getBaseStyle() {
828+
const { inverted, horizontal } = this.props;
829+
if (inverted) {
830+
if (horizontal) {
831+
return {
832+
transform: [{ scaleX: -1 }],
833+
};
834+
} else {
835+
return {
836+
transform: [{ scaleY: -1 }],
837+
};
838+
}
839+
}
840+
return {};
841+
}
842+
835843
/**
836844
* Render.
837845
* @returns {JSX.Element}
@@ -932,16 +940,17 @@ class BigList extends PureComponent {
932940
const scrollView = wrapper(
933941
<ScrollView {...scrollViewProps}>{this.renderItems()}</ScrollView>,
934942
);
943+
944+
const scrollStyle = mergeViewStyle(
945+
{
946+
flex: 1,
947+
maxHeight: Platform.select({ web: "100vh", default: "100%" }),
948+
},
949+
this.getBaseStyle(),
950+
);
951+
935952
return (
936-
<View
937-
style={[
938-
{
939-
flex: 1,
940-
maxHeight: Platform.select({ web: "100vh", default: "100%" }),
941-
},
942-
inverted ? { transform: [{ scaleY: -1 }] } : {},
943-
]}
944-
>
953+
<View style={scrollStyle}>
945954
{scrollView}
946955
{renderAccessory != null ? renderAccessory(this) : null}
947956
</View>
@@ -951,6 +960,7 @@ class BigList extends PureComponent {
951960

952961
BigList.propTypes = {
953962
inverted: PropTypes.bool,
963+
horizontal: PropTypes.bool,
954964
actionSheetScrollRef: PropTypes.any,
955965
batchSizeThreshold: PropTypes.number,
956966
bottom: PropTypes.number,
@@ -1052,6 +1062,7 @@ BigList.defaultProps = {
10521062
// Data
10531063
data: [],
10541064
inverted: false,
1065+
horizontal: false,
10551066
sections: null,
10561067
refreshing: false,
10571068
batchSizeThreshold: 1,

lib/index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111

1212
interface BigListProps<ItemT> extends ScrollViewProps {
1313
inverted: bool | null | undefined;
14+
horizontal: bool | null | undefined;
1415
actionSheetScrollRef?: any | null | undefined;
1516
batchSizeThreshold?: number | null | undefined;
1617
contentInset?: {

0 commit comments

Comments
 (0)