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: use rc-util getNodeRef #238

Merged
merged 1 commit into from
Dec 11, 2024
Merged
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
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;
Loading