From 6ec67d9229318e5510675248a54573c1abd4dae1 Mon Sep 17 00:00:00 2001 From: Rodrigo Date: Wed, 28 Oct 2020 02:23:45 +0000 Subject: [PATCH] Created dynamic callback to support dynamic conditions --- src/columnGrid/types/header.type.ts | 15 ++++++--------- src/gridWrapper/GridWrapper.tsx | 3 +-- src/rowSelection/selectionProps.tsx | 5 +++-- src/types/dynamic-callback.ts | 10 ++++++++++ 4 files changed, 20 insertions(+), 13 deletions(-) create mode 100644 src/types/dynamic-callback.ts diff --git a/src/columnGrid/types/header.type.ts b/src/columnGrid/types/header.type.ts index 57ca344..16f72cd 100644 --- a/src/columnGrid/types/header.type.ts +++ b/src/columnGrid/types/header.type.ts @@ -5,6 +5,7 @@ import { EditorProps, EditorRef } from '../../editorManager' import { PopperProps } from '@material-ui/core/Popper/Popper' import { ReactDatePickerProps } from 'react-datepicker' import { Row } from '../../types' +import { DynamicCallback } from '../../types/dynamic-callback' export interface CellRendererProps { row: TRow @@ -40,10 +41,6 @@ export interface DisableNavigationFn { (coords: NavigationCoords): boolean } -export interface CellClassNameFn { - (row: TRow, column: Column): string -} - export interface ComponentPropsFn { (row: TRow, column: Column): | Partial> @@ -68,7 +65,7 @@ export interface Column { maxLength?: number width?: React.ReactText className?: string - cellClassName?: string | CellClassNameFn + cellClassName?: string | DynamicCallback readOnly?: boolean | IsReadOnlyCallback disableNavigation?: boolean | DisableNavigationFn /** @@ -122,9 +119,9 @@ export interface Column { * Forces to disable the backspace keydown on cells (travel like excel default behaviour) * @default false */ - disableBackspace?: boolean - disableCellPaste?: boolean - disableCellCut?: boolean + disableBackspace?: boolean | DynamicCallback + disableCellPaste?: boolean | DynamicCallback + disableCellCut?: boolean | DynamicCallback /** * Number of ms to open editor (used in second arms) * @default undefined @@ -142,7 +139,7 @@ export interface Column { export interface NestedHeader { title: string tooltip?: string - className?: string + className?: string | DynamicCallback tooltipProps?: { /** @default true **/ arrow?: boolean diff --git a/src/gridWrapper/GridWrapper.tsx b/src/gridWrapper/GridWrapper.tsx index 8270180..591dad7 100644 --- a/src/gridWrapper/GridWrapper.tsx +++ b/src/gridWrapper/GridWrapper.tsx @@ -175,7 +175,7 @@ const GridWrapper = React.memo((props: GridWrapperProps) => { classes.cellDefaultStyle, theme?.cellClass, typeof column.cellClassName === 'function' - ? column.cellClassName(row, column) + ? column.cellClassName({ row, column }) : column.cellClassName, ) if (isRowActive && !cell.dummy && theme?.currentRowClass) { @@ -187,7 +187,6 @@ const GridWrapper = React.memo((props: GridWrapperProps) => { } if (props.selection && props.selection.cellClassName) { - const row = props.apiRef.current.getRowAt(rowIndex) const isRowSelected = props.apiRef.current.isRowSelected(row?.[props.selection.key]) if (isRowSelected) { cellClassName = clsx(cellClassName, props.selection.cellClassName) diff --git a/src/rowSelection/selectionProps.tsx b/src/rowSelection/selectionProps.tsx index d40a5f7..d15c3e5 100644 --- a/src/rowSelection/selectionProps.tsx +++ b/src/rowSelection/selectionProps.tsx @@ -1,5 +1,6 @@ import React from 'react' import { Row } from '../types' +import { DynamicCallback } from '../types/dynamic-callback' export interface SelectionProps { /** @@ -17,7 +18,7 @@ export interface SelectionProps { /** * Classname for the row checkbox */ - checkboxClass?: string + checkboxClass?: string | DynamicCallback /** * Style for the column of selection @@ -26,7 +27,7 @@ export interface SelectionProps { /** * Style for the cell in the selected row */ - cellClassName?: string + cellClassName?: string | DynamicCallback /** * @default 2% diff --git a/src/types/dynamic-callback.ts b/src/types/dynamic-callback.ts new file mode 100644 index 0000000..0f043ca --- /dev/null +++ b/src/types/dynamic-callback.ts @@ -0,0 +1,10 @@ +import { Row } from './row.type' +import { Column } from '../columnGrid/types' + +export interface DynamicCallbackParams { + row: TRow + column: Column +} +export interface DynamicCallback { + (params: DynamicCallbackParams): TResult +} \ No newline at end of file