Skip to content

Commit

Permalink
fix(Dashboard): Add border to row when hovering HoverMenu in edit mode (
Browse files Browse the repository at this point in the history
  • Loading branch information
rtexelm authored Apr 4, 2024
1 parent ebcf4e0 commit 265390c
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,11 @@ const DashboardContentWrapper = styled.div`
pointer-events: none;
}
.grid-row.grid-row--hovered:after,
.dashboard-component-tabs > .grid-row--hovered:after {
border: 2px dashed ${theme.colors.primary.base};
}
.resizable-container {
& .dashboard-component-chart-holder {
.dashboard-chart {
Expand Down
16 changes: 14 additions & 2 deletions superset-frontend/src/dashboard/components/gridComponents/Row.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ class Row extends React.PureComponent {
this.state = {
isFocused: false,
isInView: false,
hoverMenuHovered: false,
};
this.handleDeleteComponent = this.handleDeleteComponent.bind(this);
this.handleUpdateMeta = this.handleUpdateMeta.bind(this);
Expand All @@ -143,6 +144,7 @@ class Row extends React.PureComponent {
'background',
);
this.handleChangeFocus = this.handleChangeFocus.bind(this);
this.handleMenuHover = this.handleMenuHover.bind(this);
this.setVerticalEmptyContainerHeight = debounce(
this.setVerticalEmptyContainerHeight.bind(this),
FAST_DEBOUNCE,
Expand Down Expand Up @@ -235,6 +237,11 @@ class Row extends React.PureComponent {
deleteComponent(component.id, parentId);
}

handleMenuHover = hovered => {
const { isHovered } = hovered;
this.setState(() => ({ hoverMenuHovered: isHovered }));
};

render() {
const {
component: rowComponent,
Expand All @@ -252,7 +259,7 @@ class Row extends React.PureComponent {
onChangeTab,
isComponentVisible,
} = this.props;
const { containerHeight } = this.state;
const { containerHeight, hoverMenuHovered } = this.state;

const rowItems = rowComponent.children || [];

Expand Down Expand Up @@ -287,7 +294,11 @@ class Row extends React.PureComponent {
editMode={editMode}
>
{editMode && (
<HoverMenu innerRef={dragSourceRef} position="left">
<HoverMenu
onHover={this.handleMenuHover}
innerRef={dragSourceRef}
position="left"
>
<DragHandle position="left" />
<DeleteComponentButton onDelete={this.handleDeleteComponent} />
<IconButton
Expand All @@ -300,6 +311,7 @@ class Row extends React.PureComponent {
className={cx(
'grid-row',
rowItems.length === 0 && 'grid-row--empty',
hoverMenuHovered && 'grid-row--hovered',
backgroundStyle.className,
)}
data-test={`grid-row-${backgroundStyle.className}`}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,25 @@
* under the License.
*/
import React from 'react';
import { render } from 'spec/helpers/testing-library';
import { render, screen } from 'spec/helpers/testing-library';
import userEvent from '@testing-library/user-event';

import HoverMenu from 'src/dashboard/components/menu/HoverMenu';

test('should render a div.hover-menu', () => {
const { container } = render(<HoverMenu />);
expect(container.querySelector('.hover-menu')).toBeInTheDocument();
});

test('should call onHover when mouse enters and leaves', () => {
const onHover = jest.fn();
render(<HoverMenu onHover={onHover} />);

const hoverMenu = screen.getByTestId('hover-menu');

userEvent.hover(hoverMenu);
expect(onHover).toBeCalledWith({ isHovered: true });

userEvent.unhover(hoverMenu);
expect(onHover).toBeCalledWith({ isHovered: false });
});
19 changes: 19 additions & 0 deletions superset-frontend/src/dashboard/components/menu/HoverMenu.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable react/no-unused-state */
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
Expand All @@ -24,6 +25,7 @@ interface HoverMenuProps {
position: 'left' | 'top';
innerRef: RefObject<HTMLDivElement>;
children: React.ReactNode;
onHover?: (data: { isHovered: boolean }) => void;
}

const HoverStyleOverrides = styled.div`
Expand Down Expand Up @@ -70,6 +72,20 @@ export default class HoverMenu extends React.PureComponent<HoverMenuProps> {
children: null,
};

handleMouseEnter = () => {
const { onHover } = this.props;
if (onHover) {
onHover({ isHovered: true });
}
};

handleMouseLeave = () => {
const { onHover } = this.props;
if (onHover) {
onHover({ isHovered: false });
}
};

render() {
const { innerRef, position, children } = this.props;
return (
Expand All @@ -81,6 +97,9 @@ export default class HoverMenu extends React.PureComponent<HoverMenuProps> {
position === 'left' && 'hover-menu--left',
position === 'top' && 'hover-menu--top',
)}
onMouseEnter={this.handleMouseEnter}
onMouseLeave={this.handleMouseLeave}
data-test="hover-menu"
>
{children}
</div>
Expand Down

0 comments on commit 265390c

Please sign in to comment.