Skip to content
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

Feat: creating row with external button #903

Merged
Merged
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 @@ -98,15 +98,18 @@ export const MRT_EditActionButtons = <TData extends MRT_RowData>({
<CancelIcon />
</IconButton>
</Tooltip>
<Tooltip title={localization.save}>
<IconButton
aria-label={localization.save}
color="info"
onClick={handleSubmitRow}
>
{isSaving ? <CircularProgress size={18} /> : <SaveIcon />}
</IconButton>
</Tooltip>
{((isCreating && onCreatingRowSave) ||
(isEditing && onEditingRowSave)) && (
<Tooltip title={localization.save}>
<IconButton
aria-label={localization.save}
color="info"
onClick={handleSubmitRow}
>
{isSaving ? <CircularProgress size={18} /> : <SaveIcon />}
</IconButton>
</Tooltip>
)}
</>
) : (
<>
Expand Down
60 changes: 60 additions & 0 deletions packages/material-react-table/stories/features/Editing.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ export const EditingEnabledEditModeRow = () => {
data={tableData}
editDisplayMode="row"
enableEditing
onCreatingRowSave={() => {}}
onEditingRowSave={handleSaveRow}
renderTopToolbarCustomActions={({ table }) => (
<Button onClick={() => table.setCreatingRow(true)}>Add</Button>
Expand All @@ -186,6 +187,62 @@ export const EditingEnabledEditModeRow = () => {
);
};

export const EditingEnabledEditModeRowCustomSave = () => {
const [tableData, setTableData] = useState(data);

const handleSaveRow: MRT_TableOptions<Person>['onEditingRowSave'] = ({
exitEditingMode,
row,
values,
}) => {
tableData[row.index] = values;
setTableData([...tableData]);
exitEditingMode();
};

return (
<MaterialReactTable
columns={[
{
accessorKey: 'firstName',
header: 'First Name',
},
{
accessorKey: 'lastName',
header: 'Last Name',
},
{
accessorKey: 'address',
header: 'Address',
},
{
accessorKey: 'state',
header: 'State',
},
{
accessorKey: 'phoneNumber',
enableEditing: false,
header: 'Phone Number',
},
]}
createDisplayMode="row"
data={tableData}
editDisplayMode="row"
enableEditing
onEditingRowSave={handleSaveRow}
renderTopToolbarCustomActions={({ table }) =>
table.getState().creatingRow ? (
<Button color="success" onClick={() => {}}>
Save
</Button>
) : (
<Button onClick={() => table.setCreatingRow(true)}>Add</Button>
)
}
/>
);
};

export const EditingEnabledEditModeRowVirtualized = () => {
const [tableData, setTableData] = useState(data);

Expand Down Expand Up @@ -237,6 +294,7 @@ export const EditingEnabledEditModeRowVirtualized = () => {
enableRowSelection
enableRowVirtualization
muiTableContainerProps={{ sx: { height: 400 } }}
onCreatingRowSave={() => {}}
onEditingRowSave={handleSaveRow}
renderTopToolbarCustomActions={({ table }) => (
<Button onClick={() => table.setCreatingRow(true)}>Add</Button>
Expand Down Expand Up @@ -288,6 +346,7 @@ export const EditingEnabledEditModeCell = () => {
handleSaveCell(cell, event.target.value);
},
})}
onCreatingRowSave={() => {}}
renderTopToolbarCustomActions={({ table }) => (
<Button onClick={() => table.setCreatingRow(true)}>Add</Button>
)}
Expand Down Expand Up @@ -339,6 +398,7 @@ export const EditingEnabledEditModeCellWithRowActions = () => {
handleSaveCell(cell, event.target.value);
},
})}
onCreatingRowSave={() => {}}
renderTopToolbarCustomActions={({ table }) => (
<Button onClick={() => table.setCreatingRow(true)}>Add</Button>
)}
Expand Down
Loading