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

[DataGrid] Change page index base, from 1 to 0 #1021

Merged
merged 2 commits into from
Feb 10, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 2 additions & 2 deletions packages/grid/_modules_/grid/components/Pagination.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export function Pagination() {

const onPageChange = React.useCallback(
(event: any, page: number) => {
apiRef!.current!.setPage(page + 1);
apiRef!.current!.setPage(page);
},
[apiRef],
);
Expand All @@ -67,7 +67,7 @@ export function Pagination() {
classes={classes}
component="div"
count={paginationState.rowCount}
page={paginationState.page - 1}
page={paginationState.page}
rowsPerPageOptions={
options.rowsPerPageOptions &&
options.rowsPerPageOptions.indexOf(paginationState.pageSize) > -1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ export const useKeyboard = (gridRootRef: React.RefObject<HTMLDivElement>, apiRef
const currentColIndex = Number(cellEl.getAttribute('aria-colindex'));
const currentRowIndex = Number(cellEl.getAttribute('data-rowindex'));
const rowCount = options.pagination
? paginationState.pageSize * paginationState.page
? paginationState.pageSize * (paginationState.page + 1)
: totalRowCount;

let nextCellIndexes: CellIndexCoordinates;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ export const setRowCountStateUpdate = (state, payload): PaginationState => {
};

export const INITIAL_PAGINATION_STATE: PaginationState = {
page: 1,
page: 0,
pageCount: 0,
pageSize: 0,
paginationMode: 'client',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ export const usePagination = (apiRef: ApiRef): void => {
}, [apiRef, dispatch, options.paginationMode]);

React.useEffect(() => {
setPage(options.page != null ? options.page : 1);
setPage(options.page != null ? options.page : 0);
}, [apiRef, options.page, setPage]);

React.useEffect(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,7 @@ export const useVirtualRows = (
paginationState.pageSize != null &&
paginationState.paginationMode === 'client'
) {
minRowIdx =
paginationState.pageSize * (paginationState.page - 1 > 0 ? paginationState.page - 1 : 0);
minRowIdx = paginationState.pageSize * paginationState.page;
}

const firstRowIdx = page * apiRef.current.state.containerSizes.viewportPageSize + minRowIdx;
Expand Down Expand Up @@ -210,7 +209,7 @@ export const useVirtualRows = (
let scrollTop;

const currentRowPage =
(params.rowIndex - (gridState.pagination.page - 1) * gridState.pagination.pageSize) /
(params.rowIndex - gridState.pagination.page * gridState.pagination.pageSize) /
gridState.containerSizes!.viewportPageSize;
const scrollPosition = currentRowPage * gridState!.viewportSizes.height;
const viewportHeight = gridState.viewportSizes.height;
Expand Down
17 changes: 7 additions & 10 deletions packages/grid/data-grid/src/tests/pagination.DataGrid.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ describe('<DataGrid /> - Pagination', () => {
}
});

it('should apply the page prop correctly', (done) => {
it('should apply the page prop correctly', () => {
const rows = [
{
id: 0,
Expand All @@ -58,14 +58,11 @@ describe('<DataGrid /> - Pagination', () => {
];
render(
<div style={{ width: 300, height: 300 }}>
<DataGrid {...baselineProps} rows={rows} page={2} pageSize={1} />
<DataGrid {...baselineProps} rows={rows} page={1} pageSize={1} />
</div>,
);
setTimeout(() => {
const cell = document.querySelector('[role="cell"][aria-colindex="0"]')!;
expect(cell).to.have.text('Addidas');
done();
}, 50);
const cell = document.querySelector('[role="cell"][aria-colindex="0"]')!;
expect(cell).to.have.text('Addidas');
});

it('should trigger onPageChange once if page prop is set', () => {
Expand Down Expand Up @@ -126,7 +123,7 @@ describe('<DataGrid /> - Pagination', () => {

it('should support server side pagination', () => {
const ServerPaginationGrid = () => {
const [page, setPage] = React.useState(1);
const [page, setPage] = React.useState(0);
const [rows, setRows] = React.useState<RowsProp>([]);

const handlePageChange = (params) => {
Expand Down Expand Up @@ -172,9 +169,9 @@ describe('<DataGrid /> - Pagination', () => {
};

render(<ServerPaginationGrid />);
expect(getColumnValues()).to.deep.equal(['Nike 1']);
expect(getColumnValues()).to.deep.equal(['Nike 0']);
fireEvent.click(screen.getByRole('button', { name: /next page/i }));
expect(getColumnValues()).to.deep.equal(['Nike 2']);
expect(getColumnValues()).to.deep.equal(['Nike 1']);
});
});
});
2 changes: 1 addition & 1 deletion packages/grid/x-grid/src/tests/pagination.XGrid.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ describe('<XGrid /> - Pagination', () => {
let cell = document.querySelector('[role="cell"][aria-colindex="0"]')!;
expect(cell).to.have.text('Nike');
act(() => {
apiRef.current.setPage(2);
apiRef.current.setPage(1);
});

cell = document.querySelector('[role="cell"][aria-colindex="0"]')!;
Expand Down
10 changes: 5 additions & 5 deletions packages/storybook/src/stories/grid-pagination.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export const PaginationArgs: Story = (args) => {
PaginationArgs.args = {
pagination: true,
pageSize: 100,
page: 1,
page: 0,
rowCount: 2000,
autoPageSize: false,
rowsPerPageOptions: [10, 20, 50, 100, 200],
Expand Down Expand Up @@ -130,9 +130,9 @@ export function PaginationApiTests() {
Pagination: ({ state }) => (
<Pagination
className="my-custom-pagination"
page={state.pagination.page}
page={state.pagination.page + 1}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need it here? Is it to load the second page?

Copy link
Member Author

@dtassone dtassone Feb 10, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it to load the second page?

No it's for the component. As it's 0 indexed, we need to add 1 to convert it to the page number from the index

@material-ui/lab/Pagination is 1-based.

count={state.pagination.pageCount}
onChange={(e, value) => apiRef.current.setPage(value)}
onChange={(e, value) => apiRef.current.setPage(value - 1)}
/>
),
}}
Expand Down Expand Up @@ -347,7 +347,7 @@ export const GridTest = () => {
function loadDocsDemoServerRows(page: number, data: any): Promise<any> {
return new Promise<any>((resolve) => {
setTimeout(() => {
resolve(data.rows.slice((page - 1) * 5, page * 5));
resolve(data.rows.slice(page * 5, (page + 1) * 5));
}, Math.random() * 500 + 100); // simulate network latency
});
}
Expand All @@ -358,7 +358,7 @@ export function ServerPaginationDocsDemo() {
rowLength: 100,
maxColumns: 6,
});
const [page, setPage] = React.useState(1);
const [page, setPage] = React.useState(0);
const [rows, setRows] = React.useState<RowsProp>([]);
const [loading, setLoading] = React.useState<boolean>(false);

Expand Down