Skip to content

Commit 2b0a65a

Browse files
chore: replace section-list with flatlist
1 parent cd1f17d commit 2b0a65a

2 files changed

Lines changed: 29 additions & 48 deletions

File tree

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

Lines changed: 28 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ import type { ScrollParams, State } from './types';
1919
import { createTestProps, getAncestorByTagName } from 'hyperview/src/services';
2020
import { DOMParser } from '@instawork/xmldom';
2121
import type { ElementRef } from 'react';
22+
import { FlatList } from 'hyperview/src/core/components/scroll';
2223
import HvElement from 'hyperview/src/core/components/hv-element';
23-
import { SectionList } from 'hyperview/src/core/components/scroll';
2424

2525
const getSectionIndex = (
2626
sectionTitle: Element,
@@ -72,13 +72,13 @@ export default class HvSectionList extends PureComponent<
7272

7373
parser: DOMParser = new DOMParser();
7474

75-
ref: ElementRef<typeof SectionList> | null = null;
75+
ref: ElementRef<typeof FlatList> | null = null;
7676

7777
state: State = {
7878
refreshing: false,
7979
};
8080

81-
onRef = (ref: ElementRef<typeof SectionList> | null) => {
81+
onRef = (ref: ElementRef<typeof FlatList> | null) => {
8282
this.ref = ref;
8383
};
8484

@@ -184,7 +184,7 @@ export default class HvSectionList extends PureComponent<
184184

185185
const params: ScrollParams = {
186186
animated,
187-
itemIndex: itemIndex + 1,
187+
index: itemIndex + 1,
188188
sectionIndex,
189189
};
190190
if (typeof viewOffset === 'number') {
@@ -195,7 +195,7 @@ export default class HvSectionList extends PureComponent<
195195
params.viewPosition = viewPosition;
196196
}
197197

198-
this.ref?.scrollToLocation(params);
198+
this.ref?.scrollToIndex(params);
199199
} else {
200200
// eslint-disable-next-line max-len
201201
// No parent section? Check new section-list format, where items are nested under the section-list
@@ -222,7 +222,7 @@ export default class HvSectionList extends PureComponent<
222222
}
223223
const params: ScrollParams = {
224224
animated,
225-
itemIndex,
225+
index: itemIndex,
226226
sectionIndex,
227227
};
228228
if (viewOffset) {
@@ -231,7 +231,7 @@ export default class HvSectionList extends PureComponent<
231231
if (viewPosition) {
232232
params.viewPosition = viewPosition;
233233
}
234-
this.ref?.scrollToLocation(params);
234+
this.ref?.scrollToIndex(params);
235235
}
236236
};
237237

@@ -280,6 +280,18 @@ export default class HvSectionList extends PureComponent<
280280
});
281281
};
282282

283+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
284+
renderListItem = (item: any) => {
285+
return (
286+
<HvElement
287+
element={item.item as Element}
288+
onUpdate={this.onUpdate}
289+
options={this.props.options}
290+
stylesheets={this.props.stylesheets}
291+
/>
292+
);
293+
};
294+
283295
render() {
284296
const styleAttr = this.props.element.getAttribute('style');
285297
const style = styleAttr
@@ -309,35 +321,21 @@ export default class HvSectionList extends PureComponent<
309321

310322
addNodes(this.props.element);
311323

312-
let items = [];
313-
let titleElement = null;
314-
const sections: { data: Element[]; title: Element | null }[] = [];
324+
const data: Element[] = [];
325+
const headerIndeces: number[] = [];
315326

316327
for (let j = 0; j < flattened.length; j += 1) {
317328
const sectionElement = flattened[j];
318329
if (sectionElement) {
319330
if (sectionElement.nodeName === LOCAL_NAME.ITEM) {
320-
items.push(sectionElement);
331+
data.push(sectionElement);
321332
} else if (sectionElement.nodeName === LOCAL_NAME.SECTION_TITLE) {
322-
if (items.length > 0) {
323-
sections.push({
324-
data: items,
325-
title: titleElement,
326-
});
327-
items = [];
328-
}
329-
titleElement = sectionElement;
333+
headerIndeces.push(j);
334+
data.push(sectionElement);
330335
}
331336
}
332337
}
333338

334-
if (items.length > 0) {
335-
sections.push({
336-
data: items,
337-
title: titleElement,
338-
});
339-
}
340-
341339
// Fix scrollbar rendering issue in iOS 13+
342340
// https://github.com/facebook/react-native/issues/26610#issuecomment-539843444
343341
const scrollIndicatorInsets =
@@ -354,9 +352,10 @@ export default class HvSectionList extends PureComponent<
354352
const hasRefreshTrigger =
355353
this.props.element.getAttribute('trigger') === 'refresh';
356354
return (
357-
<SectionList
355+
<FlatList
358356
ref={this.onRef}
359357
accessibilityLabel={accessibilityLabel}
358+
data={data}
360359
element={this.props.element}
361360
keyboardDismissMode={Keyboard.getKeyboardDismissMode(
362361
this.props.element,
@@ -375,27 +374,9 @@ export default class HvSectionList extends PureComponent<
375374
) : undefined
376375
}
377376
removeClippedSubviews={false}
378-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
379-
renderItem={({ item }: any): any => (
380-
<HvElement
381-
element={item as Element}
382-
onUpdate={this.onUpdate}
383-
options={this.props.options}
384-
stylesheets={this.props.stylesheets}
385-
/>
386-
)}
387-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
388-
renderSectionHeader={({ section: { title } }: any): any => (
389-
<HvElement
390-
element={title as Element}
391-
onUpdate={this.onUpdate}
392-
options={this.props.options}
393-
stylesheets={this.props.stylesheets}
394-
/>
395-
)}
377+
renderItem={this.renderListItem}
396378
scrollIndicatorInsets={scrollIndicatorInsets}
397-
sections={sections}
398-
stickySectionHeadersEnabled={this.getStickySectionHeadersEnabled()}
379+
stickyHeaderIndices={headerIndeces}
399380
style={style}
400381
testID={testID}
401382
/>

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export type State = {
55
// https://reactnative.dev/docs/sectionlist#scrolltolocation
66
export type ScrollParams = {
77
animated?: boolean | undefined;
8-
itemIndex: number;
8+
index: number;
99
sectionIndex: number;
1010
viewOffset?: number;
1111
viewPosition?: number;

0 commit comments

Comments
 (0)