forked from Expensify/App
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request Expensify#32267 from fabioh8010/ts/hook/remaining-…
…group-1 [TS migration] Migrate '[Remaining Group 1]' hook to TypeScript
- Loading branch information
Showing
11 changed files
with
88 additions
and
63 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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,8 @@ | ||
import UseActiveElement from './types'; | ||
|
||
/** | ||
* Native doesn't have the DOM, so we just return null. | ||
*/ | ||
const useActiveElement: UseActiveElement = () => null; | ||
|
||
export default useActiveElement; |
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,3 @@ | ||
type UseActiveElement = () => Element | null; | ||
|
||
export default UseActiveElement; |
This file was deleted.
Oops, something went wrong.
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,45 @@ | ||
import {DebouncedFunc, DebounceSettings} from 'lodash'; | ||
import lodashDebounce from 'lodash/debounce'; | ||
import {useCallback, useEffect, useRef} from 'react'; | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
type GenericFunction = (...args: any[]) => void; | ||
|
||
/** | ||
* Create and return a debounced function. | ||
* | ||
* Every time the identity of any of the arguments changes, the debounce operation will restart (canceling any ongoing debounce). | ||
* This is especially important in the case of func. To prevent that, pass stable references. | ||
* | ||
* @param func The function to debounce. | ||
* @param wait The number of milliseconds to delay. | ||
* @param options The options object. | ||
* @param options.leading Specify invoking on the leading edge of the timeout. | ||
* @param options.maxWait The maximum time func is allowed to be delayed before it’s invoked. | ||
* @param options.trailing Specify invoking on the trailing edge of the timeout. | ||
* @returns Returns a function to call the debounced function. | ||
*/ | ||
export default function useDebounce<T extends GenericFunction>(func: T, wait: number, options?: DebounceSettings): T { | ||
const debouncedFnRef = useRef<DebouncedFunc<T>>(); | ||
const {leading, maxWait, trailing = true} = options ?? {}; | ||
|
||
useEffect(() => { | ||
const debouncedFn = lodashDebounce(func, wait, {leading, maxWait, trailing}); | ||
|
||
debouncedFnRef.current = debouncedFn; | ||
|
||
return () => { | ||
debouncedFn.cancel(); | ||
}; | ||
}, [func, wait, leading, maxWait, trailing]); | ||
|
||
const debounceCallback = useCallback((...args: Parameters<T>) => { | ||
const debouncedFn = debouncedFnRef.current; | ||
|
||
if (debouncedFn) { | ||
debouncedFn(...args); | ||
} | ||
}, []); | ||
|
||
return debounceCallback as T; | ||
} |
This file was deleted.
Oops, something went wrong.
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,5 @@ | ||
import UseDefaultDragAndDrop from './types'; | ||
|
||
const useDefaultDragAndDrop: UseDefaultDragAndDrop = () => {}; | ||
|
||
export default useDefaultDragAndDrop; |
18 changes: 13 additions & 5 deletions
18
src/hooks/useDefaultDragAndDrop/index.js → src/hooks/useDefaultDragAndDrop/index.ts
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 |
---|---|---|
@@ -1,21 +1,29 @@ | ||
import {useEffect} from 'react'; | ||
import UseDefaultDragAndDrop from './types'; | ||
|
||
export default function useDefaultDragAndDrop() { | ||
const useDefaultDragAndDrop: UseDefaultDragAndDrop = () => { | ||
useEffect(() => { | ||
const dropDragListener = (event) => { | ||
const dropDragListener = (event: DragEvent) => { | ||
event.preventDefault(); | ||
// eslint-disable-next-line no-param-reassign | ||
event.dataTransfer.dropEffect = 'none'; | ||
|
||
if (event.dataTransfer) { | ||
// eslint-disable-next-line no-param-reassign | ||
event.dataTransfer.dropEffect = 'none'; | ||
} | ||
}; | ||
|
||
document.addEventListener('dragover', dropDragListener); | ||
document.addEventListener('dragenter', dropDragListener); | ||
document.addEventListener('dragleave', dropDragListener); | ||
document.addEventListener('drop', dropDragListener); | ||
|
||
return () => { | ||
document.removeEventListener('dragover', dropDragListener); | ||
document.removeEventListener('dragenter', dropDragListener); | ||
document.removeEventListener('dragleave', dropDragListener); | ||
document.removeEventListener('drop', dropDragListener); | ||
}; | ||
}, []); | ||
} | ||
}; | ||
|
||
export default useDefaultDragAndDrop; |
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,3 @@ | ||
type UseDefaultDragAndDrop = () => void; | ||
|
||
export default UseDefaultDragAndDrop; |
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