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: fix hover dropdown menu does not display single menu item #1150

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
30 changes: 25 additions & 5 deletions src/components/HoverDropdownMenu/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import Popover from '../Popover';
import PopoverLinkItem from './PopoverLinkItem';
import './styles.scss';

const HoverDropdownMenu = ({ arrowPosition, headerText, hoverComponent, children }) => {
const HoverDropdownMenu = ({ arrowPosition, headerText, popoverClassNames, hoverComponent, children }) => {
const [popperNode, setPopperNode] = React.useState(null);
const [isOpen, setIsOpen] = React.useState(false);
const [mouseInPopover, setMouseInPopover] = React.useState(false);
Expand Down Expand Up @@ -38,16 +38,32 @@ const HoverDropdownMenu = ({ arrowPosition, headerText, hoverComponent, children
</div>
);

let placement;
switch (arrowPosition) {
case 'left':
placement = 'bottom-start';
break;
case 'right':
placement = 'bottom-end';
break;
case 'none':
default:
placement = 'bottom';
break;
}

return (
<div data-testid="hover-dropdown-wrapper" className="hover-dropdown">
{children && children.length > 0 ? (
{children ? (
<Popover
placement={`bottom-${arrowPosition === 'left' ? 'start' : 'end'}`}
popoverClassNames={popoverClassNames}
placement={placement}
triggers={['disabled']}
isOpen={isOpen || mouseInPopover}
title={headerText}
popoverContent={<ul className="list-unstyled">{children}</ul>}
popperRef={setPopperNode}
arrowStyles={arrowPosition === 'none' ? { display: 'none' } : null}
Copy link
Contributor

Choose a reason for hiding this comment

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

maybe add a modifiers props here as well, in case you want to customize it later

Copy link
Contributor

Choose a reason for hiding this comment

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

you can either expose popoverModifiers or considering pre-defined modifiers like set the width of the popper as the width of the reference

Copy link
Contributor

Choose a reason for hiding this comment

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

sorry, I just found we have a props named popoverProps in the <TextEllipsis /> so maybe pass popoverProps to the <Popover />

>
{element}
</Popover>
Expand All @@ -57,15 +73,19 @@ const HoverDropdownMenu = ({ arrowPosition, headerText, hoverComponent, children
};

HoverDropdownMenu.propTypes = {
/**
* allow more styling for popover
*/
popoverClassNames: PropTypes.string,
/**
* Determine the placement of the popover
*/
arrowPosition: PropTypes.oneOf(['left', 'right']),
arrowPosition: PropTypes.oneOf(['left', 'right', 'none']),
/**
* If set to empty string, header will not be rendered.
*/
headerText: PropTypes.string,
hoverComponent: PropTypes.element.isRequired,
hoverComponent: PropTypes.node.isRequired,
children: PropTypes.node,
};

Expand Down
19 changes: 19 additions & 0 deletions src/components/HoverDropdownMenu/index.spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -149,4 +149,23 @@ describe('<HoverDropdownMenu />', () => {
const { queryAllByTestId } = render(<HoverDropdownMenu hoverComponent={<div />} />);
expect(queryAllByTestId('popover-wrapper')).toHaveLength(0);
});

it('should not render arrow if arrow position is none', () => {
const { getByTestId, queryAllByTestId } = render(
<HoverDropdownMenu {...props} arrowPosition="none">
{_.map(links, (link, idx) => (
<HoverDropdownMenu.Item key={idx} {...link} />
))}
</HoverDropdownMenu>
);

act(() => {
fireEvent.mouseEnter(getByTestId('hover-dropdown-element'));
jest.runAllTimers();
});
expect(queryAllByTestId('popover-wrapper')[0].querySelector('.aui--popover-arrow')).toHaveAttribute(
'style',
'display: none;'
);
});
});
13 changes: 12 additions & 1 deletion www/containers/props.json
Original file line number Diff line number Diff line change
Expand Up @@ -1862,6 +1862,13 @@
"displayName": "HoverDropdownMenu",
"methods": [],
"props": {
"popoverClassNames": {
"type": {
"name": "string"
},
"required": false,
"description": "allow more styling for popover"
},
"arrowPosition": {
"type": {
"name": "enum",
Expand All @@ -1873,6 +1880,10 @@
{
"value": "'right'",
"computed": false
},
{
"value": "'none'",
"computed": false
}
]
},
Expand All @@ -1896,7 +1907,7 @@
},
"hoverComponent": {
"type": {
"name": "element"
"name": "node"
},
"required": true,
"description": ""
Expand Down
18 changes: 13 additions & 5 deletions www/examples/HoverDropdownMenu.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,19 @@ class Example extends React.PureComponent {
return (
<>
<h5>Hover the avatar below</h5>
<HoverDropdownMenu {...props}>
{_.map(links, link => (
<HoverDropdownMenu.Item key={link.title} {...link} />
))}
</HoverDropdownMenu>
<div style={{ display: 'flex', alignItems: 'center' }}>
<HoverDropdownMenu {...props}>
{_.map(links, link => (
<HoverDropdownMenu.Item key={link.title} {...link} />
))}
</HoverDropdownMenu>

<div style={{ width: '12px' }} />

<HoverDropdownMenu hoverComponent="Single menu item" arrowPosition="none">
<HoverDropdownMenu.Item {...links[0]} />
</HoverDropdownMenu>
</div>
</>
);
}
Expand Down