Skip to content

Commit

Permalink
Created dynamic callback to support dynamic conditions
Browse files Browse the repository at this point in the history
  • Loading branch information
underfisk committed Oct 28, 2020
1 parent a067891 commit 6ec67d9
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 13 deletions.
15 changes: 6 additions & 9 deletions src/columnGrid/types/header.type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<TRow = Row> {
row: TRow
Expand Down Expand Up @@ -40,10 +41,6 @@ export interface DisableNavigationFn {
(coords: NavigationCoords): boolean
}

export interface CellClassNameFn<TRow = Row> {
(row: TRow, column: Column): string
}

export interface ComponentPropsFn<TRow = Row> {
(row: TRow, column: Column):
| Partial<React.HTMLAttributes<HTMLInputElement>>
Expand All @@ -68,7 +65,7 @@ export interface Column<Key = string, Metadata = unknown> {
maxLength?: number
width?: React.ReactText
className?: string
cellClassName?: string | CellClassNameFn
cellClassName?: string | DynamicCallback<Row, string>
readOnly?: boolean | IsReadOnlyCallback
disableNavigation?: boolean | DisableNavigationFn
/**
Expand Down Expand Up @@ -122,9 +119,9 @@ export interface Column<Key = string, Metadata = unknown> {
* Forces to disable the backspace keydown on cells (travel like excel default behaviour)
* @default false
*/
disableBackspace?: boolean
disableCellPaste?: boolean
disableCellCut?: boolean
disableBackspace?: boolean | DynamicCallback<Row, boolean>
disableCellPaste?: boolean | DynamicCallback<Row, boolean>
disableCellCut?: boolean | DynamicCallback<Row, boolean>
/**
* Number of ms to open editor (used in second arms)
* @default undefined
Expand All @@ -142,7 +139,7 @@ export interface Column<Key = string, Metadata = unknown> {
export interface NestedHeader {
title: string
tooltip?: string
className?: string
className?: string | DynamicCallback<Row, string>
tooltipProps?: {
/** @default true **/
arrow?: boolean
Expand Down
3 changes: 1 addition & 2 deletions src/gridWrapper/GridWrapper.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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)
Expand Down
5 changes: 3 additions & 2 deletions src/rowSelection/selectionProps.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React from 'react'
import { Row } from '../types'
import { DynamicCallback } from '../types/dynamic-callback'

export interface SelectionProps {
/**
Expand All @@ -17,7 +18,7 @@ export interface SelectionProps {
/**
* Classname for the row checkbox
*/
checkboxClass?: string
checkboxClass?: string | DynamicCallback<Row, string>

/**
* Style for the column of selection
Expand All @@ -26,7 +27,7 @@ export interface SelectionProps {
/**
* Style for the cell in the selected row
*/
cellClassName?: string
cellClassName?: string | DynamicCallback<Row, string>

/**
* @default 2%
Expand Down
10 changes: 10 additions & 0 deletions src/types/dynamic-callback.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Row } from './row.type'
import { Column } from '../columnGrid/types'

export interface DynamicCallbackParams<TRow> {
row: TRow
column: Column
}
export interface DynamicCallback<TRow = Row, TResult = unknown> {
(params: DynamicCallbackParams<TRow>): TResult
}

0 comments on commit 6ec67d9

Please sign in to comment.