kibana/packages/kbn-unsaved-changes-prompt
Luke Elmers b6287708f6
Adds AGPL 3.0 license (#192025)
Updates files outside of x-pack to be triple-licensed under Elastic
License 2.0, AGPL 3.0, or SSPL 1.0.
2024-09-06 19:02:41 -06:00
..
src/unsaved_changes_prompt Adds AGPL 3.0 license (#192025) 2024-09-06 19:02:41 -06:00
index.ts Adds AGPL 3.0 license (#192025) 2024-09-06 19:02:41 -06:00
jest.config.js Adds AGPL 3.0 license (#192025) 2024-09-06 19:02:41 -06:00
kibana.jsonc Minimize shared-common everywhere (#188606) 2024-07-29 12:47:46 -06:00
package.json Adds AGPL 3.0 license (#192025) 2024-09-06 19:02:41 -06:00
README.md [Ingest Pipelines] Add unsaved changes prompt (#183699) 2024-06-07 20:35:56 +02:00
tsconfig.json [Ingest Pipelines] Add unsaved changes prompt (#183699) 2024-06-07 20:35:56 +02:00

@kbn/unsaved-changes-prompt

The useUnsavedChangesPrompt function is a custom React hook that prompts users with a confirmation dialog when they try to leave a page with unsaved changes. It blocks navigation and shows a dialog using the provided openConfirm function. If the user confirms, it navigates away; otherwise, it cancels the navigation, ensuring unsaved changes are not lost.

import { useUnsavedChangesPrompt } from '@kbn/unsaved-changes-prompt';

export const SampleForm = ({ servicesForUnsavedChangesPrompt }) => {
  const { form } = useForm();
  const isFormDirty = useFormIsModified({ form });

  useUnsavedChangesPrompt({
    hasUnsavedChanges: isFormDirty,
    ...servicesForUnsavedChangesPrompt,
  });

  return (
    <>
      <Form form={form}>
         ....
      </Form>
    </>
  );
};