import React, { useCallback, useEffect, useRef } from "react"; import equal from "react-fast-compare"; import { CollectionSize, Entity, EntityTableController, FilterValues, ResolvedProperty, SelectedCellProps } from "../../types"; import { CellRendererParams, VirtualTable, VirtualTableColumn } from "../VirtualTable"; import { enumToObjectEntries } from "../../util"; import { OnCellValueChange, OnColumnResizeParams } from "../common"; import { FilterFormFieldProps } from "../VirtualTable/VirtualTableHeader"; import { ReferenceFilterField } from "./filters/ReferenceFilterField"; import { StringNumberFilterField } from "./filters/StringNumberFilterField"; import { BooleanFilterField } from "./filters/BooleanFilterField"; import { DateTimeFilterField } from "./filters/DateTimeFilterField"; import { useOutsideAlerter } from "@firecms/ui"; import { SelectableTableContext } from "./SelectableTableContext"; export type SelectableTableProps> = { /** * Callback when a cell value changes. */ onValueChange?: OnCellValueChange; columns: VirtualTableColumn[]; cellRenderer: React.ComponentType>>; /** * Builder for creating the buttons in each row * @param entity * @param size */ tableRowActionsBuilder?: (params: { entity: Entity, size: CollectionSize, width: number, frozen?: boolean }) => React.ReactNode; /** * Callback when anywhere on the table is clicked */ onEntityClick?(entity: Entity): void; /** * Callback when a column is resized */ onColumnResize?(params: OnColumnResizeParams): void; /** * Should apply a different style to a row when hovering */ hoverRow?: boolean; /** * Controller holding the logic for the table * {@link useDataSourceEntityCollectionTableController} * {@link EntityTableController} */ tableController: EntityTableController; filterable?: boolean; sortable?: boolean; inlineEditing?: boolean; forceFilter?: FilterValues; highlightedRow?: (data: Entity) => boolean; size?: CollectionSize; emptyComponent?: React.ReactNode; endAdornment?: React.ReactNode; AddColumnComponent?: React.ComponentType; } /** * This component is in charge of rendering a collection table with a high * degree of customization. * * This component is used internally by {@link EntityCollectionView} and * {@link useReferenceDialog} * * Please note that you only need to use this component if you are building * a custom view. If you just need to create a default view you can do it * exclusively with config options. * * If you want to bind a {@link EntityCollection} to a table with the default * options you see in collections in the top level navigation, you can * check {@link EntityCollectionView}. * * The data displayed in the table is managed by a {@link EntityTableController}. * You can build the default, bound to a path in the datasource, by using the hook * {@link useDataSourceEntityCollectionTableController} * * @see EntityCollectionTableProps * @see EntityCollectionView * @see VirtualTable * @group Components */ export const SelectableTable = React.memo>( function SelectableTable> ({ onValueChange, cellRenderer, onEntityClick, onColumnResize, hoverRow = true, size, inlineEditing = false, tableController: { data, dataLoading, noMoreToLoad, dataLoadingError, filterValues, setFilterValues, sortBy, setSortBy, setSearchString, clearFilter, itemCount, setItemCount, pageSize = 50, paginationEnabled, checkFilterCombination, setPopupCell }, filterable = true, emptyComponent, columns, forceFilter, highlightedRow, endAdornment, AddColumnComponent }: SelectableTableProps) { const ref = useRef(null); const [selectedCell, setSelectedCell] = React.useState | undefined>(undefined); const loadNextPage = () => { if (!paginationEnabled || dataLoading || noMoreToLoad) return; if (itemCount !== undefined) setItemCount?.(itemCount + pageSize); }; const resetPagination = useCallback(() => { setItemCount?.(pageSize); }, [pageSize]); const onRowClick = useCallback(({ rowData }: { rowData: Entity }) => { if (inlineEditing) return; return onEntityClick && onEntityClick(rowData); }, [onEntityClick, inlineEditing]); useOutsideAlerter(ref, () => { if (selectedCell) { unselect(); } }, Boolean(selectedCell)); // on ESC key press useEffect(() => { const escFunction = (event: any) => { if (event.keyCode === 27) { unselect(); } }; document.addEventListener("keydown", escFunction, false); return () => { document.removeEventListener("keydown", escFunction, false); }; }); const select = useCallback((cell?: SelectedCellProps) => { setSelectedCell(cell); }, []); const unselect = useCallback(() => { setSelectedCell(undefined); }, []); const onFilterUpdate = useCallback((updatedFilterValues?: FilterValues) => { setFilterValues?.({ ...updatedFilterValues, ...forceFilter } as FilterValues); }, [forceFilter]); return ( ) => void), select, onValueChange, size: size ?? "m", selectedCell, }} >
void)} hoverRow={hoverRow} checkFilterCombination={checkFilterCombination} createFilterField={filterable ? createFilterField : undefined} rowClassName={useCallback((entity: Entity) => { return highlightedRow?.(entity) ? "bg-gray-100 bg-opacity-75 dark:bg-gray-800 dark:bg-opacity-75" : ""; }, [highlightedRow])} className="flex-grow" emptyComponent={emptyComponent} endAdornment={endAdornment} AddColumnComponent={AddColumnComponent} />
); }, equal ); function createFilterField({ id, filterValue, setFilterValue, column, hidden, setHidden }: FilterFormFieldProps<{ resolvedProperty: ResolvedProperty, disabled: boolean, }>): React.ReactNode { if (!column.custom) { return null; } const { resolvedProperty } = column.custom; const isArray = resolvedProperty?.dataType === "array"; const baseProperty: ResolvedProperty = isArray ? resolvedProperty.of : resolvedProperty; if (!baseProperty) { return null; } if (baseProperty.dataType === "reference") { return