Skip to content

Commit c720051

Browse files
authored
Add sticky header capability to section list (#712)
1 parent 8ed987f commit c720051

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

packages/core/src/components/SectionList/SectionList.tsx

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ type ListComponentType = "FlatList" | "FlashList";
77

88
interface AdditionalSectionListProps<T> {
99
sectionKey: string;
10+
stickyHeader?: boolean;
1011
renderItem: (itemInfo: {
1112
item?: T;
1213
index: number;
@@ -37,6 +38,7 @@ export const DEFAULT_SECTION = "Uncategorized";
3738

3839
const SectionList = <T extends { [key: string]: any }>({
3940
sectionKey,
41+
stickyHeader = false,
4042
listComponent = "FlatList",
4143
data: dataProp,
4244
renderItem: renderItemProp,
@@ -68,6 +70,16 @@ const SectionList = <T extends { [key: string]: any }>({
6870
return result;
6971
}, [data, sectionKey]);
7072

73+
const sectionHeaderIndicies = React.useMemo(
74+
() =>
75+
stickyHeader
76+
? dataWithSections
77+
.filter((item) => item.type === "SECTION_ITEM")
78+
.map((item) => dataWithSections.indexOf(item))
79+
: undefined,
80+
[dataWithSections, stickyHeader]
81+
);
82+
7183
const extractSectionHeader = (
7284
element: JSX.Element | null
7385
): JSX.Element | null => {
@@ -152,6 +164,7 @@ const SectionList = <T extends { [key: string]: any }>({
152164
case "FlatList":
153165
return (
154166
<FlatList
167+
stickyHeaderIndices={sectionHeaderIndicies}
155168
{...(rest as FlatListProps<SectionListItem<T>>)}
156169
data={dataWithSections}
157170
renderItem={renderItem}
@@ -160,6 +173,7 @@ const SectionList = <T extends { [key: string]: any }>({
160173
case "FlashList":
161174
return (
162175
<FlashList
176+
stickyHeaderIndices={sectionHeaderIndicies}
163177
{...(rest as FlashListProps<SectionListItem<T>>)}
164178
data={dataWithSections}
165179
renderItem={renderItem}

0 commit comments

Comments
 (0)