6pac / SlickGrid

A lightning fast JavaScript grid/spreadsheet
https://stackblitz.com/github/6pac/SlickGrid/tree/master/vite-demo
MIT License
1.84k stars 423 forks source link

fix: `getCellFromPoint()` should return row/cell -1 outside grid canvas, fix #961 #968

Closed ghiscoding closed 9 months ago

ghiscoding commented 9 months ago

the bulk of the unit tests I have in my lib for this change are the following tests, which are more realistic than before. So any values outside the grid canvas will return -1 regardless of its position outside the canvas

it('should return correct row/cell when providing x,y coordinates', () => {
  grid = new SlickGrid<any, Column>(container, data, columns, { ...defaultOptions, enableCellNavigation: true });

  const result1 = grid.getCellFromPoint(0, 0);
  const result2 = grid.getCellFromPoint(DEFAULT_COLUMN_WIDTH + 5, DEFAULT_COLUMN_HEIGHT + 5);
  const result3 = grid.getCellFromPoint((DEFAULT_COLUMN_WIDTH * 2) + 5, (DEFAULT_COLUMN_HEIGHT * 2) + 5);
  const result4 = grid.getCellFromPoint((DEFAULT_COLUMN_WIDTH * 3) + 5, (DEFAULT_COLUMN_HEIGHT * 3) + 5);
  const result5 = grid.getCellFromPoint((DEFAULT_COLUMN_WIDTH * 4) + 5, (DEFAULT_COLUMN_HEIGHT * 4) + 5);

  expect(result1).toEqual({ cell: 0, row: 0 });
  expect(result2).toEqual({ cell: 1, row: 1 });
  expect(result3).toEqual({ cell: 2, row: 2 });
  expect(result4).toEqual({ cell: 3, row: 3 });
  expect(result5).toEqual({ cell: 4, row: 4 });
});

it('should return negative row/cell when x,y coordinates is outside the grid canvas', () => {
  grid = new SlickGrid<any, Column>(container, data, columns, { ...defaultOptions, enableCellNavigation: true });

  const result1 = grid.getCellFromPoint(0, -999);
  const result2 = grid.getCellFromPoint(-777, 0);
  const result3 = grid.getCellFromPoint(-(DEFAULT_COLUMN_WIDTH + 5), -(DEFAULT_COLUMN_HEIGHT + 5));
  const result4 = grid.getCellFromPoint(-((DEFAULT_COLUMN_WIDTH * 2) + 5), -((DEFAULT_COLUMN_HEIGHT * 2) + 5));
  const result5 = grid.getCellFromPoint(-((DEFAULT_COLUMN_WIDTH * 3) + 5), -((DEFAULT_COLUMN_HEIGHT * 3) + 5));

  expect(result1).toEqual({ cell: 0, row: -1 });
  expect(result2).toEqual({ cell: -1, row: 0 });
  expect(result3).toEqual({ cell: -1, row: -1 });
  expect(result4).toEqual({ cell: -1, row: -1 });
  expect(result5).toEqual({ cell: -1, row: -1 });
});