Skip to content

Commit 6e97f76

Browse files
Copilothotlong
andcommitted
Add documentation and examples for row editing feature
Co-authored-by: hotlong <50353452+hotlong@users.noreply.github.com>
1 parent f5dc754 commit 6e97f76

4 files changed

Lines changed: 140 additions & 0 deletions

File tree

content/docs/plugins/plugin-grid.mdx

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,41 @@ Add action buttons to each row:
228228
}
229229
```
230230

231+
### Inline Editing
232+
233+
Enable inline cell editing for quick data updates:
234+
235+
```json
236+
{
237+
"type": "object-grid",
238+
"objectName": "users",
239+
"columns": [
240+
{ "header": "ID", "accessorKey": "id", "editable": false },
241+
{ "header": "Name", "accessorKey": "name" },
242+
{ "header": "Email", "accessorKey": "email" },
243+
{ "header": "Status", "accessorKey": "status" }
244+
],
245+
"editable": true,
246+
"onCellChange": "handleCellChange"
247+
}
248+
```
249+
250+
**Features:**
251+
- Double-click or press Enter on a cell to start editing
252+
- Press Enter to save changes
253+
- Press Escape to cancel editing
254+
- Column-level control: Set `editable: false` on specific columns to prevent editing
255+
- Callback support: Use `onCellChange` to handle cell value updates
256+
257+
**Example Handler:**
258+
```javascript
259+
function handleCellChange(rowIndex, columnKey, newValue, row) {
260+
console.log(`Cell at row ${rowIndex}, column ${columnKey} changed to:`, newValue);
261+
console.log('Full row data:', row);
262+
// Update your data source here
263+
}
264+
```
265+
231266
## TypeScript Support
232267

233268
```plaintext

packages/components/src/stories-json/data-table.stories.tsx

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,31 @@ export const FullFeatures: Story = {
5858
},
5959
render: (args) => <SchemaRenderer schema={args} />
6060
};
61+
62+
export const EditableTable: Story = {
63+
args: {
64+
type: 'data-table',
65+
caption: 'Editable Product Inventory',
66+
searchable: false,
67+
pagination: false,
68+
editable: true,
69+
columns: [
70+
{ header: 'SKU', accessorKey: 'sku', width: '100px', editable: false },
71+
{ header: 'Product Name', accessorKey: 'name' },
72+
{ header: 'Price', accessorKey: 'price' },
73+
{ header: 'Stock', accessorKey: 'stock' }
74+
],
75+
data: [
76+
{ sku: 'PROD-001', name: 'Laptop', price: '$1299.99', stock: 15 },
77+
{ sku: 'PROD-002', name: 'Mouse', price: '$29.99', stock: 120 },
78+
{ sku: 'PROD-003', name: 'Keyboard', price: '$79.99', stock: 45 },
79+
{ sku: 'PROD-004', name: 'Monitor', price: '$399.99', stock: 22 }
80+
],
81+
onCellChange: (rowIndex: number, columnKey: string, newValue: any, row: any) => {
82+
console.log('Cell edited:', { rowIndex, columnKey, newValue, row });
83+
alert(`Updated ${columnKey} to "${newValue}" for ${row.name}`);
84+
}
85+
},
86+
render: (args) => <SchemaRenderer schema={args} />
87+
};
88+

packages/components/src/stories-json/object-grid.stories.tsx

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,3 +155,40 @@ export const OpportunitiesGrid: Story = {
155155
className: 'w-full'
156156
} as any,
157157
};
158+
159+
/**
160+
* Editable Grid - Inline cell editing example
161+
*
162+
* This story demonstrates inline editing capabilities:
163+
* - Double-click or press Enter to edit a cell
164+
* - Press Enter to save, Escape to cancel
165+
* - ID column is read-only (editable: false)
166+
*/
167+
export const EditableGrid: Story = {
168+
render: renderStory,
169+
args: {
170+
type: 'object-grid',
171+
objectName: 'User',
172+
columns: [
173+
{ field: 'id', header: 'ID', width: 80, editable: false },
174+
{ field: 'name', header: 'Name', sortable: true },
175+
{ field: 'email', header: 'Email', sortable: true },
176+
{ field: 'role', header: 'Role', sortable: true },
177+
{ field: 'status', header: 'Status', sortable: true }
178+
],
179+
data: [
180+
{ id: 1, name: 'John Doe', email: 'john@example.com', role: 'Admin', status: 'Active' },
181+
{ id: 2, name: 'Jane Smith', email: 'jane@example.com', role: 'User', status: 'Active' },
182+
{ id: 3, name: 'Bob Johnson', email: 'bob@example.com', role: 'User', status: 'Inactive' }
183+
],
184+
editable: true,
185+
pagination: false,
186+
className: 'w-full',
187+
onCellChange: (rowIndex: number, columnKey: string, newValue: any, row: any) => {
188+
console.log('Cell changed:', { rowIndex, columnKey, newValue, row });
189+
// In a real application, you would update your data source here
190+
// Example: await dataSource.update(row.id, { [columnKey]: newValue });
191+
}
192+
} as any,
193+
};
194+

packages/plugin-grid/README.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,46 @@ const schema = {
244244
};
245245
```
246246

247+
### Inline Editing
248+
249+
Enable inline cell editing for quick updates:
250+
251+
```typescript
252+
const schema = {
253+
type: 'object-grid',
254+
objectName: 'users',
255+
columns: [
256+
{ header: 'ID', accessorKey: 'id', editable: false }, // Read-only column
257+
{ header: 'Name', accessorKey: 'name' }, // Editable
258+
{ header: 'Email', accessorKey: 'email' }, // Editable
259+
{ header: 'Status', accessorKey: 'status' } // Editable
260+
],
261+
editable: true, // Enable editing globally
262+
onCellChange: (rowIndex, columnKey, newValue, row) => {
263+
console.log(`Cell at row ${rowIndex}, column ${columnKey} changed to:`, newValue);
264+
console.log('Full row data:', row);
265+
// Update your data source here
266+
// Example: await dataSource.update(row.id, { [columnKey]: newValue });
267+
}
268+
};
269+
```
270+
271+
**Inline Editing Features:**
272+
- **Double-click to edit**: Double-click any editable cell to enter edit mode
273+
- **Keyboard shortcuts**:
274+
- Press `Enter` on a focused cell to start editing
275+
- Press `Enter` while editing to save changes
276+
- Press `Escape` to cancel editing
277+
- **Column-level control**: Set `editable: false` on specific columns to prevent editing
278+
- **Visual feedback**: Editable cells show hover state to indicate they can be edited
279+
- **Automatic focus**: Input field is automatically focused and selected when entering edit mode
280+
281+
**Use Cases:**
282+
- Quick data corrections
283+
- Batch data entry
284+
- Spreadsheet-like editing experience
285+
- Real-time updates with backend synchronization
286+
247287
## TypeScript Support
248288

249289
```typescript

0 commit comments

Comments
 (0)