Apporach 1: Use editing dialog
You can make the edit button open an editing dialog and dispatch the new change to the store after the user closes it:
- Create a custom cell renderer for the button and dialog. In this example, I'll use Material-UI's
Dialog
. You can choose whatever dialog library you want however.
function ButtonActionRenderer(props) {
// props is ICellRenererParams. See:
// https://www.ag-grid.com/react-grid/component-cell-renderer/#cell-renderer-component-2
const { id } = props.data;
const [open, setOpen] = useState(false);
const [memberPayload, setMemberPayload] = useState(props.data);
const dispatch = useDispatch();
const onClose = () => setOpen(false);
const onSave = () => {
dispatch(memberAction.update(id, memberPayload);
onClose();
}
return (
<>
<button onClick={() => setOpen(true)}>Edit</button>
<Dialog open={open} onClose={() => setOpen(false)}>
<DialogContent>
<EditingForm value={memberPayload} onChange={setMemberPayload} />
</DialogContent>
<DialogActions>
<Button onClick={onSave} color="primary">
Close
</Button>
<Button onClick={onClose} color="primary">
Save
</Button>
</DialogActions>
</Dialog>
</>
);
}
- Register that custom cell renderer in ag-grid
<AgGridReact
columnDefs={[...columnDefs,
{
headerName: "Action",
field: "action",
width: 100,
cellRenderer: "ButtonActionRenderer"
}]}
frameworkComponents={{
ButtonActionRenderer,
}}
/>
Apporach 2: Use Ag-Grid Cell Editor
Make your table cells editable and dispatch new changes to the store every time the input changes.
const MyEditor = forwardRef((props, ref) => {
// props is ICellEditorParams. See:
// https://www.ag-grid.com/react-grid/component-cell-editor/#cell-editor-component-1
const field = props.colDef.field;
const member = useSelector(state => state.members.id);
const value = member[field];
const onChange = (e) => dispatch(memberAction.updateField(field, e.target.value));
return (
<input
ref={refInput}
value={value}
onChange={onChange}
style={{width: "100%"}}
/>
);
});
- Register the custom cell editor in ag-grid
<AgGridReact
columnDefs={[...columnDefs,
{
headerName: "Name",
field: "name",
width: 100,
cellEditor: "MyEditor"
}]}
frameworkComponents={{
MyEditor,
}}
/>
References