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

fix: pass ref to renderSwitcher props #1107

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
32 changes: 19 additions & 13 deletions src/components/DropdownMenu/DropdownMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,13 @@ import {toItemList} from './utils/toItemList';

import './DropdownMenu.scss';

type SwitcherProps = {
type SwitcherProps<S extends HTMLElement> = {
ref: React.RefObject<S>;
onKeyDown: React.KeyboardEventHandler<HTMLElement>;
onClick: React.MouseEventHandler<HTMLElement>;
};

export type DropdownMenuProps<T> = {
export type DropdownMenuProps<T, S extends HTMLElement = HTMLElement> = {
/**
* Array of items.
* Nested arrays of items represent visually separated groups.
Expand Down Expand Up @@ -68,7 +69,7 @@ export type DropdownMenuProps<T> = {
/**
* Menu toggle control.
*/
renderSwitcher?: (props: SwitcherProps) => React.ReactNode;
renderSwitcher?: (props: SwitcherProps<S>) => React.ReactNode;
switcherWrapperClassName?: string;
/**
* Overrides the default switcher button props.
Expand All @@ -95,7 +96,7 @@ export type ControlledDropdownMenuProps<T> = DropdownMenuProps<T> & {
onOpenToggle: React.Dispatch<React.SetStateAction<boolean>>;
};

const DropdownMenu = <T,>({
const DropdownMenu = <T, S extends HTMLElement = HTMLElement>({
items = [],
size = 'm',
icon = <Icon data={Ellipsis} />,
Expand All @@ -113,8 +114,8 @@ const DropdownMenu = <T,>({
menuProps,
popupProps,
children,
}: DropdownMenuProps<T> | ControlledDropdownMenuProps<T>) => {
const anchorRef = React.useRef<HTMLDivElement | null>(null);
}: DropdownMenuProps<T, S> | ControlledDropdownMenuProps<T>) => {
const anchorRef = React.useRef<S>(null);

const {isPopupShown, togglePopup, closePopup} = usePopupVisibility(
open,
Expand All @@ -138,14 +139,17 @@ const DropdownMenu = <T,>({
[items],
);

const handleSwitcherClick: React.MouseEventHandler<HTMLElement> = (event) => {
if (disabled) {
return;
}
const handleSwitcherClick: React.MouseEventHandler<HTMLElement> = React.useCallback(
(event) => {
if (disabled) {
return;
}

onSwitcherClick?.(event);
togglePopup();
};
onSwitcherClick?.(event);
togglePopup();
},
[disabled, onSwitcherClick, togglePopup],
);

const {onKeyDown: handleSwitcherKeyDown} = useActionHandlers(handleSwitcherClick);

Expand All @@ -154,11 +158,13 @@ const DropdownMenu = <T,>({
{/* FIXME remove switcher prop and this wrapper */}
{/* eslint-disable-next-line jsx-a11y/click-events-have-key-events, jsx-a11y/no-static-element-interactions */}
<div
//@ts-ignore
ref={anchorRef}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The ref is passed twice, yes it will return the second element, but is it better to add a check like this Boolean(render Switcher) ? undefined : controlRef

className={cnDropdownMenu('switcher-wrapper', switcherWrapperClassName)}
onClick={handleSwitcherClick}
>
{renderSwitcher?.({
ref: anchorRef,
onClick: handleSwitcherClick,
onKeyDown: handleSwitcherKeyDown,
}) ||
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import React from 'react';

export type DropdownMenuNavigationContextType = {
export type DropdownMenuNavigationContextType<S extends HTMLElement = HTMLElement> = {
activeMenuPath: number[];
setActiveMenuPath: (path: number[]) => void;
anchorRef: React.RefObject<HTMLDivElement>;
anchorRef: React.RefObject<S>;
};

const rootMenuPath: number[] = [];
Expand All @@ -16,8 +16,8 @@ export const DropdownMenuNavigationContext = React.createContext<DropdownMenuNav
},
);

export type DropdownMenuNavigationContextProviderProps = {
anchorRef: React.RefObject<HTMLDivElement>;
export type DropdownMenuNavigationContextProviderProps<S extends HTMLElement = HTMLElement> = {
anchorRef: React.RefObject<S>;
children: React.ReactNode;
disabled: boolean;
};
Expand Down
17 changes: 10 additions & 7 deletions src/components/DropdownMenu/DropdownMenuPopup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,19 @@ import type {PopupProps} from '../Popup';
import {cnDropdownMenu} from './DropdownMenu.classname';
import {DropdownMenuContext} from './DropdownMenuContext';
import {DropdownMenuItem} from './DropdownMenuItem';
import {DropdownMenuNavigationContext} from './DropdownMenuNavigationContext';
import {
DropdownMenuNavigationContext,
DropdownMenuNavigationContextType,
} from './DropdownMenuNavigationContext';
import type {DropdownMenuListItem, DropdownMenuSize} from './types';
import {isSeparator} from './utils/isSeparator';
import {shouldSkipItemNavigation} from './utils/shouldSkipItemNavigation';
import {stringifyNavigationPath} from './utils/stringifyNavigationPath';

export type DropdownMenuPopupProps<T> = {
export type DropdownMenuPopupProps<T, S extends HTMLElement = HTMLElement> = {
items: DropdownMenuListItem<T>[];
open: boolean;
anchorRef: React.RefObject<HTMLDivElement>;
anchorRef: React.RefObject<S>;
onClose?: () => void;
size?: DropdownMenuSize;
menuProps?: MenuProps;
Expand All @@ -27,7 +30,7 @@ export type DropdownMenuPopupProps<T> = {
path?: number[];
};

export const DropdownMenuPopup = <T,>({
export const DropdownMenuPopup = <T, S extends HTMLElement = HTMLElement>({
items,
open,
anchorRef,
Expand All @@ -37,14 +40,14 @@ export const DropdownMenuPopup = <T,>({
children,
popupProps,
path = [],
}: DropdownMenuPopupProps<T>) => {
}: DropdownMenuPopupProps<T, S>) => {
const {toggle, data} = React.useContext(DropdownMenuContext);

const {
activeMenuPath,
setActiveMenuPath,
anchorRef: navigationAnchorRef,
} = React.useContext(DropdownMenuNavigationContext);
} = React.useContext(DropdownMenuNavigationContext) as DropdownMenuNavigationContextType<S>;

const isSubmenu = path.length > 0;

Expand Down Expand Up @@ -115,7 +118,7 @@ export const DropdownMenuPopup = <T,>({
activeItemIndex,
setActiveItemIndex,
reset: resetNavigation,
} = useListNavigation<DropdownMenuListItem<T>, HTMLDivElement>({
} = useListNavigation<DropdownMenuListItem<T>, S>({
items,
skip: shouldSkipItemNavigation,
anchorRef: navigationAnchorRef,
Expand Down
9 changes: 5 additions & 4 deletions src/components/DropdownMenu/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -545,7 +545,8 @@ This type describes individual dropdown menu items.

### SwitcherProps

| Name | Description | Type |
| :---------- | :------------------------------------------------------------- | :----------: |
| `onClick` | Called when the switcher is clicked. | `() => void` |
| `onKeyDown` | Called when the switcher is focused and action key is pressed. | `() => void` |
| Name | Description | Type |
| :---------- | :------------------------------------------------------------- | :-------------------------------------------: |
| `ref` | Reference to a custom switcher to align the dropdown popup. | `React.MutableRefObject<HTMLElement \| null>` |
| `onClick` | Called when the switcher is clicked. | `React.KeyboardEventHandler<HTMLElement>` |
| `onKeyDown` | Called when the switcher is focused and action key is pressed. | `React.MouseEventHandler<HTMLElement>` |
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ import React from 'react';
import {Bars} from '@gravity-ui/icons';
import type {Meta, StoryFn} from '@storybook/react';

import {Button} from '../../Button';
import {Icon} from '../../Icon';
import {Label} from '../../Label';
import type {LabelProps} from '../../Label';
import {cn} from '../../utils/cn';
import {DropdownMenu} from '../DropdownMenu';
import {DropdownMenu, DropdownMenuProps} from '../DropdownMenu';
import type {DropdownMenuItem} from '../DropdownMenu';

import {options, optionsAssorted, optionsWithGroups, optionsWithSubItems} from './options';
Expand Down Expand Up @@ -86,11 +87,22 @@ SwitcherTheme.args = {
SwitcherTheme.storyName = 'Icon theme';

// ----------------------------------------
const TextSwitcherTemplate: StoryFn = (args) => <DropdownMenu {...args} />;
const TextSwitcherTemplate: StoryFn<DropdownMenuProps<unknown>> = (args) => (
<DropdownMenu {...args} />
);
export const TextSwitcher = TextSwitcherTemplate.bind({});
TextSwitcher.args = {
items: options,
switcher: <div style={{cursor: 'pointer', fontWeight: 'bold'}}>&nbsp;John Doe&nbsp;</div>,
renderSwitcher: ({onClick, onKeyDown, ref}) => (
<Button
ref={ref}
onClick={onClick}
extraProps={{onKeyDown}}
style={{cursor: 'pointer', fontWeight: 'bold'}}
>
John Doe
</Button>
),
};
TextSwitcher.parameters = {
docs: {
Expand All @@ -115,13 +127,18 @@ const LabelSwitcherTemplate: StoryFn<{statuses: {text: string; style: LabelProps
}, [args.statuses, setStatus]);

return (
<DropdownMenu
<DropdownMenu<undefined, HTMLDivElement>
items={items}
switcher={
<Label theme={status.style} className={b('label-switcher-switcher')}>
renderSwitcher={({onClick, ref}) => (
<Label
ref={ref}
onClick={onClick}
theme={status.style}
className={b('label-switcher-switcher')}
>
{status.text}
</Label>
}
)}
popupProps={{className: b('label-switcher-menu')}}
/>
);
Expand Down
4 changes: 2 additions & 2 deletions src/components/DropdownMenu/hooks/useScrollHandler.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import React from 'react';

export function useScrollHandler(
export function useScrollHandler<S extends HTMLElement = HTMLElement>(
onScroll: (event: Event) => void,
anchorRef: React.RefObject<HTMLDivElement>,
anchorRef: React.RefObject<S>,
disabled?: boolean,
) {
React.useEffect(() => {
Expand Down
Loading
Loading