This repository has been archived by the owner on May 10, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: request confirmation when leaving a form with unsubmitted data (#…
…465) * feat: request confirmation when leaving a form with unsubmitted data I personally forgot to press the "Confirm" button on the final view a couple of times when adding sentences. Such leave-page notifications are a bit annoying, but more annoying to find out data has been lost IMO. * fixup: rename and move testUtils.tsx * fixup: rename renderRoute
- Loading branch information
1 parent
9743fbe
commit e117831
Showing
10 changed files
with
128 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import React, { useEffect } from 'react'; | ||
import { Prompt as ReactRouterPrompt } from 'react-router-dom'; | ||
|
||
interface PromptProps { | ||
when: boolean; | ||
message: string; | ||
} | ||
|
||
/** | ||
* Wrapper around react-router Prompt adding confirmation on tab close and external navigation. | ||
*/ | ||
export const Prompt: React.FC<PromptProps> = (props) => { | ||
const { when } = props; | ||
useEffect(() => { | ||
if (!when) { | ||
return () => null; | ||
} | ||
|
||
function listener(event: BeforeUnloadEvent) { | ||
// Cancel the event as stated by the standard. | ||
event.preventDefault(); | ||
// Chrome requires returnValue to be set. | ||
event.returnValue = ''; | ||
} | ||
|
||
window.addEventListener('beforeunload', listener); | ||
return () => window.removeEventListener('beforeunload', listener); | ||
}, [when]); | ||
|
||
return ( | ||
<ReactRouterPrompt {...props}/> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.