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

[DateGrid] Rename events and add onHover event #103

Merged
merged 12 commits into from
Jul 27, 2020
7 changes: 5 additions & 2 deletions packages/grid/data-grid/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,19 @@
"publishConfig": {
"access": "public"
},
"dependencies": {
"tslib": "^2.0.0"
},
"devDependencies": {
"@material-ui/x-grid-modules": "^0.1.62",
"@rollup/plugin-node-resolve": "^8.0.1",
"rollup": "^2.6.1",
"rollup-plugin-cleaner": "^1.0.0",
"rollup-plugin-command": "^1.1.3",
"rollup-plugin-dts": "^1.4.7",
"rollup-plugin-sourcemaps": "^0.6.2",
"rollup-plugin-terser": "^5.3.0",
"rollup-plugin-typescript2": "^0.27.1",
"tslib": "^2.0.0"
"rollup-plugin-typescript2": "^0.27.1"
},
"peerDependencies": {
"@material-ui/core": "^4.9.12",
Expand Down
2 changes: 1 addition & 1 deletion packages/grid/data-grid/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export default [
cleaner({
targets: ['./dist/'],
}),
typescript(),
typescript({ build: true }),
!production && sourceMaps(),
production && terser(),
],
Expand Down
3 changes: 0 additions & 3 deletions packages/grid/data-grid/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,2 @@
import { DataGrid } from './data-grid';

export * from '@material-ui/x-grid-modules';
export * from './data-grid';
export const Grid = DataGrid;
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as React from 'react';
import Checkbox from '@material-ui/core/Checkbox';
import styled from 'styled-components';
import { SelectionChangedParams } from '../models/params/selectionChangedParams';
import { SelectionChangeParams } from '../models/params/selectionChangeParams';
import { ColParams } from '../models/params/colParams';
import { CellParams } from '../models/params/cellParams';

Expand All @@ -17,8 +17,8 @@ export const HeaderCheckbox: React.FC<ColParams> = React.memo(({ api }) => {
setChecked(checked);
api.selectRows(api.getAllRowIds(), checked);
};
const selectionChanged = React.useCallback(
(event: SelectionChangedParams) => {
const selectionChange = React.useCallback(
(event: SelectionChangeParams) => {
const isAllSelected =
api.getAllRowIds().length === event.rows.length && event.rows.length > 0;
const hasNoneSelected = event.rows.length === 0;
Expand All @@ -29,8 +29,8 @@ export const HeaderCheckbox: React.FC<ColParams> = React.memo(({ api }) => {
);

React.useEffect(() => {
return api.onSelectionChanged(selectionChanged);
}, [api, selectionChanged]);
return api.onSelectionChange(selectionChange);
}, [api, selectionChange]);
return (
<CheckboxInputContainer>
<Checkbox
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,12 @@ export const ColumnHeaderItem = React.memo(

let headerComponent: React.ReactElement | null = null;
if (column.renderHeader) {
headerComponent = column.renderHeader({ api: api!.current!, colDef: column, colIndex });
headerComponent = column.renderHeader({
api: api!.current!,
colDef: column,
colIndex,
field: column.field,
});
}

const onResize = onResizeColumn ? () => onResizeColumn(column) : undefined;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export const DefaultFooter = React.forwardRef<HTMLDivElement, DefaultFooterProps

React.useEffect(() => {
if (api && api.current) {
return api.current!.onSelectionChanged(({ rows }) => setSelectedCount(rows.length));
return api.current!.onSelectionChange(({ rows }) => setSelectedCount(rows.length));
}

return undefined;
Expand Down
61 changes: 15 additions & 46 deletions packages/grid/x-grid-modules/src/components/row-cells.tsx
Original file line number Diff line number Diff line change
@@ -1,37 +1,9 @@
import * as React from 'react';
import {
CellClassParams,
CellValue,
ColDef,
CellParams,
Columns,
GridApi,
RowModel,
ValueFormatterParams,
ValueGetterParams,
CellClassRules,
} from '../models';
import { CellClassParams, Columns, RowModel, CellClassRules, CellParams } from '../models';
import { Cell, GridCellProps } from './cell';
import { ApiContext } from './api-context';
import { classnames, isFunction } from '../utils';

function getCellParams(
rowModel: RowModel,
col: ColDef,
rowIndex: number,
value: CellValue,
api: GridApi,
): CellParams {
return {
value,
getValue: (field: string) => rowModel.data[field],
data: rowModel.data,
rowModel,
colDef: col,
rowIndex,
api,
};
}
import { buildCellParams } from '../utils/paramsUtils';

function applyCssClassRules(cellClassRules: CellClassRules, params: CellClassParams) {
return Object.entries(cellClassRules).reduce((appliedCss, entry) => {
Expand Down Expand Up @@ -77,44 +49,41 @@ export const RowCells: React.FC<RowCellsProps> = React.memo((props) => {
: !removeLastBorderRight && !props.extendRowFullWidth;

let value = row.data[column.field!];
const cellParams: CellParams = buildCellParams({
rowModel: row,
colDef: column,
rowIndex,
value,
api: api!.current!,
});

if (column.valueGetter) {
const params: ValueGetterParams = getCellParams(row, column, rowIndex, value, api!.current!);
// Value getter override the original value
value = column.valueGetter(params);
value = column.valueGetter(cellParams);
}

let formattedValueProp = {};
if (column.valueFormatter) {
const params: ValueFormatterParams = getCellParams(
row,
column,
rowIndex,
value,
api!.current!,
);
formattedValueProp = { formattedValue: column.valueFormatter(params) };
formattedValueProp = { formattedValue: column.valueFormatter(cellParams) };
}

let cssClassProp = { cssClass: '' };
if (column.cellClassName) {
if (!isFunction(column.cellClassName)) {
cssClassProp = { cssClass: classnames(column.cellClassName) };
} else {
const params: CellClassParams = getCellParams(row, column, rowIndex, value, api!.current!);
cssClassProp = { cssClass: column.cellClassName(params) as string };
cssClassProp = { cssClass: column.cellClassName(cellParams) as string };
}
}

if (column.cellClassRules) {
const params: CellClassParams = getCellParams(row, column, rowIndex, value, api!.current!);
const cssClass = applyCssClassRules(column.cellClassRules, params);
const cssClass = applyCssClassRules(column.cellClassRules, cellParams);
cssClassProp = { cssClass: `${cssClassProp.cssClass} ${cssClass}` };
}

let cellComponent: React.ReactElement | null = null;
if (column.renderCell) {
const params: CellParams = getCellParams(row, column, rowIndex, value, api!.current!);
cellComponent = column.renderCell(params);
cellComponent = column.renderCell(cellParams);
cssClassProp = { cssClass: `${cssClassProp.cssClass} with-renderer` };
}

Expand Down
18 changes: 11 additions & 7 deletions packages/grid/x-grid-modules/src/constants/eventsConstants.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
export const RESIZE = 'resize';
export const UNMOUNT = 'unmount';
export const CLICK_EVENT = 'click';
export const HOVER_EVENT = 'mouseover';
export const KEYDOWN_EVENT = 'keydown';
export const KEYUP_EVENT = 'keyup';

export const CELL_CLICKED = 'cellClicked';
export const ROW_CLICKED = 'rowClicked';
export const CELL_CLICK = 'cellClick';
export const CELL_HOVER = 'cellHover';
export const ROW_CLICK = 'rowClick';
export const ROW_HOVER = 'rowHover';
export const ROW_SELECTED_EVENT = 'rowSelected';
export const SELECTION_CHANGED_EVENT = 'selectionChanged';
export const COLUMN_HEADER_CLICKED = 'columnClicked';
export const PAGE_CHANGED_EVENT = 'pageChanged';
export const PAGESIZE_CHANGED_EVENT = 'pageSizeChanged';
export const SELECTION_CHANGED_EVENT = 'selectionChange';
export const COLUMN_HEADER_CLICK = 'columnClick';
export const COLUMN_HEADER_HOVER = 'columnHeaderHover';
export const PAGE_CHANGED_EVENT = 'pageChange';
export const PAGESIZE_CHANGED_EVENT = 'pageSizeChange';

export const SCROLLING_START = 'scrolling:start';
export const SCROLLING = 'scrolling';
Expand All @@ -24,4 +28,4 @@ export const COLUMNS_UPDATED = 'columnsUpdated';
export const SORT_MODEL_UPDATED = 'sortModelUpdated';
export const POST_SORT = 'postSort'; // ag-grid compatible
export const COLUMNS_SORTED = 'columnsSorted';
export const MULTIPLE_KEY_PRESS_CHANGED = 'multipleKeyPressChanged';
export const MULTIPLE_KEY_PRESS_CHANGED = 'multipleKeyPressChange';
41 changes: 16 additions & 25 deletions packages/grid/x-grid-modules/src/hooks/features/usePagination.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
} from '../../constants/eventsConstants';
import { useApiMethod } from '../root/useApiMethod';
import { useApiEventHandler } from '../root/useApiEventHandler';
import { PageChangedParams } from '../../models/params/pageChangedParams';
import { PageChangeParams } from '../../models/params/pageChangeParams';
import { Rows } from '../../models/rows';
import { InternalColumns } from '../../models/colDef/colDef';
import { GridOptions } from '../../models/gridOptions';
Expand All @@ -23,7 +23,7 @@ export interface PaginationProps {
setPage: (page: number) => void;
setPageSize: (pageSize: number) => void;
}
export type PaginationState = PageChangedParams;
export type PaginationState = PageChangeParams;
const UPDATE_STATE_ACTION = 'updateState';

function updateStateAction(
Expand Down Expand Up @@ -84,7 +84,7 @@ export const usePagination = (
stateRef.current.paginationMode === FeatureMode.client ? page : 1,
);

const params: PageChangedParams = {
const params: PageChangeParams = {
...stateRef.current,
page,
};
Expand All @@ -111,30 +111,30 @@ export const usePagination = (
newPage = newPage > newPageCount ? newPageCount : newPage;
newPage = newPage < 1 ? 1 : newPage;
logger.info(
`PageSize changed to ${pageSize}, setting page to ${newPage}, total page count is ${newPageCount}`,
`PageSize change to ${pageSize}, setting page to ${newPage}, total page count is ${newPageCount}`,
);
const newState: PaginationState = {
...stateRef.current,
page: newPage,
pageCount: newPageCount,
pageSize,
};
apiRef.current!.emit(PAGESIZE_CHANGED_EVENT, newState as PageChangedParams);
apiRef.current!.emit(PAGESIZE_CHANGED_EVENT, newState as PageChangeParams);

updateState(newState);
setPage(newPage);
},
[stateRef, apiRef, setPage, updateState, logger],
);

const onPageChanged = React.useCallback(
(handler: (param: PageChangedParams) => void): (() => void) => {
const onPageChange = React.useCallback(
(handler: (param: PageChangeParams) => void): (() => void) => {
return apiRef!.current!.registerEvent(PAGE_CHANGED_EVENT, handler);
},
[apiRef],
);
const onPageSizeChanged = React.useCallback(
(handler: (param: PageChangedParams) => void): (() => void) => {
const onPageSizeChange = React.useCallback(
(handler: (param: PageChangeParams) => void): (() => void) => {
return apiRef!.current!.registerEvent(PAGESIZE_CHANGED_EVENT, handler);
},
[apiRef],
Expand Down Expand Up @@ -174,24 +174,15 @@ export const usePagination = (
React.useEffect(() => {
const rowCount = options.rowCount == null ? rows.length : options.rowCount;
if (rowCount !== state.rowCount) {
logger.info(`Options or rows changed, recalculating pageCount and rowCount`);
logger.info(`Options or rows change, recalculating pageCount and rowCount`);
const newPageCount = getPageCount(state.pageSize, rowCount);

updateState({ pageCount: newPageCount, rowCount });
updateState({ pageCount: newPageCount, rowCount: rows.length });
if (state.page > newPageCount) {
setPage(newPageCount);
}
}
}, [
rows.length,
options.rowCount,
logger,
updateState,
state.rowCount,
state.pageSize,
setPage,
state.page,
]);
}, [rows.length, logger, updateState, state.rowCount, state.pageSize, setPage, state.page]);

React.useEffect(() => {
if (
Expand All @@ -209,8 +200,8 @@ export const usePagination = (
}
}, [options.autoPageSize, resetAutopageSize, columns.visible.length]);

useApiEventHandler(apiRef, PAGE_CHANGED_EVENT, options.onPageChanged);
useApiEventHandler(apiRef, PAGESIZE_CHANGED_EVENT, options.onPageSizeChanged);
useApiEventHandler(apiRef, PAGE_CHANGED_EVENT, options.onPageChange);
useApiEventHandler(apiRef, PAGESIZE_CHANGED_EVENT, options.onPageSizeChange);

const onResize = React.useCallback(() => {
if (options.autoPageSize) {
Expand All @@ -223,8 +214,8 @@ export const usePagination = (
const paginationApi: PaginationApi = {
setPageSize,
setPage,
onPageChanged,
onPageSizeChanged,
onPageChange,
onPageSizeChange,
};

useApiMethod(apiRef, paginationApi, 'paginationApi');
Expand Down
Loading