import { Box, Table as WSRTable, TableColumn as WSRTableColumn, TableProps, } from '@wix/design-system'; import React, { ComponentType, ForwardRefExoticComponent, ReactElement, useCallback, } from 'react'; import { observer } from 'mobx-react-lite'; import { FiltersMap, KeyedItem } from '@wix/bex-core'; import { cairoCtaClicked } from '@wix/bex-core/bi'; import { CollectionProvider } from '../CollectionContext'; import { TableState, ToolbarCollectionState } from '../../state'; import { ToolbarCollectionBaseProps } from '../ToolbarCollection'; import { CollectionTableContentBaseProps } from './CollectionTableContent'; import { classes, st } from '../common.st.css.js'; import { classes as wsrTableClasses, st as wsrTableSt, } from './CollectionTableWSRTable.st.css.js'; import { CollectionToolbars } from '../CollectionToolbars/CollectionToolbars'; import { BorderOnlyBox } from '../BorderOnlyBox'; import { usePageContext, useWixPatternsContainer } from '@wix/bex-core/react'; import { useIsMobile } from '../../hooks/useIsMobile'; import { CollectionToolbarsVisualizationProps } from '../CollectionToolbars/CollectionToolbarsVisualization'; import { buildMobileColumns } from './buildMobileColumns'; import { TableColumn } from '../../model'; export interface CollectionTableWSRTableBaseProps extends ToolbarCollectionBaseProps, CollectionTableContentBaseProps, Omit< TableProps>, | 'columns' | 'data' | 'rowDataHook' | 'onRowClick' | 'onDragEnd' | 'onDragStart' | 'onDragCancel' | 'isDragAndDropDisabled' | 'hideBulkSelectionCheckbox' | 'skin' > { /** * @deprecated The table skin is fixed to `"neutral"` and any value passed here is ignored. This prop will be removed in a future release. */ skin?: TableProps>['skin']; useShowLoaderWhenEmpty?: boolean; useNewInfiniteScrollLoader?: boolean; /** * Whether to add a sticky column.
* Sticky columns allow you to display specific columns at all times while the visitor scrolls the table horizontally. * @external */ stickySelectionColumn?: boolean; /** * Limits the amount of rows a visitor can select. * @external */ maxSelection?: number; /** * Hides bulk selection checkbox in the table titlebar, If the [`MultiBulkActionToolbar`](./?path=/story/features-actions-bulk-actions--multibulkactiontoolbar) feature is enabled, this flag will have no effect. * @external */ hideBulkSelectionCheckbox?: boolean; BodyElementType?: ComponentType<{ children: ReactElement[] }>; TrElementType?: ForwardRefExoticComponent<{ children: ReactElement[]; rowData: any; className: string; }>; visualization?: CollectionToolbarsVisualizationProps['visualization']; } export interface CollectionTableWSRTableProps extends CollectionTableWSRTableBaseProps { state: { tableState: TableState; toolbar: ToolbarCollectionState; keyedItems: KeyedItem[]; showLoadingState: boolean; showEmptyState: boolean; }; sticky: boolean; columns: WSRTableColumn>[]; onRowClick?: (item: KeyedItem, index: number) => void; keyedItems?: KeyedItem[]; } function _CollectionTableWSRTable( props: CollectionTableWSRTableProps, ) { const { state, filters, title, tabs, customColumns: customColumnsProp, multiLevelSorting: multiLevelSortingProp, views, extraToolbar, summaryBar, topNotification, selectionToolbar, dragAndDropReorderModeToolbar, showSelection: showSelectionProp, exportButton, importButton, tags, sticky, onRowClick, search, children, tableGridSwitchButton, useShowLoaderWhenEmpty, dragAndDrop, showTitleBar, horizontalScroll, stickyColumns, stickySelectionColumn, hideBulkSelectionCheckbox, columns, primaryActionButton, secondaryActions, onSelectionChanged, moreActions, visualization, resizable, } = props; const isMobile = useIsMobile(); const { toolbar, showEmptyState, tableState: table, keyedItems: stateKeyedItems, } = state; const { isPanelLayout } = useWixPatternsContainer(); const { bulk, initTask } = toolbar; const keyedItems = props.keyedItems ?? stateKeyedItems; const { collection } = table; const { showRefreshLoader, showErrorState } = collection; const { stackingRootRef } = usePageContext(); const handleRowCLick = useCallback( (item: KeyedItem, index: number) => { state.toolbar.reportBi( cairoCtaClicked({ ctaName: 'row_area', ctaIndex: index, itemId: item.id, itemIndex: item.index, url: typeof window !== 'undefined' ? window.location.href : '', }), ); onRowClick?.(item, index); }, [onRowClick], ); const Table = WSRTable as ComponentType>>; const showSelection = showSelectionProp ?? selectionToolbar != null; const hideTitleBar = isMobile || showEmptyState || toolbar.showInitialLoader || initTask.status.isError; const showBorderBox = !isPanelLayout && (!table.showLoadingState || !showTitleBar) && (hideTitleBar || !showTitleBar); const actionCellColumn = columns.find( (column) => column.key === 'cairo-action-cell', ); const columnsWithoutActionCell = columns.filter( (column) => column.key !== 'cairo-action-cell', ); const tableColumns = isMobile ? [ ...buildMobileColumns( columnsWithoutActionCell as TableColumn>[], ), ...(actionCellColumn ? [actionCellColumn] : []), ] : columns; const keyedItemsWithTooltip = keyedItems.map((item) => { const isDisabled = bulk.isRowCheckboxDisabled(item); if (isDisabled) { const tooltipContent = bulk.getCheckboxTooltipContent(item); if (tooltipContent) { return { ...item, checkboxTooltipContent: tooltipContent, }; } } return item; }); return (
to render the loader even when `data.length=0` showHeaderWhenEmpty={ useShowLoaderWhenEmpty && !showRefreshLoader && !showErrorState && !showEmptyState } hideBulkSelectionCheckbox={ selectionToolbar ? false : hideBulkSelectionCheckbox } bulkSelectionCheckboxRef={bulk.bulkSelectionCheckboxRef} dragAndDrop={dragAndDrop} rowDataHook={(rowData) => `table-row-${rowData.key}`} controlled horizontalScroll={horizontalScroll} stickyColumns={table.getStickyColumnsCount({ horizontalScroll, stickyColumns, showSelection, stickySelectionColumn, })} showSelection={showSelection} selectedIds={bulk.selectedIds} onSelectionStarted={state.toolbar.onSelectionStarted} onSelectionChanged={ onSelectionChanged ?? ((_selectedIds, _change) => { state.toolbar.onSelectionChanged(_selectedIds, _change); }) } > topNotification({ query: collection.result.originQuery }) : topNotification } titleBar={ <> {showBorderBox && } {!hideTitleBar && (
{ table.headerElement = el; }} style={{ display: showTitleBar ? undefined : 'none' }} >
)} } tableGridSwitchButton={tableGridSwitchButton} exportButton={exportButton} importButton={importButton} tags={tags} primaryActionButton={primaryActionButton} secondaryActions={secondaryActions} moreActions={moreActions} visualization={visualization} /> {children}
); } export const CollectionTableWSRTable = observer(_CollectionTableWSRTable);