Skip to content

Commit 12abaf9

Browse files
authored
refactor(ToolbarListButton): reuse ToolbarButtonView (#680)
1 parent 0182567 commit 12abaf9

File tree

2 files changed

+23
-49
lines changed

2 files changed

+23
-49
lines changed

src/toolbar/ToolbarButton.tsx

+9-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {forwardRef} from 'react';
1+
import {type ReactNode, forwardRef} from 'react';
22

33
import {ActionTooltip, Button, Icon, Popover, setRef} from '@gravity-ui/uikit';
44

@@ -17,17 +17,17 @@ export type ToolbarButtonProps<E> = ToolbarBaseProps<E> & ToolbarItemData<E>;
1717

1818
export type ToolbarButtonViewProps = Pick<
1919
ToolbarItemData<unknown>,
20-
'icon' | 'title' | 'hint' | 'hotkey' | 'hintWhenDisabled'
20+
'title' | 'hint' | 'hotkey' | 'hintWhenDisabled'
2121
> & {
2222
active: boolean;
2323
enabled: boolean;
2424
onClick: () => void;
2525
className?: string;
26-
};
26+
} & (Pick<ToolbarItemData<unknown>, 'icon'> | {children: ReactNode});
2727

2828
export const ToolbarButtonView = forwardRef<HTMLButtonElement, ToolbarButtonViewProps>(
2929
function ToolbarButtonView(
30-
{icon, title, hint, hotkey, hintWhenDisabled, active, enabled, onClick, className},
30+
{title, hint, hotkey, hintWhenDisabled, active, enabled, onClick, className, ...props},
3131
ref,
3232
) {
3333
const disabled = !active && !enabled;
@@ -70,7 +70,11 @@ export const ToolbarButtonView = forwardRef<HTMLButtonElement, ToolbarButtonView
7070
className={b(null, [className])}
7171
aria-label={titleText}
7272
>
73-
<Icon data={icon.data} size={icon.size ?? 16} />
73+
{'icon' in props ? (
74+
<Icon data={props.icon.data} size={props.icon.size ?? 16} />
75+
) : (
76+
props.children
77+
)}
7478
</Button>
7579
)}
7680
</ActionTooltip>

src/toolbar/ToolbarListButton.tsx

+14-44
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,15 @@
11
import {Fragment, useEffect, useState} from 'react';
22

33
import {ChevronDown} from '@gravity-ui/icons';
4-
import {
5-
ActionTooltip,
6-
Button,
7-
HelpMark,
8-
Hotkey,
9-
Icon,
10-
Menu,
11-
Popover,
12-
Popup,
13-
} from '@gravity-ui/uikit';
4+
import {HelpMark, Hotkey, Icon, Menu, Popover, Popup} from '@gravity-ui/uikit';
145

156
import {cn} from '../classname';
167
import {i18n} from '../i18n/common';
178
import {isFunction} from '../lodash';
189
import {useBooleanState, useElementState} from '../react-utils/hooks';
1910

2011
import {PreviewTooltip} from './PreviewTooltip';
21-
import {ToolbarTooltipDelay} from './const';
12+
import {ToolbarButtonView} from './ToolbarButton';
2213
import type {
2314
ToolbarBaseProps,
2415
ToolbarButtonPopupData,
@@ -70,42 +61,21 @@ export function ToolbarListButton<E>({
7061
buttonContent.push(<Icon key={3} data={ChevronDown} size={16} />);
7162
}
7263

73-
const titleText: string = isFunction(title) ? title() : title;
74-
7564
return (
7665
<>
77-
<Popover
78-
className={b('action-disabled-popover')}
79-
content={
80-
<div className={b('action-disabled-tooltip')}>
81-
{i18n('toolbar_action_disabled')}
82-
</div>
83-
}
84-
placement={'bottom'}
85-
disabled={!everyDisabled}
66+
<ToolbarButtonView
67+
ref={setAnchorElement}
68+
active={someActive}
69+
enabled={!everyDisabled}
70+
title={title}
71+
className={b({arrow: withArrow}, [className])}
72+
onClick={() => {
73+
if (popupItem) setPopupItem(undefined);
74+
else toggleOpen();
75+
}}
8676
>
87-
<ActionTooltip
88-
title={titleText}
89-
disabled={Boolean(popupItem) || popupOpen}
90-
openDelay={ToolbarTooltipDelay.Open}
91-
closeDelay={ToolbarTooltipDelay.Close}
92-
>
93-
<Button
94-
size="m"
95-
ref={setAnchorElement}
96-
view={someActive || popupOpen ? 'normal' : 'flat'}
97-
selected={someActive}
98-
disabled={everyDisabled}
99-
className={b({arrow: withArrow}, [className])}
100-
onClick={() => {
101-
if (popupItem) setPopupItem(undefined);
102-
else toggleOpen();
103-
}}
104-
>
105-
{buttonContent}
106-
</Button>
107-
</ActionTooltip>
108-
</Popover>
77+
{buttonContent}
78+
</ToolbarButtonView>
10979
<Popup anchorElement={anchorElement} open={popupOpen} onOpenChange={hide}>
11080
<Menu size="l" className={b('menu')}>
11181
{data

0 commit comments

Comments
 (0)