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

chore(compass-components): refactor ItemActionControls #2 #6560

Merged
merged 5 commits into from
Dec 20, 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
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,19 @@ export function DropdownMenuButton<Action extends string>({
const menuTriggerRef = useRef<HTMLButtonElement | null>(null);
const [isMenuOpen, setIsMenuOpen] = useState(false);

const onClick = useCallback(
const onClick: React.MouseEventHandler<HTMLElement> = useCallback(
(evt) => {
evt.stopPropagation();
if (evt.currentTarget.dataset.menuitem) {
setIsMenuOpen(false);
// Workaround for https://jira.mongodb.org/browse/PD-1674
menuTriggerRef.current?.focus();
}
onAction(evt.currentTarget.dataset.action);
const actionName = evt.currentTarget.dataset.action;
if (typeof actionName !== 'string') {
throw new Error('Expected element to have a "data-action" attribute');
}
onAction(actionName as Action);
},
[onAction]
);
Expand Down Expand Up @@ -113,7 +117,7 @@ export function DropdownMenuButton<Action extends string>({
<MenuItem
active={activeAction === action}
key={action}
data-testid={actionTestId<Action>(dataTestId, action)}
data-testid={actionTestId(dataTestId, action)}
data-action={action}
data-menuitem={true}
glyph={<ActionGlyph glyph={icon} size={iconSize} />}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,10 @@
import React from 'react';
import { css, cx } from '@leafygreen-ui/emotion';
import { spacing } from '@leafygreen-ui/tokens';
import { cx } from '@leafygreen-ui/emotion';

import { ItemActionButtonSize } from './constants';
import type { ItemComponentProps } from './types';
import { SmallIconButton } from './small-icon-button';

// TODO: Move to a parent component - or a flex gap
const buttonStyle = css({
'&:not(:first-child)': {
marginLeft: spacing[100],
},
});

export function ItemActionButton<Action extends string>({
action,
icon = <></>,
Expand All @@ -36,7 +28,7 @@ export function ItemActionButton<Action extends string>({
data-action={action}
data-testid={dataTestId}
onClick={onClick}
className={cx(buttonStyle, iconClassName, className)}
className={cx(iconClassName, className)}
style={iconStyle}
disabled={isDisabled}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ describe('item action controls components', function () {
actions={[]}
onAction={() => {}}
data-testid="test-actions"
></ItemActionControls>
/>
);

expect(screen.queryByTestId('test-actions')).to.not.exist;
Expand All @@ -32,7 +32,7 @@ describe('item action controls components', function () {
actions={[{ action: 'copy', label: 'Copy', icon: 'Copy' }]}
onAction={() => {}}
data-testid="test-actions"
></ItemActionControls>
/>
);

expect(screen.getByTestId('test-actions')).to.exist;
Expand All @@ -47,7 +47,7 @@ describe('item action controls components', function () {
onAction={() => {}}
data-testid="test-actions"
collapseToMenuThreshold={1}
></ItemActionControls>
/>
);

const trigger = screen.getByTestId('test-actions-show-actions');
Expand All @@ -69,7 +69,7 @@ describe('item action controls components', function () {
onAction={onAction}
data-testid="test-actions"
collapseToMenuThreshold={3}
></ItemActionControls>
/>
);

expect(onAction).not.to.be.called;
Expand All @@ -90,7 +90,7 @@ describe('item action controls components', function () {
onAction={onAction}
data-testid="test-actions"
collapseToMenuThreshold={1}
></ItemActionControls>
/>
);

expect(onAction).not.to.be.called;
Expand All @@ -112,7 +112,7 @@ describe('item action controls components', function () {
onAction={() => {}}
data-testid="test-actions"
collapseAfter={1}
></ItemActionControls>
/>
);

expect(screen.queryByTestId('test-actions-connect-action')).to.exist;
Expand Down Expand Up @@ -146,7 +146,7 @@ describe('item action controls components', function () {
onAction={() => {}}
data-testid="test-actions"
collapseAfter={1}
></ItemActionControls>
/>
);

const actionButton = screen.getByTestId('test-actions-connect-action');
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useMemo } from 'react';
import React from 'react';
import { spacing } from '@leafygreen-ui/tokens';
import { css, cx } from '@leafygreen-ui/emotion';
import type { RenderMode } from '@leafygreen-ui/popover';
Expand All @@ -13,13 +13,7 @@ const actionControlsStyle = css({
marginLeft: 'auto',
alignItems: 'center',
display: 'flex',
});

// Action buttons are rendered 4px apart from each other. With this we keep the
// same spacing also when action buttons are rendered alongside action menu
// (happens when collapseAfter prop is specified)
const actionMenuWithActionControlsStyles = css({
marginLeft: spacing[100],
gap: spacing[100],
});

export type ItemActionControlsProps<Action extends string> = {
Expand Down Expand Up @@ -53,33 +47,20 @@ export function ItemActionControls<Action extends string>({
collapseToMenuThreshold = 2,
'data-testid': dataTestId,
}: ItemActionControlsProps<Action>) {
const sharedProps = useMemo(
() => ({
isVisible,
onAction,
className: cx('item-action-controls', className),
iconClassName,
iconStyle,
iconSize,
'data-testid': dataTestId,
}),
[
isVisible,
onAction,
className,
iconClassName,
iconStyle,
iconSize,
dataTestId,
]
);
const sharedMenuProps = useMemo(
() => ({
menuClassName,
renderMode,
}),
[menuClassName, renderMode]
);
const sharedProps = {
isVisible,
onAction,
className: cx('item-action-controls', className),
iconClassName,
iconStyle,
iconSize,
'data-testid': dataTestId,
};
const sharedMenuProps = {
menuClassName,
renderMode,
};

if (actions.length === 0) {
return null;
}
Expand All @@ -90,19 +71,12 @@ export function ItemActionControls<Action extends string>({
const collapsedActions = actions.slice(collapseAfter);
return (
<div className={actionControlsStyle}>
<ItemActionGroup
actions={visibleActions}
{...sharedProps}
></ItemActionGroup>
<ItemActionGroup {...sharedProps} actions={visibleActions} />
<ItemActionMenu
actions={collapsedActions}
{...sharedProps}
{...sharedMenuProps}
className={cx(
actionMenuWithActionControlsStyles,
sharedProps.className
)}
></ItemActionMenu>
actions={collapsedActions}
/>
</div>
);
}
Expand All @@ -111,13 +85,9 @@ export function ItemActionControls<Action extends string>({

if (shouldShowMenu) {
return (
<ItemActionMenu
actions={actions}
{...sharedProps}
{...sharedMenuProps}
></ItemActionMenu>
<ItemActionMenu actions={actions} {...sharedProps} {...sharedMenuProps} />
);
}

return <ItemActionGroup actions={actions} {...sharedProps}></ItemActionGroup>;
return <ItemActionGroup actions={actions} {...sharedProps} />;
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,7 @@ const containerStyle = css({
marginLeft: 'auto',
alignItems: 'center',
display: 'flex',
});

// TODO: Move to a parent component - or a flex gap
const actionGroupButtonStyle = css({
'&:not(:first-child)': {
marginLeft: spacing[100],
},
gap: spacing[100],
});

export type ItemActionGroupProps<Action extends string> = {
Expand All @@ -49,10 +43,14 @@ export function ItemActionGroup<Action extends string>({
isVisible = true,
'data-testid': dataTestId,
}: ItemActionGroupProps<Action>) {
const onClick = useCallback(
const onClick: React.MouseEventHandler<HTMLElement> = useCallback(
(evt) => {
evt.stopPropagation();
onAction(evt.currentTarget.dataset.action);
const actionName = evt.currentTarget.dataset.action;
if (typeof actionName !== 'string') {
throw new Error('Expected element to have a "data-action" attribute');
}
onAction(actionName as Action);
},
[onAction]
);
Expand Down Expand Up @@ -85,7 +83,7 @@ export function ItemActionGroup<Action extends string>({
iconStyle={iconStyle}
iconClassName={iconClassName}
onClick={onClick}
data-testid={actionTestId<Action>(dataTestId, itemProps.action)}
data-testid={actionTestId(dataTestId, itemProps.action)}
/>
);

Expand All @@ -94,14 +92,9 @@ export function ItemActionGroup<Action extends string>({
<Tooltip
key={itemProps.action}
{...tooltipProps}
trigger={
<div
className={actionGroupButtonStyle}
style={{ display: 'inherit' }}
>
{item}
</div>
}
// Wrapping the item in a div, because the `trigger` must accept and render `children`
// See docs for the prop for more information
trigger={<div style={{ display: 'inherit' }}>{item}</div>}
>
{tooltip}
</Tooltip>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import React, { useCallback, useRef, useState } from 'react';
import { css, cx } from '@leafygreen-ui/emotion';
import { spacing } from '@leafygreen-ui/tokens';
import type { RenderMode } from '@leafygreen-ui/popover';

import { Menu, MenuItem, MenuSeparator } from '../leafygreen';
Expand Down Expand Up @@ -31,13 +30,6 @@ const containerStyle = css({
display: 'flex',
});

// TODO: Move to a parent component - or a flex gap
const buttonStyle = css({
'&:not(:first-child)': {
marginLeft: spacing[100],
},
});

export type ItemActionMenuProps<Action extends string> = {
actions: MenuAction<Action>[];
onAction(actionName: Action): void;
Expand Down Expand Up @@ -70,15 +62,19 @@ export function ItemActionMenu<Action extends string>({
const menuTriggerRef = useRef<HTMLButtonElement | null>(null);
const [isMenuOpen, setIsMenuOpen] = useState(false);

const onClick = useCallback(
const onClick: React.MouseEventHandler<HTMLElement> = useCallback(
(evt) => {
evt.stopPropagation();
if (evt.currentTarget.dataset.menuitem) {
setIsMenuOpen(false);
// Workaround for https://jira.mongodb.org/browse/PD-1674
menuTriggerRef.current?.focus();
}
onAction(evt.currentTarget.dataset.action);
const actionName = evt.currentTarget.dataset.action;
if (typeof actionName !== 'string') {
throw new Error('Expected element to have a "data-action" attribute');
}
onAction(actionName as Action);
},
[onAction]
);
Expand Down Expand Up @@ -120,7 +116,7 @@ export function ItemActionMenu<Action extends string>({
evt.stopPropagation();
onClick && onClick(evt);
}}
className={cx(buttonStyle, iconClassName)}
className={iconClassName}
style={iconStyle}
>
{children}
Expand All @@ -145,7 +141,7 @@ export function ItemActionMenu<Action extends string>({
return (
<MenuItem
key={action}
data-testid={actionTestId<Action>(dataTestId, action)}
data-testid={actionTestId(dataTestId, action)}
data-action={action}
data-menuitem={true}
glyph={<ActionGlyph glyph={icon} size={iconSize} />}
Expand Down
5 changes: 1 addition & 4 deletions packages/compass-components/src/components/actions/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
export function actionTestId<Action extends string>(
dataTestId: string | undefined,
action: Action
) {
export function actionTestId(dataTestId: string | undefined, action: string) {
return dataTestId ? `${dataTestId}-${action}-action` : undefined;
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
ItemActionControls,
cx,
} from '@mongodb-js/compass-components';
import { ROW_HEIGHT, type Actions } from './constants';
import { type Actions, ROW_HEIGHT } from './constants';
import { ExpandButton } from './tree-item';
import { type NavigationItemActions } from './item-actions';

Expand Down Expand Up @@ -128,13 +128,13 @@ export const NavigationBaseItem: React.FC<NavigationBaseItemProps> = ({
<span title={name}>{name}</span>
</div>
<div className={actionControlsWrapperStyles}>
<ItemActionControls<Actions>
<ItemActionControls
menuClassName={menuStyles}
isVisible={isActive || isHovered || isFocused}
data-testid="sidebar-navigation-item-actions"
iconSize="xsmall"
{...actionProps}
></ItemActionControls>
/>
{children}
</div>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ import {
Button,
type ItemComponentProps,
} from '@mongodb-js/compass-components';
import type { Actions } from './constants';

type ConnectButtonProps = ItemComponentProps<string>;
type ConnectButtonProps = ItemComponentProps<Actions>;

export function ConnectButton({
action,
Expand Down
Loading
Loading