Skip to content

Commit

Permalink
Fixed click event without selection to not run action (#440)
Browse files Browse the repository at this point in the history
* Fixed click event without selection to not run action

* fix tests

* fix test
  • Loading branch information
ota-meshi authored Feb 7, 2025
1 parent ae1d269 commit 7e5ce60
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 0 deletions.
7 changes: 7 additions & 0 deletions packages/cheetah-grid/src/js/columns/action/actionBind.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,13 @@ export function bindCellClickAction<T>(
if (!isTarget(e.col, e.row)) {
return;
}
const sel = grid.selection.select;
if (sel.col !== e.col || sel.row !== e.row) {
// If the user drags from another element in the grid and then clicks,
// this can lead to unexpected behavior because there is no selection event.
// A guard avoids this issue.
return;
}
if (isPromise(grid.getRowRecord(e.row))) {
return;
}
Expand Down
7 changes: 7 additions & 0 deletions packages/cheetah-grid/src/js/header/action/actionBind.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ export function bindCellClickAction<T>(
if (!isTarget(e.col, e.row)) {
return;
}
const sel = grid.selection.select;
if (sel.col !== e.col || sel.row !== e.row) {
// If the user drags from another element in the grid and then clicks,
// this can lead to unexpected behavior because there is no selection event.
// A guard avoids this issue.
return;
}
action({
col: e.col,
row: e.row,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,11 +122,17 @@
});

it('toggle', function(done) {
grid.selection.select = {col: 0, row: 1};
grid.fireListeners(cheetahGrid.ListGrid.EVENT_TYPE.CLICK_CELL, {col: 0, row: 1});
grid.selection.select = {col: 1, row: 1};
grid.fireListeners(cheetahGrid.ListGrid.EVENT_TYPE.CLICK_CELL, {col: 1, row: 1});
grid.selection.select = {col: 2, row: 1};
grid.fireListeners(cheetahGrid.ListGrid.EVENT_TYPE.CLICK_CELL, {col: 2, row: 1});
grid.selection.select = {col: 3, row: 1};
grid.fireListeners(cheetahGrid.ListGrid.EVENT_TYPE.CLICK_CELL, {col: 3, row: 1});
grid.selection.select = {col: 4, row: 1};
grid.fireListeners(cheetahGrid.ListGrid.EVENT_TYPE.CLICK_CELL, {col: 4, row: 1});
grid.selection.select = {col: 0, row: 0};

expect(records[0]).toEqual({bool: true, str: 'true', onoff: 'on', num: 1, numstr: '01'});

Expand Down Expand Up @@ -286,11 +292,17 @@
it('checkBgColor', function(done) {
action.readOnly = false;
style.checkBgColor = '#0f0';
grid.selection.select = {col: 0, row: 1};
grid.fireListeners(cheetahGrid.ListGrid.EVENT_TYPE.CLICK_CELL, {col: 0, row: 1});
grid.selection.select = {col: 1, row: 1};
grid.fireListeners(cheetahGrid.ListGrid.EVENT_TYPE.CLICK_CELL, {col: 1, row: 1});
grid.selection.select = {col: 2, row: 1};
grid.fireListeners(cheetahGrid.ListGrid.EVENT_TYPE.CLICK_CELL, {col: 2, row: 1});
grid.selection.select = {col: 3, row: 1};
grid.fireListeners(cheetahGrid.ListGrid.EVENT_TYPE.CLICK_CELL, {col: 3, row: 1});
grid.selection.select = {col: 4, row: 1};
grid.fireListeners(cheetahGrid.ListGrid.EVENT_TYPE.CLICK_CELL, {col: 4, row: 1});
grid.selection.select = {col: 0, row: 0};


setTimeout(function() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,17 @@
});

it('toggle', function(done) {
grid.selection.select = {col: 0, row: 1};
grid.fireListeners(cheetahGrid.ListGrid.EVENT_TYPE.CLICK_CELL, {col: 0, row: 1});
grid.selection.select = {col: 1, row: 1};
grid.fireListeners(cheetahGrid.ListGrid.EVENT_TYPE.CLICK_CELL, {col: 1, row: 1});
grid.selection.select = {col: 2, row: 1};
grid.fireListeners(cheetahGrid.ListGrid.EVENT_TYPE.CLICK_CELL, {col: 2, row: 1});
grid.selection.select = {col: 3, row: 1};
grid.fireListeners(cheetahGrid.ListGrid.EVENT_TYPE.CLICK_CELL, {col: 3, row: 1});
grid.selection.select = {col: 4, row: 1};
grid.fireListeners(cheetahGrid.ListGrid.EVENT_TYPE.CLICK_CELL, {col: 4, row: 1});
grid.selection.select = {col: 0, row: 0};

expect(records[0]).toEqual({bool: true, str: 'true', onoff: 'on', num: 1, numstr: '01'});

Expand Down

0 comments on commit 7e5ce60

Please sign in to comment.