Fixing clickOutside

This commit is contained in:
Bel Curcio
2020-12-08 11:10:34 -03:00
parent e29dd243f6
commit b67b87c4fe
3 changed files with 21 additions and 22 deletions

View File

@@ -1,4 +1,4 @@
import React, { forwardRef, useEffect, Ref, MouseEvent } from 'react'
import React, { useRef, useEffect, MouseEvent } from 'react'
import hasParent from './has-parent'
interface ClickOutsideProps {
@@ -7,18 +7,12 @@ interface ClickOutsideProps {
children: any
}
const ClickOutside = forwardRef(
(
{ active = true, onClick, children }: ClickOutsideProps,
ref: Ref<HTMLDivElement> | null | any = {}
) => {
const innerRef = ref?.current
const ClickOutside = ({ active = true, onClick, children }: ClickOutsideProps) => {
const innerRef = useRef()
const handleClick = (event: any) => {
if (!hasParent(event.target, innerRef)) {
if (!hasParent(event.target, innerRef?.current)) {
if (typeof onClick === 'function') {
event.preventDefault()
event.stopImmediatePropagation()
onClick(event)
}
}
@@ -38,8 +32,7 @@ const ClickOutside = forwardRef(
}
})
return React.cloneElement(children, { ref })
return React.cloneElement(children, { ref: innerRef })
}
)
export default ClickOutside