Skip to content

Commit cc98748

Browse files
committed
frontend:table: fix the rows-per-page settings persist among views
1 parent 5808125 commit cc98748

File tree

4 files changed

+25
-27
lines changed

4 files changed

+25
-27
lines changed

frontend/src/components/common/Resource/ResourceTable.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ function ResourceTableContent<RowItem extends KubeObject>(props: ResourceTablePr
328328
hideColumns = [],
329329
filterFunction,
330330
errorMessage,
331-
reflectInURL,
331+
reflectInURL = `${id ? `${id}` : ''}`,
332332
data,
333333
defaultGlobalFilter,
334334
actions,

frontend/src/components/common/Table/Table.tsx

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ import { MRT_Localization_ZH_HANS } from 'material-react-table/locales/zh-Hans';
4848
import { MRT_Localization_ZH_HANT } from 'material-react-table/locales/zh-Hant';
4949
import { memo, ReactNode, useEffect, useMemo, useState } from 'react';
5050
import { useTranslation } from 'react-i18next';
51-
import { getTablesRowsPerPage } from '../../../helpers/tablesRowsPerPage';
51+
import { getTablesRowsPerPage, setTablesRowsPerPage } from '../../../helpers/tablesRowsPerPage';
5252
import { useShortcut } from '../../../lib/useShortcut';
5353
import { useURLState } from '../../../lib/util';
5454
import { useSettings } from '../../App/Settings/hook';
@@ -203,9 +203,11 @@ export default function Table<RowItem extends Record<string, any>>({
203203

204204
const storeRowsPerPageOptions = useSettings('tableRowsPerPageOptions');
205205
const rowsPerPageOptions = rowsPerPage || storeRowsPerPageOptions;
206-
const defaultRowsPerPage = useMemo(() => getTablesRowsPerPage(rowsPerPageOptions[0]), []);
207-
const [pageSize, setPageSize] = useURLState(shouldReflectInURL ? 'perPage' : '', {
206+
const defaultRowsPerPage = getTablesRowsPerPage(rowsPerPageOptions[0]);
207+
208+
const [pageSize, setPageSize] = useURLState('perPage', {
208209
defaultValue: defaultRowsPerPage,
210+
hideDefault: false,
209211
prefix,
210212
});
211213

@@ -272,6 +274,8 @@ export default function Table<RowItem extends Record<string, any>>({
272274
const pagination = updater({ pageIndex: Number(page) - 1, pageSize: Number(pageSize) });
273275
setPage(pagination.pageIndex + 1);
274276
setPageSize(pagination.pageSize);
277+
// Persist the rows per page setting
278+
setTablesRowsPerPage(pagination.pageSize);
275279
},
276280
onGlobalFilterChange: setGlobalFilter,
277281
renderToolbarInternalActions: props => {
@@ -285,27 +289,21 @@ export default function Table<RowItem extends Record<string, any>>({
285289
}
286290
return null;
287291
},
288-
initialState: useMemo(
289-
() => ({
290-
density: 'compact',
291-
globalFilter: globalFilter || '',
292-
...(tableProps.initialState ?? {}),
293-
}),
294-
[tableProps.initialState, globalFilter]
295-
),
296-
state: useMemo(
297-
() => ({
298-
...(tableProps.state ?? {}),
299-
columnOrder,
300-
pagination: {
301-
pageIndex: page - 1,
302-
pageSize: pageSize,
303-
},
304-
globalFilter,
305-
...(globalFilter ? { showGlobalFilter: true } : {}),
306-
}),
307-
[tableProps.state, columnOrder, page, pageSize, globalFilter]
308-
),
292+
initialState: {
293+
density: 'compact',
294+
globalFilter: globalFilter || '',
295+
...(tableProps.initialState ?? {}),
296+
},
297+
state: {
298+
...(tableProps.state ?? {}),
299+
columnOrder,
300+
pagination: {
301+
pageIndex: page - 1,
302+
pageSize: pageSize,
303+
},
304+
globalFilter,
305+
...(globalFilter ? { showGlobalFilter: true } : {}),
306+
},
309307
positionActionsColumn: 'last',
310308
layoutMode: 'grid',
311309
// Need to provide our own empty message

frontend/src/components/common/Table/__snapshots__/Table.ReflectInURL.stories.storyshot

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
Current URL search:
1616
</b>
1717

18-
?p=2
18+
?p=2&perPage=5
1919
</p>
2020
<div
2121
class="MuiBox-root css-1ipmh7v"

frontend/src/components/common/Table/__snapshots__/Table.ReflectInURLWithPrefix.stories.storyshot

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
Current URL search:
1616
</b>
1717

18-
?p=2
18+
?p=2&mySuperTable.perPage=5
1919
</p>
2020
<div
2121
class="MuiBox-root css-1ipmh7v"

0 commit comments

Comments
 (0)