Skip to content

Commit 8a02c5a

Browse files
committed
edit only creating row example
1 parent e20ffca commit 8a02c5a

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed

packages/material-react-table/stories/features/Creating.stories.tsx

+64
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@ import {
66
type MRT_TableOptions,
77
MaterialReactTable,
88
createRow,
9+
MRT_Row,
910
} from '../../src';
1011
import { faker } from '@faker-js/faker';
1112
import { type Meta } from '@storybook/react';
13+
import { MenuItem, Select } from '@mui/material';
1214

1315
const meta: Meta = {
1416
title: 'Features/Creating Examples',
@@ -338,3 +340,65 @@ export const CreateRowIndexIndexExpanding = () => {
338340
/>
339341
);
340342
};
343+
344+
export const CreateWithCustomEditCell = () => {
345+
const [tableData, setTableData] = useState(data);
346+
347+
const handleSaveRow: MRT_TableOptions<Person>['onEditingRowSave'] = ({
348+
exitEditingMode,
349+
row,
350+
values,
351+
}) => {
352+
tableData[row.index] = values;
353+
setTableData([...tableData]);
354+
exitEditingMode();
355+
};
356+
357+
const [creatingRow, setCreatingRow] = useState<MRT_Row<Person> | null>(null);
358+
359+
return (
360+
<MaterialReactTable
361+
columns={[
362+
{
363+
accessorKey: 'firstName',
364+
header: 'First Name',
365+
},
366+
{
367+
accessorKey: 'lastName',
368+
header: 'Last Name',
369+
},
370+
{
371+
accessorKey: 'address',
372+
header: 'Address',
373+
},
374+
{
375+
accessorKey: 'state',
376+
header: 'State',
377+
Edit: ({ cell }) => (
378+
<Select value={cell.getValue<string>()}>
379+
<MenuItem value="Alabama">Alabama</MenuItem>
380+
<MenuItem value="Alaska">Alaska</MenuItem>
381+
</Select>
382+
),
383+
},
384+
{
385+
accessorKey: 'phoneNumber',
386+
enableEditing: false,
387+
header: 'Phone Number',
388+
},
389+
]}
390+
state={{ creatingRow }}
391+
onCreatingRowChange={setCreatingRow}
392+
createDisplayMode="row"
393+
data={tableData}
394+
editDisplayMode="row"
395+
enableEditing={(row) => row.id === creatingRow?.id}
396+
onCreatingRowSave={() => {}}
397+
onEditingRowSave={handleSaveRow}
398+
positionCreatingRow="top"
399+
renderTopToolbarCustomActions={({ table }) => (
400+
<Button onClick={() => table.setCreatingRow(true)}>Add</Button>
401+
)}
402+
/>
403+
);
404+
};

0 commit comments

Comments
 (0)