-
Notifications
You must be signed in to change notification settings - Fork 341
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 | ||||||||||||
// 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This line mutates the existing
Suggested change
Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||||||||||||
} | ||||||||||||
} | ||||||||||||
}, | ||||||||||||
[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} />; | ||||||||||||
} |
There was a problem hiding this comment.
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 validatingcol_id
againstcolumns.length
or indexing directly with a proper guard to ensure you always retrieve a validGridColumn
.Copilot uses AI. Check for mistakes.