Skip to content

fix: Number kind grid cell edit behavior update for ux improvement #1001

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ const NumberOverlayEditor: React.FunctionComponent<Props> = p => {
allowNegative={allowNegative}
thousandSeparator={thousandSeparator ?? getThousandSeprator()}
decimalSeparator={decimalSeparator ?? getDecimalSeparator()}
value={Object.is(value, -0) ? "-" : value ?? ""}
defaultValue={Object.is(value, -0) ? "-" : value ?? ""}
// decimalScale={3}
// prefix={"$"}
onValueChange={onChange}
Expand Down
1 change: 1 addition & 0 deletions test-projects/cra5-gdg/public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
<div id="root"></div>
<div id="portal" />
<!--
This HTML file is a template.
If you open it directly in the browser, you will see an empty page.
Expand Down
10 changes: 1 addition & 9 deletions test-projects/cra5-gdg/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,10 @@
import React from "react";
import logo from "./logo.svg";
import "./App.css";
import Grid from "./components/Grid";

function App() {
return (
<div className="App">
<header className="App-header">
<img src={logo} className="App-logo" alt="logo" />
<Grid />
<a className="App-link" href="https://reactjs.org" target="_blank" rel="noopener noreferrer">
Learn React
</a>
</header>
<Grid />
</div>
);
}
Expand Down
121 changes: 93 additions & 28 deletions test-projects/cra5-gdg/src/components/Grid.tsx
Original file line number Diff line number Diff line change
@@ -1,36 +1,101 @@
import * as React from "react";
import DataEditor, { DataEditorProps, GridCellKind, GridColumn } from "@glideapps/glide-data-grid";
import { useCallback, useState } from "react";
import _ from "lodash";
import "@glideapps/glide-data-grid/dist/index.css";

import { DataEditor, GridCell, GridCellKind, GridColumn, Item } from "@glideapps/glide-data-grid";

// Define the shape of the data
interface Person {
firstName: string;
lastName: string;
id: number;
}

const data: Person[] = [
{
firstName: "John",
lastName: "Doe",
id: 100002,
},
{
firstName: "Maria",
lastName: "Garcia",
id: 100002,
},
{
firstName: "Nancy",
lastName: "Jones",
id: 100002,
},
{
firstName: "James",
lastName: "Smith",
id: 100002,
},
];

// Grid columns may also provide icon, overlayIcon, menu, style, and theme overrides
const columns: GridColumn[] = [
{ title: "First Name", width: 100, id: "firstName" },
{ title: "Last Name", width: 100, id: "lastName" },
{ title: "Id", width: 100, id: "id" },
];

export default function Grid() {
const getData = React.useCallback<DataEditorProps["getCellContent"]>(
cell => ({
kind: GridCellKind.Text,
allowOverlay: true,
readonly: true,
data: `${cell[0]},${cell[1]}`,
displayData: `${cell[0]},${cell[1]}`,
}),
[]
const [rows, setRows] = useState<Person[]>(data);

const onCellEdited = useCallback(
(cell: readonly [number, number], newValue: GridCell) => {
const [col_id, row_id] = cell;
const column = _.get(columns, col_id, {}) as GridColumn; // Type assertion here
Copy link
Preview

Copilot AI Jun 17, 2025

Choose a reason for hiding this comment

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

[nitpick] Using _.get with a fallback of {} can hide out-of-bounds column indices. Consider validating col_id against columns.length or indexing directly with a proper guard to ensure you always retrieve a valid GridColumn.

Suggested change
const column = _.get(columns, col_id, {}) as GridColumn; // Type assertion here
if (col_id < 0 || col_id >= columns.length) {
throw new Error(`Invalid column index: ${col_id}`);
}
const column = columns[col_id];

Copilot uses AI. Check for mistakes.

// Check if newValue is of type GridCell and has a data property
if (newValue.kind === GridCellKind.Text || newValue.kind === GridCellKind.Number) {
const value = newValue.data;

if (column.id && value !== undefined) {
const key = column.id as keyof Person; // Type assertion
rows[row_id][key] = value as never; // Use `as never` to bypass assignment restrictions
setRows([...rows]);
Comment on lines +57 to +58
Copy link
Preview

Copilot AI Jun 17, 2025

Choose a reason for hiding this comment

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

This line mutates the existing rows state directly and bypasses TypeScript safety with a never cast. Instead, perform an immutable update by copying the specific row object and using setRows with the new array, casting value to the correct type (string or number).

Suggested change
rows[row_id][key] = value as never; // Use `as never` to bypass assignment restrictions
setRows([...rows]);
const updatedRow = { ...rows[row_id], [key]: value as string | number }; // Create a new row object with the updated value
const updatedRows = rows.map((row, index) => (index === row_id ? updatedRow : row)); // Replace the old row with the updated row
setRows(updatedRows); // Update the state with the new array

Copilot uses AI. Check for mistakes.

}
}
},
[rows]
);

const cols = React.useMemo<GridColumn[]>(
() => [
{
width: 100,
title: "A",
},
{
width: 100,
title: "B",
},
{
width: 100,
title: "C",
},
],
[]
const onCellData = useCallback(
function getData([col, row]: Item): GridCell {
const person = rows[row];

if (col === 0) {
return {
kind: GridCellKind.Text,
data: person.firstName,
allowOverlay: false,
readonly: false,
displayData: person.firstName,
};
} else if (col === 1) {
return {
kind: GridCellKind.Text,
data: person.lastName,
allowOverlay: false,
displayData: person.lastName,
readonly: true,
};
} else if (col === 2) {
return {
kind: GridCellKind.Number,
data: person.id,
allowOverlay: true,
displayData: person.id.toString(),
readonly: false,
};
} else {
throw new Error("Invalid column index");
}
},
[rows]
);

return <DataEditor width={800} height={500} getCellContent={getData} columns={cols} rows={100} />;
return <DataEditor columns={columns} onCellEdited={onCellEdited} getCellContent={onCellData} rows={rows.length} />;
}