Skip to content

Commit

Permalink
feat: Add support to pass in mouseHandlers into IrisGrid (#1857)
Browse files Browse the repository at this point in the history
- Move the doubleClick behaviour from IrisGridDataSelectMouseHandler
into GridSelectionMouseHandler, so it's baked into Grid itself
(double-clicking a grid in the styleguide was exhibiting erroneous
behaviour)
- Allow passing in mouseHandlers to IrisGrid. Will be necessary to
support deephaven/deephaven-plugins#320
- Tested using my branch for `on_row_press` support on deephaven.ui:
deephaven/deephaven-plugins#346
- Verified Linker still worked as expected
  • Loading branch information
mofojed authored Mar 8, 2024
1 parent 026c101 commit acf32a6
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 16 deletions.
18 changes: 18 additions & 0 deletions packages/grid/src/mouse-handlers/GridSelectionMouseHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,24 @@ class GridSelectionMouseHandler extends GridMouseHandler {
return false;
}

// eslint-disable-next-line class-methods-use-this
onDoubleClick(
gridPoint: GridPoint,
grid: Grid,
event: GridMouseEvent
): EventHandlerResult {
const { column, row } = gridPoint;
if (row == null || column == null) {
return false;
}

grid.clearSelectedRanges();
grid.moveCursorToPosition(column, row);
grid.commitSelection();

return true;
}

onContextMenu(
gridPoint: GridPoint,
grid: Grid,
Expand Down
61 changes: 49 additions & 12 deletions packages/iris-grid/src/IrisGrid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ export interface IrisGridProps {
onCreateChart: (settings: ChartBuilderSettings, model: IrisGridModel) => void;
onColumnSelected: (column: DhType.Column) => void;
onError: (error: unknown) => void;
onDataSelected: (index: ModelIndex, map: Record<ColumnName, unknown>) => void;
onDataSelected: (index: ModelIndex, map: RowDataMap) => void;
onStateChange: (irisGridState: IrisGridState, gridState: GridState) => void;
onAdvancedSettingsChange: AdvancedSettingsMenuCallback;

Expand Down Expand Up @@ -341,6 +341,10 @@ export interface IrisGridProps {
canToggleSearch: boolean;

columnHeaderGroups?: readonly ColumnHeaderGroup[];

// Optional key and mouse handlers
keyHandlers: readonly KeyHandler[];
mouseHandlers: readonly GridMouseHandler[];
}

export interface IrisGridState {
Expand All @@ -349,8 +353,6 @@ export interface IrisGridState {
focusedFilterBarColumn: number | null;
metricCalculator: IrisGridMetricCalculator;
metrics?: GridMetrics;
keyHandlers: readonly KeyHandler[];
mouseHandlers: readonly GridMouseHandler[];

partitionConfig?: PartitionConfig;

Expand Down Expand Up @@ -519,6 +521,8 @@ class IrisGrid extends Component<IrisGridProps, IrisGridState> {
frozenColumns: null,
theme: null,
canToggleSearch: true,
mouseHandlers: EMPTY_ARRAY,
keyHandlers: EMPTY_ARRAY,
};

constructor(props: IrisGridProps) {
Expand Down Expand Up @@ -754,14 +758,15 @@ class IrisGrid extends Component<IrisGridProps, IrisGridState> {

this.tableUtils = new TableUtils(dh);

this.mouseHandlers = mouseHandlers;
this.keyHandlers = keyHandlers;

this.state = {
isFilterBarShown,
isSelectingPartition,
focusedFilterBarColumn: null,
metricCalculator,
metrics: undefined,
keyHandlers,
mouseHandlers,

partitionConfig:
partitionConfig ??
Expand Down Expand Up @@ -1032,6 +1037,10 @@ class IrisGrid extends Component<IrisGridProps, IrisGridState> {

tableUtils: TableUtils;

keyHandlers: readonly KeyHandler[];

mouseHandlers: readonly GridMouseHandler[];

get gridWrapper(): HTMLDivElement | null {
return this.grid?.canvasWrapper.current ?? null;
}
Expand Down Expand Up @@ -1394,6 +1403,26 @@ class IrisGrid extends Component<IrisGridProps, IrisGridState> {
{ max: 1 }
);

getCachedKeyHandlers = memoize((keyHandlers: readonly KeyHandler[]) =>
[...keyHandlers, ...this.keyHandlers].sort((a, b) => a.order - b.order)
);

getKeyHandlers(): readonly KeyHandler[] {
const { keyHandlers } = this.props;
return this.getCachedKeyHandlers(keyHandlers);
}

getCachedMouseHandlers = memoize(
(
mouseHandlers: readonly GridMouseHandler[]
): readonly GridMouseHandler[] => [...mouseHandlers, ...this.mouseHandlers]
);

getMouseHandlers(): readonly GridMouseHandler[] {
const { mouseHandlers } = this.props;
return this.getCachedMouseHandlers(mouseHandlers);
}

getValueForCell(
columnIndex: GridRangeIndex,
rowIndex: GridRangeIndex,
Expand Down Expand Up @@ -2723,9 +2752,20 @@ class IrisGrid extends Component<IrisGridProps, IrisGridState> {
}

/**
* Select all the data for a given row and notify listener
* Get the row data map for a given row and notifies the listener
*/
selectData(columnIndex: ModelIndex, rowIndex: ModelIndex): void {
const dataMap = this.getRowDataMap(rowIndex);
const { onDataSelected } = this.props;
onDataSelected(rowIndex, dataMap);
}

/**
* Get the data map for the given row
* @param rowIndex Row to get the data map for
* @returns Data map for the row
*/
getRowDataMap(rowIndex: ModelIndex): RowDataMap {
const { model } = this.props;
const { columns, groupedColumns } = model;
const dataMap: RowDataMap = {};
Expand All @@ -2747,8 +2787,7 @@ class IrisGrid extends Component<IrisGridProps, IrisGridState> {
visibleIndex,
};
}
const { onDataSelected } = this.props;
onDataSelected(rowIndex, dataMap);
return dataMap;
}

handleAdvancedFilterChange(
Expand Down Expand Up @@ -4049,8 +4088,6 @@ class IrisGrid extends Component<IrisGridProps, IrisGridState> {
loadingText,
loadingScrimProgress,
loadingSpinnerShown,
keyHandlers,
mouseHandlers,
shownColumnTooltip,
hoverAdvancedFilter,
shownAdvancedFilter,
Expand Down Expand Up @@ -4622,8 +4659,8 @@ class IrisGrid extends Component<IrisGridProps, IrisGridState> {
isStuckToRight={isStuckToRight}
metricCalculator={metricCalculator}
model={model}
keyHandlers={keyHandlers}
mouseHandlers={mouseHandlers}
keyHandlers={this.getKeyHandlers()}
mouseHandlers={this.getMouseHandlers()}
movedColumns={movedColumns}
movedRows={movedRows}
onError={this.handleGridError}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import type IrisGrid from '../IrisGrid';
*/
class IrisGridDataSelectMouseHandler extends GridMouseHandler {
constructor(irisGrid: IrisGrid) {
super();
super(880);

this.irisGrid = irisGrid;
}
Expand All @@ -27,9 +27,7 @@ class IrisGridDataSelectMouseHandler extends GridMouseHandler {

this.irisGrid.selectData(column, row);

grid.moveCursorToPosition(column, row);

return true;
return false;
}
}

Expand Down

0 comments on commit acf32a6

Please sign in to comment.