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

doc: write documentation for useUnsavedFormChanges #25

Merged
merged 2 commits into from
Jun 13, 2024
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
1 change: 1 addition & 0 deletions pages/hooks/_meta.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@
"use-persistent-state": "usePersistentState",
"use-query-params": "useQueryParams",
"use-timeout": "useTimeout",
"use-unsaved-form-changes": "useUnsavedFormChanges",
"use-url": "useUrl"
}
85 changes: 85 additions & 0 deletions pages/hooks/use-unsaved-form-changes.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
# useUnsavedFormChanges

<h3 className="mt-4 text-xl font-normal text-gray-400">A custom hook to manage unsaved form changes and warn users before leaving the page.</h3>

## Add hook

Create a file `use-unsaved-form-changes.ts` and copy & paste the code from [useUnsavedFormChanges](/hooks/use-unsaved-form-changes#hook).

## Usage

```tsx {3,6}

import React from 'react';
import { useUnsavedFormChanges } from './hooks/use-unsaved-form-changes';

function App() {
const { isFormChanged, setFormChanged } = useUnsavedFormChanges();

const handleChange = () => {
setFormChanged(true);
};

const handleSave = () => {
setFormChanged(false);
};

return (
<div>
<h1>useUnsavedFormChanges Example</h1>
<form>
<input type="text" onChange={handleChange} />
<button type="button" onClick={handleSave}>Save</button>
</form>
{isFormChanged && <p>You have unsaved changes!</p>}
</div>
);
}

export default App;

```

## Hook

```tsx

import { useEffect, useState } from 'react';

export function useUnsavedFormChanges() {
const [isFormChanged, setIsFormChanged] = useState(false);

const setFormChanged = (value: boolean) => {
setIsFormChanged(value);
};

useEffect(() => {
const handleBeforeUnload = (event: { returnValue: string }) => {
if (isFormChanged) {
const confirmationMessage = 'You have unsaved changes. Are you sure you want to leave?';
event.returnValue = confirmationMessage;
return confirmationMessage;
}
};

window.addEventListener('beforeunload', handleBeforeUnload);

return () => {
window.removeEventListener('beforeunload', handleBeforeUnload);
};
}, [isFormChanged]);

return { isFormChanged, setFormChanged };
}

```

## API

<h1 className='text-xl font-medium mt-6'>Returns</h1>

| Name | Type | Description |
| ---- | ---- | ----------- |
| `isFormChanged` | `boolean` | Indicates whether the form has unsaved changes. |
| `setFormChanged` | `(value: boolean) => void` | A function to set the `isFormChanged` state. |

1 change: 1 addition & 0 deletions theme.config.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ const config = {
title === "useUrl" ||
title === "useEnv" ||
title === "useMouse" ||
title === "useUnsavedFormChanges" ||
title === "useCookie") && (
<Badge className=" absolute -right-[0.5em] bg-transparent border-lime-400 text-lime-500 px-[0.5em] hover:bg-transparent">
New
Expand Down
Loading