Skip to content

Commit

Permalink
Enable editing upon row click
Browse files Browse the repository at this point in the history
  • Loading branch information
klassm committed Aug 19, 2023
1 parent 7202d8c commit ea187ff
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 34 deletions.
55 changes: 37 additions & 18 deletions __tests__/demo/demo-components/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -290,29 +290,48 @@ export function BulkEditWithDetailPanel() {
}

export function EditableRow(props) {
const [isEditing, setIsEditing] = useState(false);
const [canEdit, setCanEdit] = useState(true);
const [clickRowToEdit, setClickRowToEdit] = useState(false);

return (
<div>
<p>I am the parent</p>
<button onClick={(e) => setIsEditing(!isEditing)}>
{isEditing ? 'Disable' : 'Enable'} Editing
</button>
<fieldset>
<div>
<input
type="checkbox"
id="rowEditing"
name="rowEditing"
checked={clickRowToEdit}
onChange={(event) => setClickRowToEdit(event.target.checked)}
/>
<label htmlFor="rowEditing">Click row to edit</label>
</div>
<div>
<input
type="checkbox"
id="canEdit"
name="canEdit"
checked={canEdit}
onChange={(event) => setCanEdit(event.target.checked)}
/>
<label htmlFor="canEdit">Enable editing</label>
</div>
</fieldset>

<MaterialTable
components={{
Row: (props) => {
if (isEditing) {
return <MTableEditRow {...props} mode={'update'} />;
} else {
return <MTableBodyRow {...props} />;
}
}
}}
key={`table_${canEdit}`}
onRowClick={
clickRowToEdit
? (event, data, openDetails, enableEditMode) => enableEditMode()
: undefined
}
editable={{
onRowUpdate: (newData, oldData) => {
console.log({ newData, oldData });
return new Promise((reject, resolve) => resolve());
}
onRowUpdate: canEdit
? (newData, oldData) => {
console.log({ newData, oldData });
return new Promise((reject, resolve) => resolve());
}
: undefined
}}
data={[
{ name: 'jack', id: 1 },
Expand Down
2 changes: 1 addition & 1 deletion __tests__/detailPanel.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ describe('Detailpanel render', () => {
screen.getByRole('cell', {
name: /one/i
});
const toggleButton = screen.getByRole('button', {
const toggleButton = await screen.findByRole('button', {
name: /detail panel visibility toggle/i
});

Expand Down
28 changes: 28 additions & 0 deletions __tests__/editRow.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,34 @@ describe('Edit Row Row Update', () => {
expect(onRowUpdateCancelled.mock.calls.length).toBe(1);
});

test('clicking a row to start the edit mode', () => {
const onRowUpdate = jest.fn();
const onRowUpdateCancelled = jest.fn();
render(
<MaterialTable
data={data}
columns={columns}
onRowClick={(event, data, openDetails, enableEditMode) =>
enableEditMode()
}
editable={{
onRowUpdate,
onRowUpdateCancelled
}}
/>
);
fireEvent.click(screen.getByText(data[0].id.toString()));

screen.getByTestId('check');
const cancelButton = screen.getByRole('button', {
name: /cancel/i
});
fireEvent.click(cancelButton);

expect(onRowUpdate.mock.calls.length).toBe(0);
expect(onRowUpdateCancelled.mock.calls.length).toBe(1);
});

test('edit a row', async () => {
const onRowUpdate = jest.fn(async () => {});
render(
Expand Down
26 changes: 17 additions & 9 deletions src/components/MTableBodyRow/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ function MTableBodyRow({ forwardedRef, ...props }) {
getFieldValue,
isTreeData,
onRowSelected,
onRowEditStarted,
onTreeExpandChanged,
onToggleDetailPanel,
onEditingCanceled,
Expand All @@ -49,17 +50,24 @@ function MTableBodyRow({ forwardedRef, ...props }) {
const columns = propColumns.filter((columnDef) => !columnDef.hidden);

const onClick = (event, callback) =>
callback(event, data, (panelIndex) => {
let panel = detailPanel;
if (Array.isArray(panel)) {
panel = panel[panelIndex || 0];
if (typeof panel === 'function') {
panel = panel(data);
callback(
event,
data,
(panelIndex) => {
let panel = detailPanel;
if (Array.isArray(panel)) {
panel = panel[panelIndex || 0];
if (typeof panel === 'function') {
panel = panel(data);
}
panel = panel.render;
}
panel = panel.render;
onToggleDetailPanel(path, panel);
},
() => {
onRowEditStarted(data);
}
onToggleDetailPanel(path, panel);
});
);

const handleOnRowClick = useDoubleClick(
onRowClick ? (e) => onClick(e, onRowClick) : undefined,
Expand Down
1 change: 1 addition & 0 deletions src/components/m-table-body.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ function MTableBody(props) {
cellEditable={props.cellEditable}
onCellEditStarted={props.onCellEditStarted}
onCellEditFinished={props.onCellEditFinished}
onRowEditStarted={props.onRowEditStarted}
scrollWidth={props.scrollWidth}
/>
);
Expand Down
18 changes: 13 additions & 5 deletions src/material-table.js
Original file line number Diff line number Diff line change
Expand Up @@ -338,11 +338,7 @@ export default class MaterialTable extends React.Component {
calculatedProps.editable.isEditHidden &&
calculatedProps.editable.isEditHidden(rowData),
onClick: (e, rowData) => {
this.dataManager.changeRowEditing(rowData, 'update');
this.setState({
...this.dataManager.getRenderState(),
showAddRow: false
});
this.onRowEditStarted(rowData);
}
}));
}
Expand Down Expand Up @@ -1046,6 +1042,7 @@ export default class MaterialTable extends React.Component {
cellEditable={props.cellEditable}
onCellEditStarted={this.onCellEditStarted}
onCellEditFinished={this.onCellEditFinished}
onRowEditStarted={this.onRowEditStarted}
bulkEditOpen={this.dataManager.bulkEditOpen}
bulkEditChangedRows={this.dataManager.bulkEditChangedRows}
onBulkEditRowChanged={this.dataManager.onBulkEditRowChanged}
Expand All @@ -1054,6 +1051,17 @@ export default class MaterialTable extends React.Component {
</Table>
);

onRowEditStarted = (rowData) => {
if (!this.props.editable?.onRowUpdate) {
return;
}
this.dataManager.changeRowEditing(rowData, 'update');
this.setState({
...this.dataManager.getRenderState(),
showAddRow: false
});
};

getColumnsWidth = (props, count) => {
const result = [];

Expand Down
3 changes: 2 additions & 1 deletion types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ export interface MaterialTableProps<RowData extends object> {
onRowClick?: (
event?: React.MouseEvent,
rowData?: RowData,
toggleDetailPanel?: (panelIndex?: number) => void
toggleDetailPanel?: (panelIndex?: number) => void,
enableEditMode?: () => void
) => void;
onRowDoubleClick?: (
event?: React.MouseEvent,
Expand Down

0 comments on commit ea187ff

Please sign in to comment.