|
| 1 | +import { useState } from 'react'; |
| 2 | +import { |
| 3 | + DataGrid, |
| 4 | + exportToCsv, |
| 5 | + useGridApiRef, |
| 6 | + type GridColDef, |
| 7 | + type GridContextMenuEntry, |
| 8 | + type GridContextMenuParams, |
| 9 | +} from '@opencorestack/opengridx'; |
| 10 | +import { DocsLayout } from '../../components/DocsLayout'; |
| 11 | +import { CodeBlock } from '../../components/CodeBlock'; |
| 12 | +import '../PasteDemos.css'; |
| 13 | + |
| 14 | +import sourceCode from './ContextMenuDemo.tsx?raw'; |
| 15 | + |
| 16 | +interface Employee { |
| 17 | + id: number; |
| 18 | + name: string; |
| 19 | + department: string; |
| 20 | + salary: number; |
| 21 | + city: string; |
| 22 | +} |
| 23 | + |
| 24 | +const initialRows: Employee[] = [ |
| 25 | + { id: 1, name: 'Ada Lovelace', department: 'Engineering', salary: 128000, city: 'London' }, |
| 26 | + { id: 2, name: 'Grace Hopper', department: 'Engineering', salary: 134000, city: 'New York' }, |
| 27 | + { id: 3, name: 'Alan Turing', department: 'Research', salary: 121000, city: 'Manchester' }, |
| 28 | + { id: 4, name: 'Margaret Hamilton', department: 'Engineering', salary: 139000, city: 'Boston' }, |
| 29 | + { id: 5, name: 'Katherine Johnson', department: 'Research', salary: 117000, city: 'Hampton' }, |
| 30 | + { id: 6, name: 'Edsger Dijkstra', department: 'Research', salary: 125000, city: 'Austin' }, |
| 31 | + { id: 7, name: 'Barbara Liskov', department: 'Design', salary: 119000, city: 'Cambridge' }, |
| 32 | + { id: 8, name: 'Donald Knuth', department: 'Design', salary: 131000, city: 'Stanford' }, |
| 33 | + { id: 9, name: 'Radia Perlman', department: 'Support', salary: 98000, city: 'Seattle' }, |
| 34 | + { id: 10, name: 'Tim Berners-Lee', department: 'Support', salary: 102000, city: 'London' }, |
| 35 | +]; |
| 36 | + |
| 37 | +const columns: GridColDef<Employee>[] = [ |
| 38 | + { field: 'name', headerName: 'Name', width: 180, editable: true }, |
| 39 | + { field: 'department', headerName: 'Department', width: 140, editable: true }, |
| 40 | + { field: 'salary', headerName: 'Salary', type: 'number', width: 120, editable: true }, |
| 41 | + { field: 'city', headerName: 'City', width: 140, editable: true }, |
| 42 | +]; |
| 43 | + |
| 44 | +const snippet = `<DataGrid |
| 45 | + rows={rows} |
| 46 | + columns={columns} |
| 47 | + cellSelection // Copy then copies the selected range |
| 48 | + undoRedo // adds Undo and Redo to the menu |
| 49 | + contextMenu |
| 50 | + getContextMenuItems={(params, defaultItems) => [ |
| 51 | + ...defaultItems, |
| 52 | + 'divider', |
| 53 | + { id: 'export', label: 'Export CSV', |
| 54 | + onClick: () => exportToCsv(params.api.getAllFilteredRows(), columns) }, |
| 55 | + { id: 'delete', label: 'Delete row', danger: true, |
| 56 | + onClick: () => removeRow(params.id) }, |
| 57 | + ]} |
| 58 | +/> |
| 59 | +
|
| 60 | +// Return [] to show no menu (the browser menu then shows instead).`; |
| 61 | + |
| 62 | +export default function ContextMenuDemo() { |
| 63 | + const apiRef = useGridApiRef<Employee>(); |
| 64 | + const [rows, setRows] = useState(initialRows); |
| 65 | + const [log, setLog] = useState<string[]>([]); |
| 66 | + |
| 67 | + const note = (text: string) => setLog(prev => [text, ...prev].slice(0, 6)); |
| 68 | + |
| 69 | + const getContextMenuItems = ( |
| 70 | + params: GridContextMenuParams<Employee>, |
| 71 | + defaultItems: readonly GridContextMenuEntry[], |
| 72 | + ): GridContextMenuEntry[] => [ |
| 73 | + ...defaultItems, |
| 74 | + 'divider', |
| 75 | + { |
| 76 | + id: 'export', |
| 77 | + label: 'Export CSV', |
| 78 | + onClick: () => { |
| 79 | + exportToCsv(params.api.getAllFilteredRows(), columns, { fileName: 'employees.csv' }); |
| 80 | + note('Exported employees.csv'); |
| 81 | + }, |
| 82 | + }, |
| 83 | + { |
| 84 | + id: 'delete', |
| 85 | + label: `Delete ${params.row.name}`, |
| 86 | + danger: true, |
| 87 | + onClick: () => { |
| 88 | + setRows(prev => prev.filter(r => r.id !== params.id)); |
| 89 | + note(`Deleted row ${params.id} (${params.row.name})`); |
| 90 | + }, |
| 91 | + }, |
| 92 | + ]; |
| 93 | + |
| 94 | + return ( |
| 95 | + <DocsLayout |
| 96 | + title="Context Menu" |
| 97 | + description={ |
| 98 | + <> |
| 99 | + With <code>contextMenu</code> on, right-clicking a body cell (or pressing <kbd>Shift + F10</kbd>) |
| 100 | + opens a menu of cell actions: Copy, Copy with headers, and Undo / Redo when <code>undoRedo</code> is |
| 101 | + on. <code>getContextMenuItems</code> adds your own items, here Export CSV and a danger Delete row. |
| 102 | + </> |
| 103 | + } |
| 104 | + sourceCode={sourceCode} |
| 105 | + > |
| 106 | + <div className="paste-demo"> |
| 107 | + <section className="paste-demo__sample"> |
| 108 | + <strong>Try it</strong> |
| 109 | + <ul> |
| 110 | + <li>Drag over a few cells, then right-click inside the range: Copy copies the whole range and the range stays.</li> |
| 111 | + <li>Right-click a cell outside the range: the range collapses to that cell.</li> |
| 112 | + <li>Edit a cell, then right-click and choose Undo.</li> |
| 113 | + <li>Focus a cell with the arrow keys and press <kbd>Shift + F10</kbd>; <kbd>Esc</kbd> closes the menu and returns to the cell.</li> |
| 114 | + <li>Right-click a column header: the browser's own menu shows.</li> |
| 115 | + </ul> |
| 116 | + </section> |
| 117 | + |
| 118 | + <DataGrid<Employee> |
| 119 | + rows={rows} |
| 120 | + columns={columns} |
| 121 | + apiRef={apiRef} |
| 122 | + cellSelection |
| 123 | + undoRedo |
| 124 | + contextMenu |
| 125 | + getContextMenuItems={getContextMenuItems} |
| 126 | + processRowUpdate={(row) => { |
| 127 | + setRows(prev => prev.map(r => (r.id === row.id ? row : r))); |
| 128 | + return row; |
| 129 | + }} |
| 130 | + height={380} |
| 131 | + /> |
| 132 | + |
| 133 | + <section className="paste-demo__summary"> |
| 134 | + <strong>Actions (latest first)</strong> |
| 135 | + {log.length === 0 && <p>Choose Export CSV or Delete from the menu.</p>} |
| 136 | + <ol className="paste-demo__history"> |
| 137 | + {log.map((entry, i) => <li key={`${i}-${entry}`}>{entry}</li>)} |
| 138 | + </ol> |
| 139 | + </section> |
| 140 | + |
| 141 | + <CodeBlock code={snippet} /> |
| 142 | + </div> |
| 143 | + </DocsLayout> |
| 144 | + ); |
| 145 | +} |
0 commit comments