import React, { useEffect, useState } from 'react'; import { observer } from 'mobx-react-lite'; import { CollectionState, FiltersMap } from '@wix/bex-core'; import { CollectionTableBaseProps } from '../CollectionTable'; import { runInAction } from 'mobx'; import { VirtualTableRepeater, VirtualTableRepeaterProps, } from '../VirtualGridRepeater/VirtualTableRepeater'; import { TableBase } from './TableBase'; import { VirtualGridStateProvider } from '../VirtualGridRepeater/VirtualGridStateContext'; import { TableState } from '../../state'; import { useWixPatternsContainer } from '@wix/bex-core/react'; import { getVirtualTableProps } from './getVirtualTableProps'; import type { TableSectionsProp } from '../TableSections'; export interface TableVirtualProps extends CollectionTableBaseProps { rowHeight?: number | ((item: T, index: number) => number); estimatedRowHeight?: number; state: CollectionState | TableState; sections?: TableSectionsProp; } function _TableVirtual( props: TableVirtualProps, ) { const { rowHeight, estimatedRowHeight, horizontalScroll = true } = props; const container = useWixPatternsContainer(); const [state] = useState(() => props.state instanceof TableState ? props.state : new TableState({ collection: props.state, container, }), ); useEffect(() => { runInAction(() => { state.virtual.rowHeight = rowHeight; }); }, [rowHeight]); useEffect(() => { if (estimatedRowHeight == null) { return; } runInAction(() => { state.virtual.estimatedRowHeight = estimatedRowHeight; }); }, [estimatedRowHeight]); const [BodyElementType] = useState( () => ({ children }: VirtualTableRepeaterProps) => ( {children} ), ); return ( ); } export const TableVirtual = observer(_TableVirtual);