-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[use-clipboard-copy_v0.1.x] Flow >= 0.203 upgrade
- Loading branch information
1 parent
1ec13c5
commit 082ee2e
Showing
4 changed files
with
152 additions
and
0 deletions.
There are no files selected for viewing
File renamed without changes.
File renamed without changes.
130 changes: 130 additions & 0 deletions
130
definitions/npm/use-clipboard-copy_v0.1.x/flow_v0.203.x-/test_use-clipboard-copy_v0.1.x.js
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,130 @@ | ||
// @flow | ||
|
||
import { describe, it } from 'flow-typed-test'; | ||
|
||
import React from 'react'; | ||
import { useClipboard } from 'use-clipboard-copy'; | ||
|
||
describe('The `useClipboard` hook', () => { | ||
it('should accept valid parameters', () => { | ||
useClipboard(); | ||
useClipboard({ copiedTimeout: 200 }); | ||
|
||
// $FlowExpectedError[incompatible-call] | ||
useClipboard(null); | ||
// $FlowExpectedError[prop-missing] | ||
// $FlowExpectedError[cannot-resolve-name] | ||
useClipboard({ fail }); | ||
}); | ||
|
||
it('should validate the return type', () => { | ||
const clipboard = useClipboard(); | ||
|
||
(clipboard.copied: boolean); | ||
clipboard.copy(''); | ||
(clipboard.isSupported(): boolean); | ||
|
||
function Test() { | ||
const clipboard = useClipboard(); | ||
if (clipboard.target.current) { | ||
(clipboard.target.current: HTMLElement); | ||
} | ||
return <input ref={clipboard.target} />; | ||
} | ||
}); | ||
|
||
it('should validate on default usage', () => { | ||
function Test() { | ||
const clipboard = useClipboard(); | ||
|
||
if (clipboard.target.current) { | ||
(clipboard.target.current: HTMLElement); | ||
} | ||
|
||
return ( | ||
<> | ||
<input ref={clipboard.target} /> | ||
<button onClick={clipboard.copy}>Copy</button> | ||
</> | ||
); | ||
} | ||
}); | ||
|
||
it('should validate on type parameter usage', () => { | ||
function Test() { | ||
const clipboard = useClipboard<HTMLInputElement>(); | ||
|
||
if (clipboard.target.current) { | ||
(clipboard.target.current: HTMLInputElement); | ||
} | ||
|
||
return ( | ||
<> | ||
{/* $FlowExpectedError[incompatible-type] */} | ||
<textarea ref={clipboard.target} /> | ||
<input ref={clipboard.target} /> | ||
<button onClick={clipboard.copy}>Copy</button> | ||
</> | ||
); | ||
} | ||
}); | ||
|
||
it('should validate on imperative usage', () => { | ||
function Test({ url }: { url: string }) { | ||
const clipboard = useClipboard(); | ||
|
||
const handleClick = React.useCallback(() => { | ||
clipboard.copy(url); | ||
}, [clipboard.copy, url]); | ||
|
||
return <button onClick={handleClick}>Copy</button>; | ||
} | ||
}); | ||
|
||
it('should validate on timeout and state usage', () => { | ||
function Test({ url }: { url: string }) { | ||
const clipboard = useClipboard({ | ||
copiedTimeout: 600, | ||
}); | ||
|
||
return ( | ||
<> | ||
<input ref={clipboard.target} value={url} readOnly /> | ||
<button onClick={clipboard.copy}> | ||
{clipboard.copied ? 'Copied' : 'Copy'} | ||
</button> | ||
</> | ||
); | ||
} | ||
}); | ||
|
||
it('should validate on callbacks usage', () => { | ||
function Test() { | ||
const clipboard = useClipboard({ | ||
onSuccess() { | ||
console.log('Text was copied successfully!'); | ||
}, | ||
onError() { | ||
console.log('Failed to copy text!'); | ||
}, | ||
}); | ||
|
||
return ( | ||
<> | ||
<input ref={clipboard.target} /> | ||
<button onClick={clipboard.copy}>Copy</button> | ||
</> | ||
); | ||
} | ||
}); | ||
|
||
it('should validate on browser support check usage', () => { | ||
function Test() { | ||
const clipboard = useClipboard(); | ||
|
||
return clipboard.isSupported() | ||
? 'yay! copy-to-clipboard is supported' | ||
: 'meh. copy-to-clipboard is not supported'; | ||
} | ||
}); | ||
}); |
22 changes: 22 additions & 0 deletions
22
definitions/npm/use-clipboard-copy_v0.1.x/flow_v0.203.x-/use-clipboard-copy_v0.1.x.js
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,22 @@ | ||
declare module 'use-clipboard-copy' { | ||
declare export type UseClipboardOptions = {| | ||
copiedTimeout?: number, | ||
onSuccess?: () => void, | ||
onError?: () => void, | ||
selectOnCopy?: boolean, | ||
selectOnError?: boolean, | ||
|}; | ||
|
||
declare export type ClipboardAPI<T> = {| | ||
copied: boolean, | ||
copy: (text?: string) => void, | ||
isSupported: () => boolean, | ||
target: { current: T | null, ... }, | ||
|}; | ||
|
||
declare export function useClipboard< | ||
T: HTMLElement | HTMLInputElement | HTMLTextAreaElement = HTMLElement | ||
>( | ||
options?: UseClipboardOptions | ||
): ClipboardAPI<T>; | ||
} |