-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
52 lines (49 loc) · 1.56 KB
/
index.js
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
/**
* @callback getItemHeight
* @callback getSeparatorHeight
* @callback getSectionHeaderHeight
* @callback getSectionFooterHeight
* @param {number} [listFooterHeight]
* @param {number} [listFooterHeight]
* @returns {object} - { index, offset, length }
*/
const getSectionItemLayout = ({
getItemHeight = () => 0,
getSeparatorHeight = () => 0,
getSectionHeaderHeight = () => 0,
getSectionFooterHeight = () => 0,
listHeaderHeight = 0,
listFooterHeight = 0,
}) => (sections, index) => {
let length = listHeaderHeight, offset = 0, currentIndex = 0;
while (currentIndex < index) {
offset += length;
if (currentIndex > 0) length = listFooterHeight;
currentIndex++;
const sectionsLength = sections.length;
for (let sectionIndex = 0; ((sectionIndex < sectionsLength) && (currentIndex < index)); sectionIndex++) {
offset += length;
length = getSectionHeaderHeight(sectionIndex);
currentIndex++;
const sectionData = sections[sectionIndex].data;
const dataLength = sectionData.length;
for (let dataIndex = 0; ((dataIndex < dataLength) && (currentIndex < index)); dataIndex++) {
offset += length;
const separator_height = dataIndex < dataLength - 1 ? getSeparatorHeight(sectionIndex, dataIndex) : 0;
length = getItemHeight(sectionData[dataIndex], sectionIndex, dataIndex) + separator_height;
currentIndex++;
}
if (!dataLength && (currentIndex < index)) {
offset += length;
length = getSectionFooterHeight(sectionIndex);
currentIndex++;
}
}
}
return {
index,
length,
offset
};
}
export default getSectionItemLayout;