Skip to content

Commit 524f6cd

Browse files
chore: handle scroll behavior
1 parent 2b0a65a commit 524f6cd

2 files changed

Lines changed: 22 additions & 114 deletions

File tree

src/components/hv-section-list/index.tsx

Lines changed: 22 additions & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -13,50 +13,14 @@ import {
1313
RefreshControl as DefaultRefreshControl,
1414
Platform,
1515
} from 'react-native';
16-
import { LOCAL_NAME, NODE_TYPE } from 'hyperview/src/types';
1716
import React, { PureComponent } from 'react';
1817
import type { ScrollParams, State } from './types';
1918
import { createTestProps, getAncestorByTagName } from 'hyperview/src/services';
2019
import { DOMParser } from '@instawork/xmldom';
2120
import type { ElementRef } from 'react';
2221
import { FlatList } from 'hyperview/src/core/components/scroll';
2322
import HvElement from 'hyperview/src/core/components/hv-element';
24-
25-
const getSectionIndex = (
26-
sectionTitle: Element,
27-
sectionTitles: HTMLCollectionOf<Element>,
28-
): number => {
29-
const sectionIndex = Array.from(sectionTitles).indexOf(sectionTitle);
30-
31-
// If first section did not have an explicit title, we still need to account for it
32-
const previousElement = Dom.getPreviousNodeOfType(
33-
sectionTitles[0],
34-
NODE_TYPE.ELEMENT_NODE,
35-
);
36-
if ((previousElement as Element)?.localName === LOCAL_NAME.ITEM) {
37-
return sectionIndex + 1;
38-
}
39-
40-
return sectionIndex;
41-
};
42-
43-
const getPreviousSectionTitle = (
44-
element: Element,
45-
itemIndex: number,
46-
): [Element | null, number] => {
47-
const { previousSibling } = element;
48-
if (!previousSibling) {
49-
return [null, itemIndex];
50-
}
51-
if ((previousSibling as Element).localName === LOCAL_NAME.SECTION_TITLE) {
52-
return [previousSibling as Element, itemIndex];
53-
}
54-
if ((previousSibling as Element).localName === LOCAL_NAME.ITEM) {
55-
// eslint-disable-next-line no-param-reassign
56-
itemIndex += 1;
57-
}
58-
return getPreviousSectionTitle(previousSibling as Element, itemIndex);
59-
};
23+
import { LOCAL_NAME } from 'hyperview/src/types';
6024

6125
export default class HvSectionList extends PureComponent<
6226
HvComponentProps,
@@ -154,85 +118,30 @@ export default class HvSectionList extends PureComponent<
154118
return;
155119
}
156120

157-
// find index of target in section-list
158-
// first, check legacy section-list format, where items are nested under a <section>
159-
const targetElementParentSection = getAncestorByTagName(
160-
targetElement,
161-
LOCAL_NAME.SECTION,
121+
// eslint-disable-next-line max-len
122+
// No parent section? Check new section-list format, where items are nested under the section-list
123+
const items = this.props.element.getElementsByTagNameNS(
124+
Namespaces.HYPERVIEW,
125+
LOCAL_NAME.ITEM,
162126
);
163-
if (targetElementParentSection) {
164-
const sections = this.props.element.getElementsByTagNameNS(
165-
Namespaces.HYPERVIEW,
166-
LOCAL_NAME.SECTION,
167-
);
168-
const sectionIndex = Array.from(sections).indexOf(
169-
targetElementParentSection,
170-
);
171-
if (sectionIndex === -1) {
172-
return;
173-
}
174-
const itemsInSection = Array.from(
175-
targetElementParentSection.getElementsByTagNameNS(
176-
Namespaces.HYPERVIEW,
177-
LOCAL_NAME.ITEM,
178-
),
179-
);
180-
const itemIndex = itemsInSection.indexOf(targetListItem);
181-
if (itemIndex === -1) {
182-
return;
183-
}
184-
185-
const params: ScrollParams = {
186-
animated,
187-
index: itemIndex + 1,
188-
sectionIndex,
189-
};
190-
if (typeof viewOffset === 'number') {
191-
params.viewOffset = viewOffset;
192-
}
193-
194-
if (typeof viewPosition === 'number') {
195-
params.viewPosition = viewPosition;
196-
}
127+
const itemIndex = Array.from(items).indexOf(targetListItem);
128+
if (itemIndex === -1) {
129+
return;
130+
}
197131

198-
this.ref?.scrollToIndex(params);
199-
} else {
200-
// eslint-disable-next-line max-len
201-
// No parent section? Check new section-list format, where items are nested under the section-list
202-
const items = this.props.element.getElementsByTagNameNS(
203-
Namespaces.HYPERVIEW,
204-
LOCAL_NAME.ITEM,
205-
);
206-
if (Array.from(items).indexOf(targetListItem) === -1) {
207-
return;
208-
}
209-
const [sectionTitle, itemIndex] = getPreviousSectionTitle(
210-
targetListItem,
211-
1, // 1 instead of 0 as it appears itemIndex is 1-based
212-
);
213-
const sectionTitles = this.props.element.getElementsByTagNameNS(
214-
Namespaces.HYPERVIEW,
215-
LOCAL_NAME.SECTION_TITLE,
216-
);
217-
const sectionIndex = sectionTitle
218-
? getSectionIndex(sectionTitle, sectionTitles)
219-
: 0;
220-
if (sectionIndex === -1) {
221-
return;
222-
}
223-
const params: ScrollParams = {
224-
animated,
225-
index: itemIndex,
226-
sectionIndex,
227-
};
228-
if (viewOffset) {
229-
params.viewOffset = viewOffset;
230-
}
231-
if (viewPosition) {
232-
params.viewPosition = viewPosition;
233-
}
234-
this.ref?.scrollToIndex(params);
132+
const params: ScrollParams = {
133+
animated,
134+
index: itemIndex,
135+
};
136+
if (viewOffset) {
137+
params.viewOffset = viewOffset;
235138
}
139+
if (viewPosition) {
140+
params.viewPosition = viewPosition;
141+
}
142+
console.log('>>> scrollToIndex', params);
143+
144+
this.ref?.scrollToIndex(params);
236145
};
237146

238147
getStickySectionHeadersEnabled = (): boolean => {

src/components/hv-section-list/types.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ export type State = {
66
export type ScrollParams = {
77
animated?: boolean | undefined;
88
index: number;
9-
sectionIndex: number;
109
viewOffset?: number;
1110
viewPosition?: number;
1211
};

0 commit comments

Comments
 (0)