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

Add tooltips to context menu #817

Merged
merged 5 commits into from
May 11, 2018
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

- Added utility CSS classes for text and alignment concerns. ([#774](https://github.com/elastic/eui/pull/774))
- Added `compressed` versions of `EuiFormRow` and all form controls. ([#800](https://github.com/elastic/eui/pull/800))
- Added the ability to add tooltips to `EuiContextMenuItem`s. ([#817](https://github.com/elastic/eui/pull/817))

**Bug fixes**

Expand Down
5 changes: 4 additions & 1 deletion src-docs/src/views/context_menu/context_menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,11 @@ export default class extends Component {
icon: 'user',
onClick: () => { this.closePopover(); window.alert('Edit / add panels'); },
}, {
name: 'Display options',
name: 'You can add a tooltip',
icon: 'user',
toolTipTitle: 'Optional tooltip',
toolTipContent: 'Optional content for a tooltip',
toolTipPosition: 'right',
onClick: () => { this.closePopover(); window.alert('Display options'); },
}, {
name: 'Disabled option',
Expand Down
4 changes: 3 additions & 1 deletion src-docs/src/views/context_menu/context_menu_example.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import {
import {
EuiCode,
EuiContextMenu,
EuiContextMenuPanel,
EuiContextMenuItem,
} from '../../../../src/components';

import ContextMenu from './context_menu';
Expand Down Expand Up @@ -36,7 +38,7 @@ export const ContextMenuExample = {
which itself can be wrapped around any component (like a button in this example).
</p>
),
props: { EuiContextMenu },
props: { EuiContextMenu, EuiContextMenuPanel, EuiContextMenuItem },
demo: <ContextMenu />,
}, {
title: `With single panel`,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,13 @@ exports[`CollapsedItemActions render 1`] = `
disabled={false}
icon={undefined}
onClick={[Function]}
toolTipPosition="right"
>
default1
</EuiContextMenuItem>,
<EuiContextMenuItem
onClick={[Function]}
toolTipPosition="right"
/>,
]
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ exports[`EuiContextMenuPanel updating items and content updates to items should
"<EuiContextMenuPanel items={{...}} hasFocus={true}>
<div className=\\"euiContextMenuPanel\\" onKeyDown={[Function]} tabIndex=\\"0\\" onAnimationEnd={[Function]}>
<div>
<EuiContextMenuItem data-counter={0} buttonRef={[Function: bound ]}>
<EuiContextMenuItem data-counter={0} toolTipPosition=\\"right\\" buttonRef={[Function: bound ]}>
<button className=\\"euiContextMenuItem\\" type=\\"button\\" disabled={[undefined]} data-counter={0}>
<span className=\\"euiContextMenu__itemLayout\\">
<span className=\\"euiContextMenuItem__text\\">
Expand All @@ -124,7 +124,7 @@ exports[`EuiContextMenuPanel updating items and content updates to items should
</span>
</button>
</EuiContextMenuItem>
<EuiContextMenuItem data-counter={1} buttonRef={[Function: bound ]}>
<EuiContextMenuItem data-counter={1} toolTipPosition=\\"right\\" buttonRef={[Function: bound ]}>
<button className=\\"euiContextMenuItem\\" type=\\"button\\" disabled={[undefined]} data-counter={1}>
<span className=\\"euiContextMenu__itemLayout\\">
<span className=\\"euiContextMenuItem__text\\">
Expand All @@ -142,7 +142,7 @@ exports[`EuiContextMenuPanel updating items and content updates to items should
"<EuiContextMenuPanel items={{...}} hasFocus={true}>
<div className=\\"euiContextMenuPanel\\" onKeyDown={[Function]} tabIndex=\\"0\\" onAnimationEnd={[Function]}>
<div>
<EuiContextMenuItem data-counter={0} buttonRef={[Function: bound ]}>
<EuiContextMenuItem data-counter={0} toolTipPosition=\\"right\\" buttonRef={[Function: bound ]}>
<button className=\\"euiContextMenuItem\\" type=\\"button\\" disabled={[undefined]} data-counter={0}>
<span className=\\"euiContextMenu__itemLayout\\">
<span className=\\"euiContextMenuItem__text\\">
Expand All @@ -151,7 +151,7 @@ exports[`EuiContextMenuPanel updating items and content updates to items should
</span>
</button>
</EuiContextMenuItem>
<EuiContextMenuItem data-counter={1} buttonRef={[Function: bound ]}>
<EuiContextMenuItem data-counter={1} toolTipPosition=\\"right\\" buttonRef={[Function: bound ]}>
<button className=\\"euiContextMenuItem\\" type=\\"button\\" disabled={[undefined]} data-counter={1}>
<span className=\\"euiContextMenu__itemLayout\\">
<span className=\\"euiContextMenuItem__text\\">
Expand Down Expand Up @@ -189,7 +189,7 @@ exports[`EuiContextMenuPanel updating items and content updates to items should
"<EuiContextMenuPanel watchedItemProps={{...}} items={{...}} hasFocus={true}>
<div className=\\"euiContextMenuPanel\\" onKeyDown={[Function]} tabIndex=\\"0\\" onAnimationEnd={[Function]}>
<div>
<EuiContextMenuItem data-counter={0} buttonRef={[Function: bound ]}>
<EuiContextMenuItem data-counter={0} toolTipPosition=\\"right\\" buttonRef={[Function: bound ]}>
<button className=\\"euiContextMenuItem\\" type=\\"button\\" disabled={[undefined]} data-counter={0}>
<span className=\\"euiContextMenu__itemLayout\\">
<span className=\\"euiContextMenuItem__text\\">
Expand All @@ -198,7 +198,7 @@ exports[`EuiContextMenuPanel updating items and content updates to items should
</span>
</button>
</EuiContextMenuItem>
<EuiContextMenuItem data-counter={1} buttonRef={[Function: bound ]}>
<EuiContextMenuItem data-counter={1} toolTipPosition=\\"right\\" buttonRef={[Function: bound ]}>
<button className=\\"euiContextMenuItem\\" type=\\"button\\" disabled={[undefined]} data-counter={1}>
<span className=\\"euiContextMenu__itemLayout\\">
<span className=\\"euiContextMenuItem__text\\">
Expand All @@ -216,7 +216,7 @@ exports[`EuiContextMenuPanel updating items and content updates to items should
"<EuiContextMenuPanel watchedItemProps={{...}} items={{...}} hasFocus={true}>
<div className=\\"euiContextMenuPanel\\" onKeyDown={[Function]} tabIndex=\\"0\\" onAnimationEnd={[Function]}>
<div>
<EuiContextMenuItem data-counter={2} buttonRef={[Function: bound ]}>
<EuiContextMenuItem data-counter={2} toolTipPosition=\\"right\\" buttonRef={[Function: bound ]}>
<button className=\\"euiContextMenuItem\\" type=\\"button\\" disabled={[undefined]} data-counter={2}>
<span className=\\"euiContextMenu__itemLayout\\">
<span className=\\"euiContextMenuItem__text\\">
Expand All @@ -225,7 +225,7 @@ exports[`EuiContextMenuPanel updating items and content updates to items should
</span>
</button>
</EuiContextMenuItem>
<EuiContextMenuItem data-counter={3} buttonRef={[Function: bound ]}>
<EuiContextMenuItem data-counter={3} toolTipPosition=\\"right\\" buttonRef={[Function: bound ]}>
<button className=\\"euiContextMenuItem\\" type=\\"button\\" disabled={[undefined]} data-counter={3}>
<span className=\\"euiContextMenu__itemLayout\\">
<span className=\\"euiContextMenuItem__text\\">
Expand Down
4 changes: 4 additions & 0 deletions src/components/context_menu/context_menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,8 @@ export class EuiContextMenu extends Component {
name,
icon,
onClick,
toolTipTitle,
toolTipContent,
...rest
} = item;

Expand All @@ -221,6 +223,8 @@ export class EuiContextMenu extends Component {
icon={icon}
onClick={onClickHandler}
hasPanel={Boolean(panel)}
toolTipTitle={toolTipTitle}
toolTipContent={toolTipContent}
{...rest}
>
{name}
Expand Down
47 changes: 46 additions & 1 deletion src/components/context_menu/context_menu_item.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,49 @@ import PropTypes from 'prop-types';
import classNames from 'classnames';

import { EuiIcon } from '../icon';
import { EuiToolTip } from '../tool_tip';

export class EuiContextMenuItem extends Component {
static propTypes = {
children: PropTypes.node,
className: PropTypes.string,
/**
* Icon used for the item
*/
icon: PropTypes.oneOfType([PropTypes.element, PropTypes.string]),
onClick: PropTypes.func,
/**
* Whether the item leads to a new set of items
*/
hasPanel: PropTypes.bool,
buttonRef: PropTypes.func,
disabled: PropTypes.bool,
/**
* Required if using a tooltip. Add an optional tooltip on hover
*/
toolTipContent: PropTypes.node,
/**
* Optional title for the tooltip
*/
toolTipTitle: PropTypes.node,
/**
* Dictates the position of the tooltip.
*/
toolTipPosition: PropTypes.string,
};

render() {

const {
children,
className,
hasPanel,
icon,
buttonRef,
disabled,
toolTipTitle,
toolTipContent,
toolTipPosition,
...rest
} = this.props;

Expand Down Expand Up @@ -67,7 +90,7 @@ export class EuiContextMenuItem extends Component {
'euiContextMenuItem-isDisabled': disabled,
});

return (
const button = (
<button
className={classes}
type="button"
Expand All @@ -84,5 +107,27 @@ export class EuiContextMenuItem extends Component {
</span>
</button>
);

if (toolTipContent) {
return (
<EuiToolTip
title={toolTipTitle ? toolTipTitle : null}
content={toolTipContent}
anchorClassName="eui-displayBlock"
position={toolTipPosition}
>
{button}
</EuiToolTip>
);
} else {
return (
button
);
}

}
}

EuiContextMenuItem.defaultProps = {
toolTipPosition: "right",
};
8 changes: 7 additions & 1 deletion src/components/tool_tip/tool_tip.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ export class EuiToolTip extends Component {
const {
children,
className,
anchorClassName,
content,
title,
...rest
Expand All @@ -99,6 +100,11 @@ export class EuiToolTip extends Component {
className
);

const anchorClasses = classNames(
'euiToolTipAnchor',
anchorClassName,
);

let tooltip;
if (this.state.visible) {
tooltip = (
Expand All @@ -121,7 +127,7 @@ export class EuiToolTip extends Component {
const anchor = (
<span
ref={anchor => this.anchor = anchor}
className="euiToolTipAnchor"
className={anchorClasses}
>
{cloneElement(children, {
onFocus: this.showToolTip,
Expand Down