Skip to content

Commit

Permalink
refactor: use rc-util getNodeRef (#238)
Browse files Browse the repository at this point in the history
  • Loading branch information
zombieJ authored Dec 11, 2024
1 parent 57f1b30 commit f941569
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 15 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
"@babel/runtime": "^7.18.3",
"@rc-component/trigger": "^2.0.0",
"classnames": "^2.2.6",
"rc-util": "^5.17.0"
"rc-util": "^5.44.1"
},
"devDependencies": {
"@rc-component/father-plugin": "^1.0.0",
Expand Down
8 changes: 2 additions & 6 deletions src/Dropdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ import type {
BuildInPlacements,
} from '@rc-component/trigger/lib/interface';
import classNames from 'classnames';
import { composeRef, supportRef } from 'rc-util/lib/ref';
import type { ReactElement } from 'react';
import { composeRef, getNodeRef, supportRef } from 'rc-util/lib/ref';
import React from 'react';
import useAccessibility from './hooks/useAccessibility';
import Overlay from './Overlay';
Expand Down Expand Up @@ -139,10 +138,7 @@ function Dropdown(props: DropdownProps, ref) {
mergedVisible && getOpenClassName(),
),
ref: supportRef(children)
? composeRef(
childRef,
(children as ReactElement & { ref: React.Ref<HTMLElement> }).ref,
)
? composeRef(childRef, getNodeRef(children))
: undefined,
});

Expand Down
21 changes: 13 additions & 8 deletions src/Overlay.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import React, { forwardRef, ReactElement, useMemo } from 'react';
import { composeRef, getNodeRef, supportRef } from 'rc-util/lib/ref';
import React, { forwardRef, useMemo } from 'react';
import type { DropdownProps } from './Dropdown';
import { composeRef, supportRef } from 'rc-util/lib/ref';

export type OverlayProps = Pick<DropdownProps, 'overlay' | 'arrow' | 'prefixCls'>
export type OverlayProps = Pick<
DropdownProps,
'overlay' | 'arrow' | 'prefixCls'
>;

const Overlay = forwardRef<HTMLElement, OverlayProps>((props, ref) => {
const {overlay, arrow, prefixCls} = props;
const { overlay, arrow, prefixCls } = props;

const overlayNode = useMemo(() => {
let overlayElement: React.ReactElement;
Expand All @@ -17,14 +20,16 @@ const Overlay = forwardRef<HTMLElement, OverlayProps>((props, ref) => {
return overlayElement;
}, [overlay]);

const composedRef = composeRef(ref, (overlayNode as ReactElement & {ref: React.Ref<HTMLElement>})?.ref);
const composedRef = composeRef(ref, getNodeRef(overlayNode));

return (
<>
{arrow && <div className={`${prefixCls}-arrow`} />}
{React.cloneElement(overlayNode, { ref: supportRef(overlayNode) ? composedRef : undefined })}
{React.cloneElement(overlayNode, {
ref: supportRef(overlayNode) ? composedRef : undefined,
})}
</>
)
);
});

export default Overlay;
export default Overlay;

0 comments on commit f941569

Please sign in to comment.