Skip to content

Commit 1a43d31

Browse files
authored
Remove column events (adazzle#1879)
* Remove column events * Cleanup SelectRowEvent * Remove usused rowKey
1 parent 588e22d commit 1a43d31

File tree

6 files changed

+16
-73
lines changed

6 files changed

+16
-73
lines changed

src/Canvas.tsx

+4-3
Original file line numberDiff line numberDiff line change
@@ -159,11 +159,12 @@ function Canvas<R, K extends keyof R>({
159159
useEffect(() => {
160160
if (!onSelectedRowsChange) return;
161161

162-
const handleRowSelectionChange = ({ rowIdx, row, checked, isShiftClick }: SelectRowEvent<R>) => {
162+
const handleRowSelectionChange = ({ rowIdx, checked, isShiftClick }: SelectRowEvent) => {
163163
const newSelectedRows = new Set(selectedRows);
164+
const rowId = rowGetter(rowIdx)[rowKey];
164165

165166
if (checked) {
166-
newSelectedRows.add(row[rowKey]);
167+
newSelectedRows.add(rowId);
167168
const previousRowIdx = lastSelectedRowIdx.current;
168169
lastSelectedRowIdx.current = rowIdx;
169170
if (isShiftClick && previousRowIdx !== -1 && previousRowIdx !== rowIdx) {
@@ -173,7 +174,7 @@ function Canvas<R, K extends keyof R>({
173174
}
174175
}
175176
} else {
176-
newSelectedRows.delete(row[rowKey]);
177+
newSelectedRows.delete(rowId);
177178
lastSelectedRowIdx.current = -1;
178179
}
179180

src/Cell.test.tsx

-2
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ const testProps: CellProps<Row> = {
2525
isRowSelected: false,
2626
scrollLeft: 0,
2727
isSummaryRow: false,
28-
rowKey: 'id',
2928
eventBus: new EventBus()
3029
};
3130

@@ -72,7 +71,6 @@ describe('Cell', () => {
7271
isRowSelected: false,
7372
scrollLeft: 0,
7473
isSummaryRow: false,
75-
rowKey: 'id',
7674
eventBus: new EventBus()
7775
};
7876

src/Cell.tsx

+9-52
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React, { memo, createElement } from 'react';
22
import classNames from 'classnames';
33

4-
import { CellRendererProps, ColumnEventInfo } from './common/types';
4+
import { CellRendererProps } from './common/types';
55
import { EventTypes } from './common/enums';
66

77
export interface CellProps<R> extends CellRendererProps<R> {
@@ -11,7 +11,6 @@ export interface CellProps<R> extends CellRendererProps<R> {
1111
}
1212

1313
function Cell<R>({
14-
rowKey,
1514
children,
1615
className,
1716
column,
@@ -57,63 +56,16 @@ function Cell<R>({
5756

5857
function handleCellDoubleClick(e: React.MouseEvent<HTMLDivElement>) {
5958
e.stopPropagation();
60-
onRowDoubleClick?.(rowIdx, rowData, column);
6159
selectCell(true);
60+
onRowDoubleClick?.(rowIdx, rowData, column);
6261
}
6362

6463
function handleDragOver(e: React.DragEvent<HTMLDivElement>) {
6564
e.preventDefault();
6665
}
6766

6867
function onRowSelectionChange(checked: boolean, isShiftClick: boolean) {
69-
eventBus.dispatch(EventTypes.SELECT_ROW, { rowIdx, row: rowData, checked, isShiftClick });
70-
}
71-
72-
function getEvents() {
73-
if (isSummaryRow) return null;
74-
75-
const columnEvents = column.events;
76-
const allEvents: { [key: string]: Function } = {
77-
onClick: handleCellClick,
78-
onDoubleClick: handleCellDoubleClick,
79-
onContextMenu: handleCellContextMenu,
80-
onDragOver: handleDragOver
81-
};
82-
83-
if (enableCellRangeSelection) {
84-
allEvents.onMouseDown = handleCellMouseDown;
85-
allEvents.onMouseEnter = handleCellMouseEnter;
86-
}
87-
88-
if (!columnEvents) {
89-
return allEvents;
90-
}
91-
92-
const eventInfo: ColumnEventInfo<R> = {
93-
idx,
94-
rowIdx,
95-
column,
96-
rowId: rowData[rowKey]
97-
};
98-
99-
for (const event in columnEvents) {
100-
const columnEventHandler = columnEvents[event];
101-
if (columnEventHandler) {
102-
if (allEvents.hasOwnProperty(event)) {
103-
const existingEvent = allEvents[event];
104-
allEvents[event] = (e: Event) => {
105-
existingEvent(e);
106-
columnEventHandler(e, eventInfo);
107-
};
108-
} else {
109-
allEvents[event] = (e: Event) => {
110-
columnEventHandler(e, eventInfo);
111-
};
112-
}
113-
}
114-
}
115-
116-
return allEvents;
68+
eventBus.dispatch(EventTypes.SELECT_ROW, { rowIdx, checked, isShiftClick });
11769
}
11870

11971
className = classNames(
@@ -149,7 +101,12 @@ function Cell<R>({
149101
<div
150102
className={className}
151103
style={style}
152-
{...getEvents()}
104+
onClick={isSummaryRow ? undefined : handleCellClick}
105+
onDoubleClick={isSummaryRow ? undefined : handleCellDoubleClick}
106+
onContextMenu={isSummaryRow ? undefined : handleCellContextMenu}
107+
onDragOver={isSummaryRow ? undefined : handleDragOver}
108+
onMouseDown={isSummaryRow || !enableCellRangeSelection ? undefined : handleCellMouseDown}
109+
onMouseEnter={isSummaryRow || !enableCellRangeSelection ? undefined : handleCellMouseEnter}
153110
>
154111
{children}
155112
</div>

src/EventBus.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { Position, SelectRowEvent } from './common/types';
22

33
interface EventMap {
44
SELECT_CELL(position: Position, enableEditor?: boolean): void;
5-
SELECT_ROW(event: SelectRowEvent<unknown>): void;
5+
SELECT_ROW(event: SelectRowEvent): void;
66
SELECT_START(position: Position): void;
77
SELECT_UPDATE(position: Position, isFromKeyboard?: boolean, callback?: () => void): void;
88
SELECT_END(): void;

src/Row.tsx

+1-3
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,10 @@ export default function Row<R>({
4141

4242
function getCells() {
4343
return viewportColumns.map(column => {
44-
const { key } = column;
4544
return (
4645
<CellRenderer
47-
key={key as string} // FIXME: fix key type
46+
key={column.key as string} // FIXME: fix key type
4847
idx={column.idx}
49-
rowKey={key}
5048
rowIdx={idx}
5149
column={column}
5250
lastFrozenColumnIndex={lastFrozenColumnIndex}

src/common/types.ts

+1-12
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,6 @@ interface ColumnValue<TRow, TField extends keyof TRow = keyof TRow> {
1313
/** Column width. If not specified, it will be determined automatically based on grid width and specified widths of other columns*/
1414
width?: number | string;
1515
cellClass?: string;
16-
/** By adding an event object with callbacks for the native react events you can bind events to a specific column. That will not break the default behaviour of the grid and will run only for the specified column */
17-
events?: {
18-
[key: string]: undefined | ((e: Event, info: ColumnEventInfo<TRow>) => void);
19-
};
2016
/** Formatter to be used to render the cell content */
2117
formatter?: React.ComponentType<FormatterProps<TRow>>;
2218
/** Enables cell editing. If set and no editor property specified, then a textinput will be used as the cell editor */
@@ -127,7 +123,6 @@ export interface HeaderRendererProps<TRow> {
127123
}
128124

129125
export interface CellRendererProps<TRow> {
130-
rowKey: keyof TRow;
131126
idx: number;
132127
rowIdx: number;
133128
column: CalculatedColumn<TRow>;
@@ -171,11 +166,6 @@ export interface FilterRendererProps<TRow, TFilterValue = unknown> {
171166
onChange(value: TFilterValue): void;
172167
}
173168

174-
export interface ColumnEventInfo<TRow> extends Position {
175-
rowId: unknown;
176-
column: CalculatedColumn<TRow>;
177-
}
178-
179169
export interface ScrollPosition {
180170
scrollLeft: number;
181171
scrollTop: number;
@@ -219,9 +209,8 @@ export interface CheckCellIsEditableEvent<TRow> extends Position {
219209
column: CalculatedColumn<TRow>;
220210
}
221211

222-
export interface SelectRowEvent<R> {
212+
export interface SelectRowEvent {
223213
rowIdx: number;
224-
row: R;
225214
checked: boolean;
226215
isShiftClick: boolean;
227216
}

0 commit comments

Comments
 (0)