Skip to content

Use mouse down event to select cell #3774

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

Merged
merged 19 commits into from
May 8, 2025
Merged

Use mouse down event to select cell #3774

merged 19 commits into from
May 8, 2025

Conversation

amanmahajan7
Copy link
Contributor

@amanmahajan7 amanmahajan7 commented Apr 27, 2025

I checked a few popular grids and all select the cell on pointer down but they do not scroll the cell into view which we do. Not sure which behavior is better

🆕 onCellMouseDown event

@amanmahajan7 amanmahajan7 self-assigned this Apr 27, 2025
Copy link

codecov bot commented May 1, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 98.68%. Comparing base (1cff0fc) to head (3e43814).
Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3774      +/-   ##
==========================================
- Coverage   98.79%   98.68%   -0.11%     
==========================================
  Files          46       46              
  Lines        3475     3497      +22     
  Branches      762      763       +1     
==========================================
+ Hits         3433     3451      +18     
- Misses         42       46       +4     
Files with missing lines Coverage Δ
src/Cell.tsx 100.00% <100.00%> (ø)
src/DataGrid.tsx 99.59% <100.00%> (-0.01%) ⬇️
src/GroupCell.tsx 100.00% <100.00%> (ø)
src/GroupRow.tsx 100.00% <100.00%> (ø)
src/GroupedColumnHeaderCell.tsx 94.44% <100.00%> (ø)
src/HeaderCell.tsx 97.53% <100.00%> (+0.03%) ⬆️
src/Row.tsx 100.00% <100.00%> (ø)
src/SummaryCell.tsx 100.00% <100.00%> (ø)
src/TreeDataGrid.tsx 95.98% <100.00%> (+0.02%) ⬆️
src/index.ts 100.00% <ø> (ø)
... and 1 more

... and 1 file with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

}
selectCellWrapper();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

onClick and onContextMenu events do not have any default behavior now but we are keeping the same signature as users may want to edit on single click or right click

@@ -42,12 +42,14 @@ import type {
CalculatedColumn,
CellClickArgs,
CellClipboardEvent,
CellCopyEvent,
CellCopyArgs,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Renamed to match other types

src/DataGrid.tsx Outdated
Comment on lines 502 to 504
const onCellPointerDownLatest = useLatestFunc(onCellPointerDown);
const onCellClickLatest = useLatestFunc(onCellClick);
const onCellDoubleClickLatest = useLatestFunc(onCellDoubleClick);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we should remove useLatestFunc on these handlers and add a note on using useCallback in the readme. These events are probably not used often

@@ -17,39 +17,41 @@ export { default as renderHeaderCell } from './renderHeaderCell';
export { renderSortIcon, renderSortPriority } from './sortStatus';
export { useRowSelection, useHeaderRowSelection } from './hooks';
export type {
CalculatedColumn,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorted

@@ -294,6 +294,9 @@ function HeaderFilters() {
rows={filteredRows}
headerRowHeight={filters.enabled ? 70 : undefined}
direction={direction}
style={{
scrollbarGutter: 'stable'
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Too much layout shift when filtering

@@ -525,23 +533,17 @@ export function DataGrid<R, SR = unknown, K extends Key = Key>(props: DataGridPr
/**
* effects
*/
useLayoutEffect(() => {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We set shouldFocusCell so both effects were running when focus sink needs to be focused. I have combined them

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This effect didn't depend on shouldFocusCell, am I missing something?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This effect was not checking shouldFocusCell but it was set to true and we were running both effects. I think it is better to only focus when shouldFocusCell flag is true so there are no surprises

Copy link
Contributor

@nstepien nstepien May 2, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was it never running when shouldFocusCell was false?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't believe so

@amanmahajan7 amanmahajan7 marked this pull request as ready for review May 1, 2025 15:28
@amanmahajan7 amanmahajan7 requested a review from nstepien as a code owner May 1, 2025 15:28
@@ -525,23 +533,17 @@ export function DataGrid<R, SR = unknown, K extends Key = Key>(props: DataGridPr
/**
* effects
*/
useLayoutEffect(() => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This effect didn't depend on shouldFocusCell, am I missing something?

@@ -37,7 +37,7 @@ function SummaryCell<R, SR>({
typeof summaryCellClass === 'function' ? summaryCellClass(row) : summaryCellClass
);

function onClick() {
function onPointerDown() {
selectCell({ rowIdx, idx: column.idx });
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we should have unified event handlers at the DataGrid level instead of having the same implementations in various components.
Something like

function onCellEvent(event: any event, pointerArgs: which row/cell) {
  // if pointer down...
  // if click...
}

Copy link
Contributor Author

@amanmahajan7 amanmahajan7 May 2, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking the same when I added #3774 (comment). I will check

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried but it is getting little complex with group row and summary row event handlers. I will revisit

@amanmahajan7 amanmahajan7 marked this pull request as draft May 2, 2025 16:53
@amanmahajan7 amanmahajan7 changed the title Use pointer event to select cell Use mouse down event to select cell May 2, 2025
@amanmahajan7 amanmahajan7 marked this pull request as ready for review May 7, 2025 14:22
@amanmahajan7 amanmahajan7 requested a review from nstepien May 7, 2025 14:22
@amanmahajan7 amanmahajan7 merged commit 7b8a205 into main May 8, 2025
2 checks passed
@amanmahajan7 amanmahajan7 deleted the am-cell-selection branch May 8, 2025 13:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants