import * as React from 'react'; import { StyleSheet, View } from 'react-native'; import { useColors, useDelayExecTask } from '../../hook'; import { usePaletteContext } from '../../theme'; import { SingleLineText } from '../../ui/Text'; import { g_index_alphabet_size, g_thumb_alphabet_size, } from './ListIndex.const'; import type { ListIndexProps } from './types'; /** * List Index Component. * * This component is mainly used with alphabetical lists. */ export const ListIndex = (props: ListIndexProps) => { const { indexTitles, onIndexSelected, indexContainerStyle, fontContainerStyle, isVisibleLetter = false, } = props; const ref = React.useRef>(null); const offsetRef = React.useRef(0); const maxIndex = indexTitles.length - 1; const [currentIndex, setCurrentIndex] = React.useState(); const [currentTitle, setCurrentTitle] = React.useState(); const _onIndexSelected = (index: number) => { let _index = index; if (index < 0) { _index = 0; } else if (index > maxIndex) { _index = maxIndex; } setCurrentIndex(_index); setCurrentTitle(indexTitles[_index]?.[0]); onIndexSelected?.(_index); }; const { delayExecTask } = useDelayExecTask(500, () => { setCurrentIndex(undefined); setCurrentTitle(undefined); }); const { colors } = usePaletteContext(); const { getColor } = useColors({ fg2: { light: colors.neutral[5], dark: colors.neutral[6], }, }); return ( <> { // offsetRef.current = e.nativeEvent.layout.y; ref.current?.measure( ( _x: number, _y: number, _width: number, _height: number, _pageX: number, pageY: number ) => { offsetRef.current = pageY; } ); }} onTouchMove={(e) => { const y = e.nativeEvent.pageY; const index = Math.floor( (y - offsetRef.current) / g_index_alphabet_size ); _onIndexSelected(index); }} onMoveShouldSetResponder={() => { return true; }} onTouchEnd={(e) => { const y = e.nativeEvent.pageY; const index = Math.floor( (y - offsetRef.current) / g_index_alphabet_size ); _onIndexSelected(index); delayExecTask(); }} > {indexTitles.map((section, index: number) => ( {section[0]} ))} {currentTitle && isVisibleLetter === true ? ( {currentTitle} ) : null} ); };