Skip to content

feat(core): add warnWhenUnsavedChanges to useEditableTable #6831

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
9 changes: 9 additions & 0 deletions .changeset/yellow-dots-beg.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
"@refinedev/core": minor
---

feat: add warnWhenUnsavedChanges prop support to useEditableTable #6811

Now with the warnWhenUnsavedChanges prop, developers can enable unsaved changes warnings in useEditableTable via useForm integration.

Resolves #6811
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ type useEditableTableProps<
* When true, row will be closed after successful submit.
*/
autoSubmitClose?: boolean;
/**
* Show confirmation dialog when navigating away with unsaved changes
*/
warnWhenUnsavedChanges?: boolean;
};

/**
Expand Down Expand Up @@ -89,11 +93,18 @@ export const useEditableTable = <
successNotification: undefined,
errorNotification: undefined,
});
const edit = useForm<TQueryFnData, TError, TVariables>({
...props,
const { warnWhenUnsavedChanges, ...restProps } = props;

const formProps: UseFormProps<TQueryFnData, TError, TVariables> = {
...restProps,
action: "edit",
redirect: false,
});
...(typeof warnWhenUnsavedChanges !== "undefined"
? { warnWhenUnsavedChanges }
: {}),
};

const edit = useForm<TQueryFnData, TError, TVariables>(formProps);

const { id: editId, setId, saveButtonProps } = edit;

Expand All @@ -117,11 +128,14 @@ export const useEditableTable = <
formProps: {
...edit.formProps,
onFinish: async (values) => {
const result = await edit.onFinish(values);
if (autoSubmitClose) {
setId(undefined);
if (typeof edit.onFinish === "function") {
const result = await edit.onFinish(values);
if (autoSubmitClose) {
setId(undefined);
}
return result;
}
return result;
return Promise.resolve();
},
},
saveButtonProps,
Expand Down