Skip to content
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

[DataGrid] Backspace stopped working in inputs in cells #1300

Closed
2 tasks done
piotrdytkowski opened this issue Mar 26, 2021 · 6 comments
Closed
2 tasks done

[DataGrid] Backspace stopped working in inputs in cells #1300

piotrdytkowski opened this issue Mar 26, 2021 · 6 comments
Assignees
Labels
bug 🐛 Something doesn't work component: data grid This is the name of the generic UI component, not the React module!

Comments

@piotrdytkowski
Copy link

I have an Input component inside a cell, I can type text in it, but pressing backspace freezes the input.
This was working in version 4.0.0-alpha.22, stopped working in 4.0.0-alpha.23.

  • The issue is present in the latest release.
  • I have searched the issues of this repository and believe that this is not a duplicate.

Current Behavior 😯

Pressing backspace in an input freezes it

Expected Behavior 🤔

Pressing backspace in an input should remove last character in an input.

Steps to Reproduce 🕹

Steps:

  1. Focus in some input.
  2. Type some text
  3. Hit backspace
    Sandbox : https://codesandbox.io/s/data-grid-with-broken-input-fw5dx

To see expected behaviour change data grid version to 4.0.0-alpha.22 and repeat steps.

Your Environment 🌎

System:
OS: Linux 5.4 Ubuntu 18.04.5 LTS (Bionic Beaver)
Binaries:
Node: 12.21.0 - /usr/bin/node
Yarn: 1.22.5 - /usr/bin/yarn
npm: 6.14.11 - /usr/bin/npm
Browsers:
Chrome: 89.0.4389.82
Firefox: 86.0.1
npmPackages:
@material-ui/core: 4.11.3 => 4.11.3
@material-ui/data-grid: 4.0.0-alpha.23 => 4.0.0-alpha.23
@material-ui/styles: 4.11.3
@material-ui/system: 4.11.3
@material-ui/types: 5.1.0
@material-ui/utils: 4.11.2
@types/react: ^16.9.35 => 16.14.4
react: ^16.13.1 => 16.14.0
react-dom: ^16.13.1 => 16.14.0
styled-components: ^5.1.1 => 5.2.1
typescript: 4.0.5 => 4.0.5

Order id 💳

@piotrdytkowski piotrdytkowski added the status: waiting for maintainer These issues haven't been looked at yet by a maintainer label Mar 26, 2021
@dtassone dtassone added bug 🐛 Something doesn't work and removed status: waiting for maintainer These issues haven't been looked at yet by a maintainer labels Mar 26, 2021
@oliviertassinari
Copy link
Member

oliviertassinari commented Mar 27, 2021

Backspace is not the only regression with cells in read mode and containing a textbox. See this one too. It's basically impossible to focus something inside any cells as soon as one cell is focused. It's not a light regression:

focus

It was reported in #1295 too.

@oliviertassinari oliviertassinari added the component: data grid This is the name of the generic UI component, not the React module! label Mar 27, 2021
@oliviertassinari
Copy link
Member

oliviertassinari commented Mar 27, 2021

I have noticed a couple of bugs with the logic. It seems that we have a couple of new e2e tests to add :D.

  1. First, if the cell is not editable, it doesn't make sense to handle the GRID_CELL_EXIT_EDIT event:
diff --git a/packages/grid/_modules_/grid/hooks/features/rows/useGridEditRows.ts b/packages/grid/_modules_/grid/hooks/features/rows/useGridEditRows.
ts
index 4f31e22f..6299c7bf 100644
--- a/packages/grid/_modules_/grid/hooks/features/rows/useGridEditRows.ts
+++ b/packages/grid/_modules_/grid/hooks/features/rows/useGridEditRows.ts
@@ -208,6 +208,10 @@ export function useGridEditRows(apiRef: GridApiRef) {

   const handleExitEdit = React.useCallback(
     (params: GridCellParams, event: React.SyntheticEvent) => {
+      if (!params.isEditable) {
+        return;
+      }
+
       setCellMode(params.id, params.field, 'view');
  1. Second, firing GRID_CELL_EXIT_EDIT on a Backspace on a nested input is simply wrong. I propose we go even further, ignoring all events:
diff --git a/packages/grid/_modules_/grid/hooks/features/rows/useGridEditRows.ts b/packages/grid/_modules_/grid/hooks/features/rows/useGridEditRows.
ts
index 4f31e22f..392d7dee 100644
--- a/packages/grid/_modules_/grid/hooks/features/rows/useGridEditRows.ts
+++ b/packages/grid/_modules_/grid/hooks/features/rows/useGridEditRows.ts
@@ -13,6 +13,7 @@ import {
   GRID_CELL_KEYDOWN,
   GRID_CELL_VALUE_CHANGE,
 } from '../../../constants/eventsConstants';
+import { isGridCellRoot } from '../../../utils/domUtils';
 import { GridApiRef } from '../../../models/api/gridApiRef';
 import { GridEditRowApi } from '../../../models/api/gridEditRowApi';
 import { GridCellMode } from '../../../models/gridCell';
@@ -265,12 +266,13 @@ export function useGridEditRows(apiRef: GridApiRef) {

   const handleCellKeyDown = React.useCallback(
     (params: GridCellParams, event) => {
+      const fromCellRoot = isGridCellRoot(event.target)
       const isEditMode = params.cellMode === 'edit';

-      if (!isEditMode && isCellEnterEditModeKeys(event.key)) {
+      if (!isEditMode && fromCellRoot && isCellEnterEditModeKeys(event.key)) {
         apiRef.current.publishEvent(GRID_CELL_ENTER_EDIT, params, event);
       }
-      if (!isEditMode && isDeleteKeys(event.key)) {
+      if (!isEditMode && fromCellRoot && isDeleteKeys(event.key)) {
         const commitParams: GridEditCellPropsParams = apiRef.current.getEditCellPropsParams(
           params.id,
           params.field,
  1. Third, I noticed that we can hedge for future iframe, popup support. ⚠ there are multiple document:
diff --git a/packages/grid/_modules_/grid/hooks/features/keyboard/useGridKeyboard.ts b/packages/grid/_modules_/grid/hooks/features/keyboard/useGridK
eyboard.ts
index 40feeccb..41fd49e8 100644
--- a/packages/grid/_modules_/grid/hooks/features/keyboard/useGridKeyboard.ts
+++ b/packages/grid/_modules_/grid/hooks/features/keyboard/useGridKeyboard.ts
@@ -150,7 +150,7 @@ export const useGridKeyboard = (

   const handleCellKeyDown = React.useCallback(
     (params: GridCellParams, event: React.KeyboardEvent) => {
-      if (!isGridCellRoot(document.activeElement)) {
+      if (!isGridCellRoot(event.target as Element)) {
         return;
       }
  1. Fourth, the event name is wrong:
diff --git a/packages/grid/_modules_/grid/constants/eventsConstants.ts b/packages/grid/_modules_/grid/constants/eventsConstants.ts
index 0d2a6634..8f4ddb6d 100644
--- a/packages/grid/_modules_/grid/constants/eventsConstants.ts
+++ b/packages/grid/_modules_/grid/constants/eventsConstants.ts
@@ -28,7 +28,7 @@ export const GRID_CELL_VALUE_CHANGE = 'cellValueChange';
 export const GRID_CELL_ENTER_EDIT = 'cellEnterEdit';
 export const GRID_CELL_EXIT_EDIT = 'cellExitEdit';
 export const GRID_CELL_NAVIGATION_KEYDOWN = 'cellNavigationKeyDown';
-export const GRID_CELL_FOCUS = 'cellCellFocus';
+export const GRID_CELL_FOCUS = 'cellFocus';

 export const GRID_ROW_CLICK = 'rowClick';
 export const GRID_ROW_DOUBLE_CLICK = 'rowDoubleClick';
  1. Lastly, for the click issue, it's the one I spent the most time on. This commit is wrong 5169c94. I didn't look into a solution. But I already left a comment about it, so it's not a surprise: 5169c94#r596918212.

Screenshot 2021-03-27 at 15 33 33

@ZeeshanTamboli
Copy link
Member

This works with latest version 4.0.0-alpha.27- https://codesandbox.io/s/data-grid-with-broken-input-forked-4qudh. So perhaps we can close this.

@oliviertassinari
Copy link
Member

@ZeeshanTamboli Before we close, do you want to take care of the small side bugs found in #1300 (comment)? :)

@ZeeshanTamboli
Copy link
Member

@ZeeshanTamboli Before we close, do you want to take care of the small side bugs found in #1300 (comment)? :)

I could but I am not understanding what those are. Writing/fixing code that I don't understand is not good.
Here are some:

First, if the cell is not editable, it doesn't make sense to handle the GRID_CELL_EXIT_EDIT event:

Why would we want to check isEditable here if the exit event won't even get triggered in the first place if the cell is not editable and which won't go into edit state?

Second, firing GRID_CELL_EXIT_EDIT on a Backspace on a nested input is simply wrong. I propose we go even further, ignoring all events:

Isn't it a view mode, !isEditMode?

4th can be fixed by renaming event.

3rd and 5th I did not understand.

@oliviertassinari
Copy link
Member

@ZeeshanTamboli

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug 🐛 Something doesn't work component: data grid This is the name of the generic UI component, not the React module!
Projects
None yet
Development

No branches or pull requests

4 participants