Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Psp 7128 fix table pagination when table size prop is supplied. #3559

Merged
merged 3 commits into from
Nov 6, 2023
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
1 change: 0 additions & 1 deletion source/frontend/src/components/Table/PageSizeSelector.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ export const TablePageSizeSelector: React.FC<React.PropsWithChildren<IProps>> =
<Menu options={pageSizeOptions} alignTop={alignTop}>
<div style={{ display: 'flex' }}>
<StyledText>Show</StyledText>
{/*the selector appears to capture the click event which prevents the page change from being registered*/}
<Form.Control
size="sm"
value={`${selected}`}
Expand Down
19 changes: 18 additions & 1 deletion source/frontend/src/components/Table/Table.test.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { act, render, RenderOptions, userEvent } from '@/utils/test-utils';
import { act, render, RenderOptions, userEvent, waitFor } from '@/utils/test-utils';

import { ColumnWithProps, Table, TableProps } from '.';
import { IIdentifiedObject } from './Table';
Expand Down Expand Up @@ -172,6 +172,23 @@ describe('Generic table component', () => {
});
expect(onPageSizeChange).toHaveBeenCalledWith(5);
});

it('page size displays a second page as expected', async () => {
const { getByLabelText, container } = setup({
props: {
pageSize: 5,
manualPagination: false,
},
});

await act(async () => {
userEvent.click(getByLabelText('Page 2'));
});
await waitFor(async () => {
const tableRows = container.querySelectorAll('.table .tbody .tr-wrapper');
expect(tableRows).toHaveLength(1);
});
});
});

it('can select only one row at a time when single select prop set', async () => {
Expand Down
6 changes: 4 additions & 2 deletions source/frontend/src/components/Table/Table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,9 @@ export const Table = <T extends IIdentifiedObject, TFilter extends object = {}>(
const filterFormRef = useRef<FormikProps<any>>();

const [expandedRows, setExpandedRows] = React.useState<T[]>([]);
const [internalPageSize, setInternalPageSize] = React.useState<number>(DEFAULT_PAGE_SIZE);
const [internalPageSize, setInternalPageSize] = React.useState<number>(
props.pageSize ?? DEFAULT_PAGE_SIZE,
);
const defaultColumn = React.useMemo(
() => ({
// When using the useFlexLayout:
Expand Down Expand Up @@ -292,7 +294,7 @@ export const Table = <T extends IIdentifiedObject, TFilter extends object = {}>(
} = props;
const manualSortBy = !!externalSort || props.manualSortBy;
const totalItems = externalTotalItems ?? data?.length;
const pageCount = externalPageCount ?? Math.ceil(totalItems / (pageSizeProp ?? internalPageSize));
const pageCount = externalPageCount ?? Math.ceil(totalItems / internalPageSize);
const selectedRowsRef = React.useRef<T[]>(externalSelectedRows ?? []); // used as a global var for providing up to date list of selected rows to code within the table (that is arrow function scoped).

// manually update the contents of the global ref of selected rows if the list of external selected rows changes.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ export const DocumentResults: React.FunctionComponent<
externalSort={{ sort, setSort }}
data={results ?? []}
noRowsMessage="No matching Documents found"
pageSize={10}
manualPagination={false}
{...rest}
></Table>
Expand Down
Loading