import React, { useEffect, useRef } from 'react'; import cn from 'classnames'; import './ScrollableContainer.scss'; import LoaderComponent from '../../UI/Placeholders/LoaderComponent/LoaderComponent'; interface FlatListProps { className?: string; rootStyles?: React.CSSProperties; mainContainerStyles?: React.CSSProperties; data: Array; ListEmptyComponent?: React.ComponentClass | React.FunctionComponent; onEndReached?: VoidFunction; onEndReachedThreshold?: number; refreshing?: boolean; autoScrollToBottom?: boolean; renderItem: (item: T, index: number) => React.ReactElement | null; } export default function ScrollableContainer(props: FlatListProps) { const { className, rootStyles, mainContainerStyles, data, ListEmptyComponent, onEndReached, onEndReachedThreshold, refreshing, autoScrollToBottom, renderItem, } = props; const container = useRef(null); const scrollHandler = () => { console.log('call scrollHandler'); if (container.current) { const { scrollHeight, clientHeight, scrollTop } = container.current; const scrollOffset = (scrollTop + clientHeight) / scrollHeight; let endReached = false; if (typeof onEndReachedThreshold === 'number') { if (scrollOffset >= onEndReachedThreshold) { endReached = true; } } else if (scrollOffset >= 0.99) { endReached = true; } if (endReached && onEndReached) { onEndReached(); } } }; const messageEndRef = useRef(null); const scrollToBottom = () => { console.log('call scrollToBottom start'); if (autoScrollToBottom) messageEndRef.current?.scrollIntoView({ behavior: 'smooth' }); }; useEffect(() => { console.log('useEffect []'); if (autoScrollToBottom) scrollToBottom(); }, []); useEffect(() => { console.log('useEffect messages'); if (autoScrollToBottom) scrollToBottom(); }, [data.length]); console.log('çount items to scroll:', data.length); console.log('data to scrolling: ', JSON.stringify(data)); return (
{refreshing && (
)} {data && data.length ? (
{data.map((it, index) => renderItem(it, index))}
) : ( !refreshing && ListEmptyComponent && )}
); }