From f393e617ace66229acaf2d55ddacd520435838cb Mon Sep 17 00:00:00 2001 From: Nemanja Stojanovic Date: Fri, 28 Jun 2019 11:34:14 -0400 Subject: [PATCH] Add types for `react-beforeunload` (#36380) * :tada: add types for `react-beforeunload` * :bug: [react-beforeunload] - fix broken tests from travis-ci (add `dom` for compiler `lib` option and duplicate `Handler` type because of https://github.com/Microsoft/dtslint/blob/master/docs/strict-export-declare-modifiers.md) * [react-beforeunload] make the handler and its `event` argument optional * [react-beforeunload] only make the hook handler optional Only make the hook handler optional (but not the event!) * [react-beforeunload] add noop hook test case * [react-beforeunload] oops, fix lint error :) * [react-beforeunload] add appropriate `undefined` return type * [react-beforeunload] split types semantically by function signatures * [react-beforeunload] fix typo --- types/react-beforeunload/index.d.ts | 16 +++++ .../react-beforeunload-tests.tsx | 59 +++++++++++++++++++ types/react-beforeunload/tsconfig.json | 25 ++++++++ types/react-beforeunload/tslint.json | 1 + 4 files changed, 101 insertions(+) create mode 100644 types/react-beforeunload/index.d.ts create mode 100644 types/react-beforeunload/react-beforeunload-tests.tsx create mode 100644 types/react-beforeunload/tsconfig.json create mode 100644 types/react-beforeunload/tslint.json 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" }