-
Notifications
You must be signed in to change notification settings - Fork 3k
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 #27490 from bernhardoj/fix/26197-suggestion-hide-o…
…n-typing Fix suggestion menu closes on typing @ at second line
- Loading branch information
Showing
2 changed files
with
63 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import {useEffect, useRef} from 'react'; | ||
import lodashDebounce from 'lodash/debounce'; | ||
|
||
/** | ||
* 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 {Function} func The function to debounce. | ||
* @param {Number} wait The number of milliseconds to delay. | ||
* @param {Object} options The options object. | ||
* @param {Boolean} options.leading Specify invoking on the leading edge of the timeout. | ||
* @param {Number} options.maxWait The maximum time func is allowed to be delayed before it’s invoked. | ||
* @param {Boolean} options.trailing Specify invoking on the trailing edge of the timeout. | ||
* @returns {Function} Returns a function to call the debounced function. | ||
*/ | ||
export default function useDebounce(func, wait, options) { | ||
const debouncedFnRef = useRef(); | ||
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]); | ||
|
||
return (...args) => { | ||
const debouncedFn = debouncedFnRef.current; | ||
|
||
if (debouncedFn) { | ||
debouncedFn(...args); | ||
} | ||
}; | ||
} |
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