import React, { useMemo, useState } from 'react'; import { runInAction } from 'mobx'; import { FiltersMap, KeyedItem } from '@wix/bex-core'; import { observer } from 'mobx-react-lite'; import { CollectionTable, CollectionTableBaseProps } from '../CollectionTable'; import { useTableSectionRow } from './useTableSectionRow'; import { TableState } from '../../state'; import { GroupBy, RenderSection, SectionEvents, } from '../CollectionSectionHeader'; import { TableSectionsState } from './TableSectionsState'; export interface TableSectionsProps extends CollectionTableBaseProps { groupBy: GroupBy; renderSection: RenderSection; table: TableState; collapsible?: boolean; events?: SectionEvents; } const _TableSections = ({ groupBy, renderSection, table, columns, rowDetails, collapsible, events, ...collectionTableProps }: TableSectionsProps) => { const [state] = useState( () => new TableSectionsState(table, groupBy, renderSection, collapsible), ); runInAction(() => { state.renderSection = renderSection; }); state.onToggleCallback = events?.onToggle; const columnsWithSections = useMemo( () => columns.map((column) => ({ ...column, render: (item: T, rowNum: number) => { const keyedItem = state.sectionsKeyedItems[rowNum]; if (keyedItem && state.isSectionRow(keyedItem)) { return null; } return column.render(item, rowNum); }, })), [columns], ); const wrappedRowDetails = useMemo(() => { if (!rowDetails) { return undefined; } return (rowData: KeyedItem, rowNum: number) => { if (state.isSectionRow(rowData)) { return null; } return rowDetails(rowData, rowNum); }; }, [rowDetails, state]); const TableSectionsTrElement = useTableSectionRow(state); return ( { return state.isSectionRow(keyedItem); }} isUsingExpandCollapse={collapsible} /> ); }; export const TableSections = observer(_TableSections); export type TableSectionsProp = { TableSections: typeof TableSections; groupBy: GroupBy; renderSection: RenderSection; collapsible?: boolean; events?: SectionEvents; };