import React, { memo, useRef } from 'react'; import isEqual from 'react-fast-compare'; import type { SectionListData } from 'react-native'; import { SectionList } from 'react-native'; import { ERRORS, isSectionSelected, logError } from '../../helpers'; import { getSectionLocation } from '../../helpers/get-section-location'; import type { OptionType, SectionOptionType } from '../../types'; import { NoOptions } from '../no-options'; import { SectionHeader } from '../section-header'; import type { SectionOptionsListProps } from './section-options-list.types'; export const SectionOptionsList = memo( ({ resolvedData, getItemLayout, renderItem, accessibilityState, selectedOption, scrollToSelectedOption, sectionListProps, disabled, }: SectionOptionsListProps) => { const sectionOptionsListRef = useRef(null); const renderSectionHeader = (info: { section: SectionListData }) => { const isSelected = isSectionSelected({ title: info.section.title, selectedOptions: selectedOption as OptionType[], sectionData: resolvedData as unknown as SectionOptionType[], }); return ; }; const scrollToIndex = () => { if (sectionOptionsListRef.current) { try { const location = getSectionLocation({ data: resolvedData, selectedOption, scrollToSelectedOption, }); if (location.sectionIndex >= 0 && location.itemIndex >= 0) { sectionOptionsListRef.current.scrollToLocation({ ...location, animated: false, }); } } catch { logError(ERRORS.SCROLL_TO_LOCATION); } } }; return ( } scrollEnabled={!disabled} {...sectionListProps} ref={sectionOptionsListRef} renderSectionHeader={renderSectionHeader} sections={resolvedData} getItemLayout={getItemLayout} renderItem={renderItem} keyExtractor={({ value }) => value} onLayout={scrollToIndex} /> ); }, isEqual, ); SectionOptionsList.displayName = 'SectionOptionsList';