Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 42 additions & 33 deletions packages/react-mui-hooks/hooks/useDataGrid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,10 @@ export function useDataGrid({
* @param newVisibility - The new visibility model.
*/
const handleColumnVisibilityChange = (newVisibility: GridColumnVisibilityModel) => {
// Do not hide the last remaining column
const hiddenColumns = Object.values(newVisibility).filter(v => !v).length;
if ((columns.length - hiddenColumns) <= 0) return;

setColumnVisibility(newVisibility);
if (tableId) {
localStorage.setItem(`${columnVisibilityLocalStorageKey}-${tableId}`, JSON.stringify(newVisibility));
Expand Down Expand Up @@ -538,15 +542,15 @@ export function useDataGrid({
if (selection && !checkboxSelection) {
// Move the selected row index only if the key pressed is either ArrowUp or ArrowDown
if (event.key === 'ArrowUp' || event.key === 'ArrowDown') {
const currentRowId = details.api.getRowId(params.row);
const currentRowIndex = details.api.getRowIndexRelativeToVisibleRows(currentRowId);
const currentRowId = details.api.getRowId(params.row);
const currentRowIndex = details.api.getRowIndexRelativeToVisibleRows(currentRowId);

const nextRowIndex = Math.min(Math.max(currentRowIndex + (event.key === 'ArrowDown' ? 1 : -1), 0), allRows.length - 1);
const nextRowId = details.api.getRowIdFromRowIndex(nextRowIndex);
const nextRowIndex = Math.min(Math.max(currentRowIndex + (event.key === 'ArrowDown' ? 1 : -1), 0), allRows.length - 1);
const nextRowId = details.api.getRowIdFromRowIndex(nextRowIndex);

if (nextRowId && nextRowId !== currentRowId) {
setCustomSelectionModel([ nextRowId ]);
}
if (nextRowId && nextRowId !== currentRowId) {
setCustomSelectionModel([nextRowId]);
}
}
}
};
Expand All @@ -566,34 +570,34 @@ export function useDataGrid({

const columnsMemo = useMemo(() => {
const columnsAdjusted = columns.map((c) => {
// Get column width from local storage
let width: number | null | undefined;
// Get column width from local storage
let width: number | null | undefined;

if (tableId) {
const columnSizeStorageValue = localStorage.getItem(`${columnSizeLocalStorageKey}-${tableId}-${c.field}`);
if (columnSizeStorageValue) {
width = Number(columnSizeStorageValue);
if (tableId) {
const columnSizeStorageValue = localStorage.getItem(`${columnSizeLocalStorageKey}-${tableId}-${c.field}`);
if (columnSizeStorageValue) {
width = Number(columnSizeStorageValue);
}
}
}

return {
...c,
cellClassName: `${c.cellClassName || ''} mui-datagrid-cell-narrow-on-mobile`,
renderCell: c.renderCell || ((params: ExtendedGridRenderCellParams) => (
<CellRenderer
customType={params.colDef.customType}
value={params.value}
width={params.width}
rowHeight={rowHeight}
params={params.colDef}
/>
)),
renderHeader: c.renderHeader || headerRenderer,
...resolveCustomTypeOperators(c),
...width && {
flex: undefined,
width
}
return {
...c,
cellClassName: `${c.cellClassName || ''} mui-datagrid-cell-narrow-on-mobile`,
renderCell: c.renderCell || ((params: ExtendedGridRenderCellParams) => (
<CellRenderer
customType={params.colDef.customType}
value={params.value}
width={params.width}
rowHeight={rowHeight}
params={params.colDef}
/>
)),
renderHeader: c.renderHeader || headerRenderer,
...resolveCustomTypeOperators(c),
...width && {
flex: undefined,
width
}
};
});

Expand Down Expand Up @@ -726,7 +730,12 @@ export function useDataGrid({
loadingOverlay: LinearProgress,
...slots
},
slotProps,
slotProps: {
columnsPanel: {
disableHideAllButton: true
},
...slotProps
},
keepNonExistentRowsSelected
},
refreshTable: handleTableRefresh,
Expand Down