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

[XGrid] Add onViewportRowsChange prop #2038

Merged
merged 46 commits into from
Aug 11, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
97f4d34
Add onVirtualPageChange option
DanailH Jul 5, 2021
4127f39
PR comments
DanailH Jul 6, 2021
863378b
test
DanailH Jul 6, 2021
5afc0da
Resolve conflicts
DanailH Jul 6, 2021
3518322
Renameing
DanailH Jul 8, 2021
83c6d02
resolve conflicts
DanailH Jul 26, 2021
551cb17
Rework onVirtualRowsChange
DanailH Jul 27, 2021
9d7f44e
PR comments
DanailH Jul 27, 2021
36fca97
fix docs
DanailH Jul 27, 2021
315b58e
Merge branch 'master' of github.com:mui-org/material-ui-x into featur…
DanailH Jul 27, 2021
3ba0e5c
Trigger notification
DanailH Jul 27, 2021
cc2a479
PR comments
DanailH Jul 28, 2021
664c45f
Trigger Build
DanailH Jul 28, 2021
4bca503
Story
DanailH Jul 28, 2021
96bfd37
Trigger Build
DanailH Jul 28, 2021
15460f4
Revert story changes
DanailH Jul 30, 2021
d6d4845
Update implementation
DanailH Aug 3, 2021
f94bfec
Update docs/pages/api-docs/data-grid/x-grid.md
DanailH Aug 5, 2021
a245996
Update packages/grid/_modules_/grid/constants/eventsConstants.ts
DanailH Aug 5, 2021
4889fc9
Update packages/grid/_modules_/grid/models/gridOptions.tsx
DanailH Aug 5, 2021
0a3f5f7
adjust tests
DanailH Aug 5, 2021
3477c89
Merge branch 'feature/DataGrid-1715-onVirtualPageChange' of github.co…
DanailH Aug 5, 2021
f90ab87
update docs
DanailH Aug 5, 2021
0edf6ae
Update packages/grid/x-grid/src/tests/events.XGrid.test.tsx
DanailH Aug 5, 2021
b72127c
Update packages/grid/x-grid/src/tests/events.XGrid.test.tsx
DanailH Aug 5, 2021
191848f
Update packages/grid/x-grid/src/tests/events.XGrid.test.tsx
DanailH Aug 5, 2021
2c83269
Resolve conflicts
DanailH Aug 5, 2021
05e6749
Fixing tests
DanailH Aug 5, 2021
7f77ba5
PR comments
DanailH Aug 9, 2021
db1dc5c
Conflicts
DanailH Aug 9, 2021
d301dc8
resolve conflicts
DanailH Aug 10, 2021
c525e4a
Merge branch 'master' of github.com:mui-org/material-ui-x into featur…
DanailH Aug 10, 2021
70c1587
Fix tests
DanailH Aug 10, 2021
72364ca
Fix test
DanailH Aug 10, 2021
fd24d6e
test
DanailH Aug 10, 2021
d9c4bf5
Tests
DanailH Aug 10, 2021
ce57a0e
Resolve conflicts
DanailH Aug 11, 2021
beeaed6
docs update
DanailH Aug 11, 2021
e16832f
Fix windows tests
DanailH Aug 11, 2021
bfe9ac0
Fix tests under Windows
DanailH Aug 11, 2021
30dac03
Update packages/grid/_modules_/grid/constants/eventsConstants.ts
DanailH Aug 11, 2021
b74b946
Update packages/grid/_modules_/grid/models/gridOptions.tsx
DanailH Aug 11, 2021
7715ee4
PR comments
DanailH Aug 11, 2021
219085c
Merge branch 'feature/DataGrid-1715-onVirtualPageChange' of github.co…
DanailH Aug 11, 2021
5bf84c1
formatting and docs
DanailH Aug 11, 2021
2959a5b
small typo
m4theushw Aug 11, 2021
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: 1 addition & 0 deletions docs/pages/api-docs/data-grid/x-grid.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ import { XGrid } from '@material-ui/x-grid';
| <span class="prop-name">onRowsScrollEnd</span> | <span class="prop-type">(param: GridRowScrollEndParams) => void</span> | | Callback fired when scrolling to the bottom of the grid viewport. |
| <span class="prop-name">onSelectionModelChange</span> | <span class="prop-type">(param: GridSelectionModelChangeParams) => void</span> | | Callback fired when the selection state of one or multiple rows changes. |
| <span class="prop-name">onSortModelChange</span> | <span class="prop-type">(param: GridSortModelParams) => void</span> | | Callback fired when the sort model changes before a column is sorted. |
| <span class="prop-name">onVirtualPageChange</span> | <span class="prop-type">(param: GridVirtualPageChangeParams) => void</span> | | Callback fired when the virtual page changes. |
| <span class="prop-name">page</span> | <span class="prop-type">number</span> | 1 | Set the current page. |
| <span class="prop-name">pageSize</span> | <span class="prop-type">number</span> | 100 | Set the number of rows in one page. |
| <span class="prop-name">pagination</span> | <span class="prop-type">boolean</span> | false | If `true`, pagination is enabled. |
Expand Down
4 changes: 4 additions & 0 deletions docs/src/pages/components/data-grid/events/events.json
Original file line number Diff line number Diff line change
Expand Up @@ -181,5 +181,9 @@
{
"name": "columnVisibilityChange",
"description": "Fired when a column visibility changes. Called with a GridColumnVisibilityChangeParams object."
},
{
"name": "virtualPageChange",
"description": "Fired when a the virtual page changes. Called with a GridVirtualPageChangeParams object."
}
]
6 changes: 6 additions & 0 deletions packages/grid/_modules_/grid/constants/eventsConstants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -457,3 +457,9 @@ export const GRID_STATE_CHANGE = 'stateChange';
* @event
*/
export const GRID_COLUMN_VISIBILITY_CHANGE = 'columnVisibilityChange';

/**
* Fired when a the virtual page changes. Called with a [[GridVirtualPageChangeParams]] object.
* @event
*/
export const GRID_VIRTUAL_PAGE_CHANGE = 'virtualPageChange';
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import * as React from 'react';
import { GRID_SCROLL, GRID_ROWS_SCROLL } from '../../../constants/eventsConstants';
import {
GRID_SCROLL,
GRID_ROWS_SCROLL,
GRID_VIRTUAL_PAGE_CHANGE,
} from '../../../constants/eventsConstants';
import { GridApiRef } from '../../../models/api/gridApiRef';
import { GridVirtualizationApi } from '../../../models/api/gridVirtualizationApi';
import { GridCellIndexCoordinates } from '../../../models/gridCell';
Expand All @@ -25,6 +29,7 @@ import { InternalRenderingState } from './renderingState';
import { useGridVirtualColumns } from './useGridVirtualColumns';
import { gridDensityRowHeightSelector } from '../density/densitySelector';
import { scrollStateSelector } from './renderingStateSelector';
import { useGridApiOptionHandler } from '../../root/useGridApiEventHandler';

export const useGridVirtualRows = (apiRef: GridApiRef): void => {
const logger = useLogger('useGridVirtualRows');
Expand Down Expand Up @@ -148,6 +153,14 @@ export const useGridVirtualRows = (apiRef: GridApiRef): void => {
setRenderingState({ virtualPage: nextPage });
logger.debug(`Changing page from ${page} to ${nextPage}`);
requireRerender = true;

apiRef.current.publishEvent(GRID_VIRTUAL_PAGE_CHANGE, {
currentPage: nextPage,
nextPage: nextPage + 1 < containerProps.lastPage ? nextPage + 1 : containerProps.lastPage,
firstRowIndex: nextPage * containerProps.viewportPageSize,
pageSize: containerProps.viewportPageSize,
api: apiRef,
});
} else {
if (!containerProps.isVirtualized && page > 0) {
logger.debug(`Virtualization disabled, setting virtualPage to 0`);
Expand Down Expand Up @@ -405,4 +418,5 @@ export const useGridVirtualRows = (apiRef: GridApiRef): void => {
GRID_SCROLL,
preventViewportScroll,
);
useGridApiOptionHandler(apiRef, GRID_VIRTUAL_PAGE_CHANGE, options.onVirtualPageChange);
};
5 changes: 5 additions & 0 deletions packages/grid/_modules_/grid/models/gridOptions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import { GridResizeParams } from './params/gridResizeParams';
import { GridColumnResizeParams } from './params/gridColumnResizeParams';
import { GridColumnVisibilityChangeParams } from './params/gridColumnVisibilityChangeParams';
import { GridClasses } from './gridClasses';
import { GridVirtualPageChangeParams } from './params/gridVirtualPageChangeParams';

export type MuiEvent<E> = (E | React.SyntheticEvent) & {
defaultMuiPrevented?: boolean;
Expand Down Expand Up @@ -431,6 +432,10 @@ export interface GridOptions {
* Callback fired when the state of the grid is updated.
*/
onStateChange?: (params: any) => void;
/**
* Callback fired when the virtual page changes.
DanailH marked this conversation as resolved.
Show resolved Hide resolved
*/
onVirtualPageChange?: (params: GridVirtualPageChangeParams) => void;
/**
* Set the current page.
* @default 1
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* Object passed as parameter of the virtual page change event.
*/
export interface GridVirtualPageChangeParams {
/**
* The index of the current page.
*/
currentPage: number;
/**
* The index of the next page.
*/
nextPage: number;
DanailH marked this conversation as resolved.
Show resolved Hide resolved
/**
* The index of the first row in the current page.
DanailH marked this conversation as resolved.
Show resolved Hide resolved
*/
firstRowIndex: number;
/**
* The size of the virtual page.
*/
pageSize: number;
DanailH marked this conversation as resolved.
Show resolved Hide resolved
/**
* Api that let you manipulate the grid.
*/
api: any;
}
15 changes: 15 additions & 0 deletions packages/grid/data-grid/src/DataGrid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export type DataGridProps = Omit<
| 'licenseStatus'
| 'options'
| 'onRowsScrollEnd'
| 'onVirtualPageChange'
DanailH marked this conversation as resolved.
Show resolved Hide resolved
| 'pagination'
| 'scrollEndThreshold'
> & {
Expand All @@ -38,6 +39,7 @@ export type DataGridProps = Omit<
disableMultipleColumnsSorting?: true;
disableMultipleSelection?: true;
onRowsScrollEnd?: undefined;
onVirtualPageChange?: undefined;
pagination?: true;
};

Expand Down Expand Up @@ -187,6 +189,19 @@ DataGrid.propTypes = {
}
return null;
}),
onVirtualPageChange: chainPropTypes(PropTypes.any, (props: any) => {
if (props.onVirtualPageChange != null) {
return new Error(
[
`Material-UI: \`onVirtualPageChange\` is not a valid prop.`,
'onVirtualPageChange is not available in the MIT version.',
'',
'You need to upgrade to the XGrid component to unlock this feature.',
].join('\n'),
);
}
return null;
}),
pageSize: chainPropTypes(PropTypes.number, (props: any) => {
if (props.pageSize && props.pageSize > MAX_PAGE_SIZE) {
return new Error(
Expand Down
30 changes: 30 additions & 0 deletions packages/grid/x-grid/src/tests/events.XGrid.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
} from '@material-ui/x-grid';
import { getCell, getColumnHeaderCell, getRow } from 'test/utils/helperFn';
import { spy } from 'sinon';
import { useData } from 'packages/storybook/src/hooks/useData';

describe('<XGrid /> - Events Params', () => {
// TODO v5: replace with createClientRender
Expand Down Expand Up @@ -72,6 +73,16 @@ describe('<XGrid /> - Events Params', () => {
);
};

const TestVirtualization = (props: Partial<XGridProps>) => {
apiRef = useGridApiRef();
DanailH marked this conversation as resolved.
Show resolved Hide resolved
const data = useData(50, 10);
return (
<div style={{ width: 300, height: 300 }}>
<XGrid rows={data.rows} columns={data.columns} apiRef={apiRef} {...props} />
</div>
);
};

describe('columnHeaderParams', () => {
it('should include the correct params', () => {
let eventArgs: { params: GridColumnHeaderParams; event: React.MouseEvent } | null = null;
Expand Down Expand Up @@ -284,4 +295,23 @@ describe('<XGrid /> - Events Params', () => {
gridWindow.dispatchEvent(new Event('scroll'));
expect(handleOnRowsScrollEnd.callCount).to.equal(1);
});

it('call onVirtualPageChange when the virtual page changes', () => {
const handleOnVirtualPageChange = spy();
const { container } = render(
<div style={{ width: 300, height: 300 }}>
<TestVirtualization onVirtualPageChange={handleOnVirtualPageChange} />
</div>,
);
const gridWindow = container.querySelector('.MuiDataGrid-window');
// arbitrary number to make sure that the bottom of the grid window is reached.
gridWindow.scrollTop = 12345;
DanailH marked this conversation as resolved.
Show resolved Hide resolved
gridWindow.dispatchEvent(new Event('scroll'));

expect(handleOnVirtualPageChange.callCount).to.equal(1);
expect(handleOnVirtualPageChange.lastCall.args[0].currentPage).to.equal(15);
expect(handleOnVirtualPageChange.lastCall.args[0].nextPage).to.equal(16);
expect(handleOnVirtualPageChange.lastCall.args[0].firstRowIndex).to.equal(45);
expect(handleOnVirtualPageChange.lastCall.args[0].pageSize).to.equal(3);
});
});