diff --git a/src/components/popover/popover.tsx b/src/components/popover/popover.tsx index 719a74154f..e30b9248d5 100644 --- a/src/components/popover/popover.tsx +++ b/src/components/popover/popover.tsx @@ -34,7 +34,7 @@ import { import { Arrow } from './arrow' import { DeprecatedPlacement, Placement } from './index' import { normalizePlacement } from './normalize-placement' -import { Wrapper } from './wrapper' +import { Wrapper, WrapperRef } from './wrapper' const classPrefix = `adm-popover` @@ -86,7 +86,7 @@ export const Popover = forwardRef((p, ref) => { [visible] ) - const targetRef = useRef(null) + const wrapperRef = useRef(null) const floatingRef = useRef(null) const arrowRef = useRef(null) @@ -113,7 +113,7 @@ export const Popover = forwardRef((p, ref) => { const [targetElement, setTargetElement] = useState(null) async function update() { - const target = targetRef.current?.element ?? null + const target = wrapperRef.current?.element ?? null const floating = floatingRef.current const arrowElement = arrowRef.current setTargetElement(target) @@ -198,7 +198,7 @@ export const Popover = forwardRef((p, ref) => { if (!props.trigger) return setVisible(false) }, - [() => targetRef.current?.element, floatingRef], + [() => wrapperRef.current?.element, floatingRef], ['click', 'touchmove'] ) @@ -206,7 +206,7 @@ export const Popover = forwardRef((p, ref) => { return ( <> - {props.children} + {props.children} {shouldRender && renderToContainer(props.getContainer, floating)} ) diff --git a/src/components/popover/wrapper.tsx b/src/components/popover/wrapper.tsx index f4d08aefef..55b4ec3e0c 100644 --- a/src/components/popover/wrapper.tsx +++ b/src/components/popover/wrapper.tsx @@ -1,29 +1,29 @@ -import React from 'react' -import type { ReactNode } from 'react' -import { findDOMNode } from 'react-dom' +import React, { ReactElement, useImperativeHandle, useRef } from 'react' -export class Wrapper extends React.Component< - { - children?: ReactNode - }, - {} -> { - element: Element | null = null - componentDidMount() { - this.componentDidUpdate() - } +export interface WrapperRef { + element: Element +} - componentDidUpdate() { - // eslint-disable-next-line - const node = findDOMNode(this) - if (node instanceof Element) { - this.element = node - } else { - this.element = null - } - } +export const Wrapper = React.forwardRef( + ({ children }, ref) => { + const childRef = useRef(null) - render() { - return React.Children.only(this.props.children) + useImperativeHandle(ref, () => ({ + element: childRef.current, + })) + + return ( + <> + { + if (el) { + childRef.current = el.nextElementSibling + } + }} + /> + {React.Children.only(children)} + + ) } -} +)