Skip to content

Commit

Permalink
fix(dropdown): transmit ref to popover wrapper (#1630)
Browse files Browse the repository at this point in the history
Co-authored-by: maxin <maxin@growingio.com>
  • Loading branch information
nnmax and maxin committed Dec 7, 2021
1 parent 14e74aa commit 1397868
Showing 1 changed file with 11 additions and 12 deletions.
23 changes: 11 additions & 12 deletions src/dropdown/Dropdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import DropdownProps from './interface';
import useControlledState from '../utils/hooks/useControlledState';
import Popover from '../popover';

export function Dropdown<T = HTMLElement>(props: DropdownProps, ref: React.ForwardedRef<T>): React.ReactElement {
export const Dropdown = forwardRef<HTMLDivElement, DropdownProps>((props, ref) => {
const {
children,
placement = 'bottomLeft',
Expand All @@ -30,8 +30,7 @@ export function Dropdown<T = HTMLElement>(props: DropdownProps, ref: React.Forwa
{
'dropdown-active': controlledVisible,
},
(child as React.ReactElement).props.className,
ref
(child as React.ReactElement).props.className
),
onClick: (...arg: any) => {
setControlledVisible(!controlledVisible);
Expand Down Expand Up @@ -63,7 +62,13 @@ export function Dropdown<T = HTMLElement>(props: DropdownProps, ref: React.Forwa
}
};
return (
<div role="button" tabIndex={0} onClick={isUndefined(visible) ? onClick : undefined} onKeyDown={onKeyDown}>
<div
role="button"
ref={ref}
tabIndex={0}
onClick={isUndefined(visible) ? onClick : undefined}
onKeyDown={onKeyDown}
>
{contentNode}
</div>
);
Expand All @@ -89,12 +94,6 @@ export function Dropdown<T = HTMLElement>(props: DropdownProps, ref: React.Forwa
{getDropdownTrigger()}
</Popover>
);
}
});

// 支持 ForwardRef 传入泛型
// eslint-disable-next-line @typescript-eslint/no-explicit-any
type ForwardRefFn = <T = HTMLElement>(
props: React.PropsWithChildren<DropdownProps> & React.RefAttributes<T>
) => React.ReactElement;

export default forwardRef(Dropdown) as ForwardRefFn;
export default Dropdown;

1 comment on commit 1397868

@vercel
Copy link

@vercel vercel bot commented on 1397868 Dec 7, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.