Created
May 5, 2019 09:49
-
-
Save sbeugen/9320c44afa6b2db9badcb79b65ebd53c to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| /* global $ */ | |
| import { useEffect, useRef, useCallback } from "react"; | |
| const useErrorPopover = errorMessage => { | |
| const elementRef = useRef(null); | |
| const showPopover = useCallback( | |
| () => { | |
| if (errorMessage) { | |
| $(elementRef.current) | |
| .popover({ | |
| content: errorMessage, | |
| template: | |
| '<div class="popover" role="tooltip"><div class="arrow"></div><div class="popover-body"></div></div>' | |
| }) | |
| .popover("show"); | |
| } | |
| }, | |
| [errorMessage] | |
| ); | |
| const hidePopover = useCallback(() => { | |
| $(elementRef.current).popover("hide"); | |
| }, []); | |
| useEffect( | |
| () => { | |
| const element = elementRef.current; | |
| if (element) { | |
| element.onfocus = showPopover; | |
| element.onblur = hidePopover; | |
| } | |
| if (!errorMessage) { | |
| hidePopover(); | |
| } | |
| }, | |
| [errorMessage, showPopover, hidePopover] | |
| ); | |
| return elementRef; | |
| }; | |
| export default useErrorPopover; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment