Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor/findDomNode #6805

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions src/components/popover/popover.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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`

Expand Down Expand Up @@ -86,7 +86,7 @@ export const Popover = forwardRef<PopoverRef, PopoverProps>((p, ref) => {
[visible]
)

const targetRef = useRef<Wrapper>(null)
const wrapperRef = useRef<WrapperRef>(null)
const floatingRef = useRef<HTMLDivElement>(null)
const arrowRef = useRef<HTMLDivElement>(null)

Expand All @@ -113,7 +113,7 @@ export const Popover = forwardRef<PopoverRef, PopoverProps>((p, ref) => {
const [targetElement, setTargetElement] = useState<Element | null>(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)
Expand Down Expand Up @@ -198,15 +198,15 @@ export const Popover = forwardRef<PopoverRef, PopoverProps>((p, ref) => {
if (!props.trigger) return
setVisible(false)
},
[() => targetRef.current?.element, floatingRef],
[() => wrapperRef.current?.element, floatingRef],
['click', 'touchmove']
)

const shouldRender = useShouldRender(visible, false, props.destroyOnHide)

return (
<>
<Wrapper ref={targetRef}>{props.children}</Wrapper>
<Wrapper ref={wrapperRef}>{props.children}</Wrapper>
{shouldRender && renderToContainer(props.getContainer, floating)}
</>
)
Expand Down
50 changes: 25 additions & 25 deletions src/components/popover/wrapper.tsx
Original file line number Diff line number Diff line change
@@ -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<WrapperRef, { children: ReactElement }>(
({ children }, ref) => {
const childRef = useRef<any>(null)

render() {
return React.Children.only(this.props.children)
useImperativeHandle(ref, () => ({
element: childRef.current,
}))

return (
<>
<span
style={{ display: 'none' }}
ref={el => {
if (el) {
childRef.current = el.nextElementSibling
}
}}
/>
{React.Children.only(children)}
</>
)
}
}
)
Loading