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

Remove onCheckCellIsEditable prop #2016

Merged
merged 8 commits into from
Oct 29, 2020
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@
- ⚠️ `getValidFilterValues`
- ⚠️ `onCellCopyPaste`
- ⚠️ `onSelectedCellRangeChange`
- ⚠️ `onCheckCellIsEditable`
- Use `column.editable` instead.
- ⚠️ `onGridKeyDown`
- ⚠️ `onGridKeyUp`
- ⚠️ `onRowDoubleClick`
Expand Down
6 changes: 1 addition & 5 deletions src/DataGrid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import {

import {
CalculatedColumn,
CheckCellIsEditableEvent,
Column,
Filters,
Position,
Expand Down Expand Up @@ -148,8 +147,6 @@ export interface DataGridProps<R, SR = unknown> extends SharedDivProps {
onColumnResize?: (idx: number, width: number) => void;
/** Function called whenever selected cell is changed */
onSelectedCellChange?: (position: Position) => void;
/** called before cell is set active, returns a boolean to determine whether cell is editable */
onCheckCellIsEditable?: (event: CheckCellIsEditableEvent<R, SR>) => boolean;

/**
* Toggles and modes
Expand Down Expand Up @@ -208,7 +205,6 @@ function DataGrid<R, SR>({
onScroll,
onColumnResize,
onSelectedCellChange,
onCheckCellIsEditable,
// Toggles and modes
enableFilters = false,
enableCellCopyPaste = false,
Expand Down Expand Up @@ -639,7 +635,7 @@ function DataGrid<R, SR>({

function isCellEditable(position: Position): boolean {
return isCellWithinBounds(position)
&& isSelectedCellEditable<R, SR>({ columns, rows, selectedPosition: position, onCheckCellIsEditable, isGroupRow });
&& isSelectedCellEditable<R, SR>({ columns, rows, selectedPosition: position, isGroupRow });
}

function selectCell(position: Position, enableEditor = false): void {
Expand Down
5 changes: 0 additions & 5 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,11 +209,6 @@ export interface RowsUpdateEvent<TUpdatedValue = never> {
fromCellKey?: string;
}

export interface CheckCellIsEditableEvent<TRow, TSummaryRow> extends Position {
row: TRow;
column: CalculatedColumn<TRow, TSummaryRow>;
}

export interface SelectRowEvent {
rowIdx: number;
checked: boolean;
Expand Down
43 changes: 2 additions & 41 deletions src/utils/columnUtils.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { getColumnMetrics, getColumnScrollPosition, canEdit } from './columnUtils';
import { getColumnMetrics, getColumnScrollPosition } from './columnUtils';
import { ValueFormatter } from '../formatters';
import { Column, CalculatedColumn } from '../types';
import { Column } from '../types';
import { createColumns } from '../test/utils';

describe('getColumnMetrics', () => {
Expand Down Expand Up @@ -126,42 +126,3 @@ describe('getColumnScrollPosition', () => {
});
});
});

describe('canEdit', () => {
interface Row {
id: number;
}

const column: CalculatedColumn<Row> = {
idx: 0,
key: 'id',
name: 'ID',
left: 460,
width: 150,
resizable: false,
sortable: false,
formatter: ValueFormatter
};

it('should return the result of editable(row)', () => {
const fnColumn = {
...column,
editor: () => null,
editable(row: Row) { return row.id === 1; }
};
expect(canEdit(fnColumn, { id: 1 })).toBe(true);
expect(canEdit(fnColumn, { id: 0 })).toBe(false);
});

it('should return correct booleans', () => {
const row: Row = { id: 1 };
const editor = () => null;

expect(canEdit(column, row)).toBe(false);
expect(canEdit({ ...column, editable: false }, row)).toBe(false);
expect(canEdit({ ...column, editable: true }, row)).toBe(false);
expect(canEdit({ ...column, editor }, row)).toBe(true);
expect(canEdit({ ...column, editor, editable: false }, row)).toBe(false);
expect(canEdit({ ...column, editor, editable: true }, row)).toBe(true);
});
});
7 changes: 0 additions & 7 deletions src/utils/columnUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,13 +158,6 @@ function clampColumnWidth<R, SR>(
return width;
}

// Logic extented to allow for functions to be passed down in column.editable
// this allows us to decide whether we can be editing from a cell level
export function canEdit<R, SR>(column: Column<R, SR>, row: R): boolean {
const isEditable = typeof column.editable === 'function' ? column.editable(row) : column.editable;
return column.editor != null && isEditable !== false;
}

export function getColumnScrollPosition<R, SR>(columns: readonly CalculatedColumn<R, SR>[], idx: number, currentScrollLeft: number, currentClientWidth: number): number {
let left = 0;
let frozen = 0;
Expand Down
11 changes: 5 additions & 6 deletions src/utils/selectedCellUtils.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
import { CellNavigationMode } from '../enums';
import { canEdit } from './columnUtils';
import { CalculatedColumn, Position, GroupRow } from '../types';

interface IsSelectedCellEditableOpts<R, SR> {
selectedPosition: Position;
columns: readonly CalculatedColumn<R, SR>[];
rows: readonly (R | GroupRow<R>)[];
onCheckCellIsEditable?: (arg: { row: R; column: CalculatedColumn<R, SR> } & Position) => boolean;
isGroupRow: (row: R | GroupRow<R>) => row is GroupRow<R>;
}

export function isSelectedCellEditable<R, SR>({ selectedPosition, columns, rows, onCheckCellIsEditable, isGroupRow }: IsSelectedCellEditableOpts<R, SR>): boolean {
export function isSelectedCellEditable<R, SR>({ selectedPosition, columns, rows, isGroupRow }: IsSelectedCellEditableOpts<R, SR>): boolean {
const column = columns[selectedPosition.idx];
const row = rows[selectedPosition.rowIdx];
if (column.rowGroup === true || isGroupRow(row)) return false;
const isCellEditable = onCheckCellIsEditable ? onCheckCellIsEditable({ row, column, ...selectedPosition }) : true;
return isCellEditable && canEdit<R, SR>(column, row);
return column.editor != null
&& !column.rowGroup
&& !isGroupRow(row)
&& (typeof column.editable === 'function' ? column.editable(row) : column.editable) !== false;
}

interface GetNextSelectedCellPositionOpts<R, SR> {
Expand Down