All files / src/utils/components/tables/pinned-table PinnedTable.jsx

100% Statements 23/23
65.78% Branches 25/38
100% Functions 5/5
100% Lines 20/20

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168          2645x   7x 3256x 2960x 1480x             7x                                                   299x   299x 299x 299x 299x 299x 299x 299x   299x                                                                                                                                                     1165x   1165x   1165x               23x                                             299x        
import { Box, Typography } from "@mui/material";
import { useTheme } from "@mui/material/styles";
import { MaterialReactTable, useMaterialReactTable } from "material-react-table";
import { getHeroSoftTableProps } from "@/utils/components/tables/heroTableStyles";
 
const mergeSx = (...parts) => Object.assign({}, ...parts.filter(Boolean));
 
const mergeProps = (base, override) => {
    if (!base) return override;
    if (!override) return base;
    return {
        ...base,
        ...override,
        sx: mergeSx(base.sx, override.sx),
    };
};
 
const PinnedTable = ({
    columns,
    data,
    topToolbar,
    pinnedColumn,
    rowActions,
    isLoading,
    muiTablePaginationProps,
    initialState: customInitialState,
    muiTableContainerProps: customTableContainerProps,
    enableStickyHeader = false,
    variant = "default",
    tableId,
    ...rest
}) => {
    const {
        muiTableBodyRowProps: customBodyRowProps,
        muiTablePaperProps: customPaperProps,
        muiTableProps: customTableProps,
        muiTableHeadRowProps: customHeadRowProps,
        muiTableHeadCellProps: customHeadCellProps,
        muiTableBodyCellProps: customBodyCellProps,
        muiTopToolbarProps: customTopToolbarProps,
        muiBottomToolbarProps: customBottomToolbarProps,
        muiSelectProps: customSelectProps,
        ...remainingRest
    } = rest;
 
    const theme = useTheme();
    const isSoft = variant === "soft";
    const softProps = isSoft ? getHeroSoftTableProps(theme) : {};
    const searchFieldId = tableId ? `${tableId}-search` : undefined;
    const paperId = tableId ? `${tableId}-paper` : undefined;
    const containerId = tableId ? `${tableId}-container` : undefined;
    const emptyId = tableId ? `${tableId}-empty` : undefined;
 
    const table = useMaterialReactTable({
        renderTopToolbar: topToolbar,
        columns,
        data,
        layoutMode: isSoft ? softProps.layoutMode : "grid",
        initialState: {
            showGlobalFilter: true,
            columnPinning: pinnedColumn ? pinnedColumn : false,
            // pageIndex belongs here: MRT takes this object as the WHOLE initial
            // pagination state, so omitting it leaves pageIndex undefined and the
            // paginated row model slices with NaN — getPrePaginationRowModel() holds
            // every row while getRowModel() holds none.
            //
            // The deployed app is fine (the admin-staff client ships the identical
            // config on the same MRT version and its tables render rows), so
            // something in a real browser settles the state that jsdom does not.
            // Under jsdom it does NOT settle: every PinnedTable renders its
            // "No records to display" fallback, which is why no unit test could
            // render this table with rows until now. Completing the initial state
            // is the documented default and makes the row model well-defined in
            // both. Modified by Allan Ninal.
            pagination: { pageIndex: 0, pageSize: 10 },
            ...customInitialState,
        },
 
        enableRowActions: !!rowActions,
        positionActionsColumn: "last",
        renderRowActionMenuItems: rowActions,
 
        paginationDisplayMode: "pages",
        muiPaginationProps: mergeProps(softProps.muiTablePaginationProps, muiTablePaginationProps),
        positionToolbarAlertBanner: "bottom",
 
        muiSearchTextFieldProps: mergeProps(
            isSoft ? softProps.muiSearchTextFieldProps : { size: "small", variant: "outlined" },
            searchFieldId ? { id: searchFieldId } : null
        ),
 
        enableColumnActions: false,
        enableSorting: false,
        enableFullScreenToggle: false,
        enableDensityToggle: false,
        enableHiding: false,
        enableColumnFilters: false,
        enableColumnPinning: false,
        enableGlobalFilter: true,
        enableStickyHeader,
 
        muiTablePaperProps: mergeProps(
            softProps.muiTablePaperProps,
            paperId ? { id: paperId } : null,
            customPaperProps
        ),
        muiTableProps: mergeProps(softProps.muiTableProps, customTableProps),
        muiTableHeadRowProps: mergeProps(softProps.muiTableHeadRowProps, customHeadRowProps),
        muiTableHeadCellProps: mergeProps(softProps.muiTableHeadCellProps, customHeadCellProps),
        muiTableBodyCellProps: mergeProps(softProps.muiTableBodyCellProps, customBodyCellProps),
        muiTopToolbarProps: mergeProps(
            softProps.muiTopToolbarProps,
            tableId ? { id: `${tableId}-toolbar` } : null,
            customTopToolbarProps
        ),
        muiBottomToolbarProps: mergeProps(
            softProps.muiBottomToolbarProps,
            tableId ? { id: `${tableId}-pagination` } : null,
            customBottomToolbarProps
        ),
        muiSelectProps: mergeProps(softProps.muiSelectProps, customSelectProps),
        muiTableContainerProps: mergeProps(
            softProps.muiTableContainerProps,
            containerId ? { id: containerId } : null,
            customTableContainerProps
        ),
 
        muiTableBodyRowProps: (rowProps) => {
            const softRow = softProps.muiTableBodyRowProps || {};
            const customRow =
                typeof customBodyRowProps === "function" ? customBodyRowProps(rowProps) : customBodyRowProps || {};
 
            return {
                ...softRow,
                ...customRow,
                sx: mergeSx(softRow.sx, customRow.sx),
            };
        },
 
        renderEmptyRowsFallback: () => (
            <Box
                id={emptyId}
                sx={{
                    display: "flex",
                    justifyContent: "center",
                    alignItems: "center",
                    width: "100%",
                    py: 4,
                }}
            >
                <Typography variant="body2" color="text.secondary">
                    No records to display
                </Typography>
            </Box>
        ),
 
        state: {
            isLoading: isLoading,
        },
 
        ...remainingRest,
    });
 
    return <MaterialReactTable table={table} />;
};
 
export default PinnedTable;