forked from Shopify/flash-list
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathContacts.tsx
58 lines (52 loc) · 1.68 KB
/
Contacts.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import React, { useContext, useEffect, useState } from "react";
import { FlashList } from "@shopify/flash-list";
import { DebugContext } from "../Debug";
import Contact from "./models/Contact";
import contacts from "./data/contacts";
import ContactCell from "./ContactCell";
import ContactSectionHeader from "./ContactSectionHeader";
import ContactHeader from "./ContactHeader";
import ContactDivider from "./ContactDivider";
const Contacts = () => {
const debugContext = useContext(DebugContext);
const [data, setData] = useState<(Contact | string)[]>([]);
useEffect(() => {
const contactsWithTitles = Array.from(contacts.keys())
.sort((aKey, bKey) => aKey.localeCompare(bKey))
.flatMap((key) => {
return [key, ...(contacts.get(key) ?? [])];
});
setData(contactsWithTitles);
}, []);
const stickyHeaderIndices = data
.map((item, index) => {
if (typeof item === "string") {
return index;
} else {
return null;
}
})
.filter((item) => item !== null) as number[];
return (
<FlashList
testID="FlashList"
estimatedItemSize={44}
data={data}
renderItem={({ item }) => {
if (typeof item === "string") {
return <ContactSectionHeader title={item} />;
} else {
return <ContactCell contact={item as Contact} />;
}
}}
getItemType={(item) => {
return typeof item === "string" ? "sectionHeader" : "row";
}}
ItemSeparatorComponent={ContactDivider}
stickyHeaderIndices={stickyHeaderIndices}
ListHeaderComponent={ContactHeader}
initialScrollIndex={debugContext.initialScrollIndex}
/>
);
};
export default Contacts;