Added clickAwayHandler + POST functionality + Bug fixed.

This commit is contained in:
Daniel
2023-02-14 00:52:47 +03:30
parent eadf021939
commit cefeb5e7a9
6 changed files with 94 additions and 8 deletions
+35
View File
@@ -0,0 +1,35 @@
import React, { useRef, useEffect, ReactNode, RefObject } from "react";
interface Props {
children: ReactNode;
onClickOutside: Function;
}
function useOutsideAlerter(
ref: RefObject<HTMLElement>,
onClickOutside: Function
) {
useEffect(() => {
function handleClickOutside(event: Event) {
if (
ref.current &&
!ref.current.contains(event.target as HTMLInputElement)
) {
onClickOutside();
}
}
// Bind the event listener
document.addEventListener("mousedown", handleClickOutside);
return () => {
// Unbind the event listener on clean up
document.removeEventListener("mousedown", handleClickOutside);
};
}, [ref, onClickOutside]);
}
export default function OutsideAlerter({ children, onClickOutside }: Props) {
const wrapperRef = useRef(null);
useOutsideAlerter(wrapperRef, onClickOutside);
return <div ref={wrapperRef}>{children}</div>;
}