diff --git a/types/react-beforeunload/index.d.ts b/types/react-beforeunload/index.d.ts new file mode 100644 index 0000000000..67f73b9917 --- /dev/null +++ b/types/react-beforeunload/index.d.ts @@ -0,0 +1,16 @@ +// Type definitions for react-beforeunload 2.1 +// Project: https://github.com/jacobbuck/react-beforeunload#readme +// Definitions by: nem035 +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.8 + +import * as React from 'react'; + +export type UseBeforeunloadHandler = ((arg: Event) => string | undefined) | ((arg: Event) => void); + +export function useBeforeunload(handler?: UseBeforeunloadHandler): void; + +export const Beforeunload: React.FC<{ + children?: React.ReactNode; + onBeforeunload: UseBeforeunloadHandler; +}>; diff --git a/types/react-beforeunload/react-beforeunload-tests.tsx b/types/react-beforeunload/react-beforeunload-tests.tsx new file mode 100644 index 0000000000..ba9dd03896 --- /dev/null +++ b/types/react-beforeunload/react-beforeunload-tests.tsx @@ -0,0 +1,59 @@ +import * as React from 'react'; +import * as ReactDOM from 'react-dom'; +import { useBeforeunload, Beforeunload } from 'react-beforeunload'; + +function AppWithNoopHook() { + useBeforeunload(); + + return

AppWithDialogBoxHook

; +} + +function AppWithDialogBoxHook() { + useBeforeunload(event => event.preventDefault()); + + return

AppWithDialogBoxHook

; +} + +function AppWithCustomMessageHook() { + useBeforeunload(() => "You'll lose your data!"); + + return

AppWithCustomMessageHook

; +} + +function AppWithDialogBoxComponent() { + return event.preventDefault()} />; +} + +function AppWithCustomMessageComponent() { + return "You'll lose your data!"} />; +} + +function AppWithDialogBoxWithChildrenComponent() { + return ( + event.preventDefault()}> +

AppWithCustomMessageHook

+
+ ); +} + +function AppWithCustomMessageWithChildrenComponent() { + return ( + "You'll lose your data!"}> +

AppWithCustomMessageHook

+
+ ); +} + +function App() { + return ( + <> + + + + + + + + ); +} +ReactDOM.render(, document.getElementById('app')); diff --git a/types/react-beforeunload/tsconfig.json b/types/react-beforeunload/tsconfig.json new file mode 100644 index 0000000000..068e418470 --- /dev/null +++ b/types/react-beforeunload/tsconfig.json @@ -0,0 +1,25 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "dom", + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictFunctionTypes": true, + "strictNullChecks": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true, + "jsx": "react" + }, + "files": [ + "index.d.ts", + "react-beforeunload-tests.tsx" + ] +} diff --git a/types/react-beforeunload/tslint.json b/types/react-beforeunload/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/react-beforeunload/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" }