2018-04-12 14:15:44 +00:00
|
|
|
// @flow
|
|
|
|
|
|
|
|
import Tooltip from '@atlaskit/tooltip';
|
2018-05-22 14:22:16 +00:00
|
|
|
import React, { Fragment } from 'react';
|
2018-04-12 14:15:44 +00:00
|
|
|
|
2019-08-30 16:39:06 +00:00
|
|
|
import { Icon } from '../../icons';
|
|
|
|
|
2018-04-12 14:15:44 +00:00
|
|
|
import AbstractToolboxItem from './AbstractToolboxItem';
|
|
|
|
import type { Props } from './AbstractToolboxItem';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Web implementation of {@code AbstractToolboxItem}.
|
|
|
|
*/
|
|
|
|
export default class ToolboxItem extends AbstractToolboxItem<Props> {
|
2020-04-16 01:06:11 +00:00
|
|
|
/**
|
|
|
|
* Initializes a new {@code ToolboxItem} instance.
|
|
|
|
*
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
|
|
|
constructor(props: Props) {
|
|
|
|
super(props);
|
|
|
|
|
|
|
|
this._onKeyDown = this._onKeyDown.bind(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
_onKeyDown: (Object) => void;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handles 'Enter' key on the button to trigger onClick for accessibility.
|
|
|
|
* We should be handling Space onKeyUp but it conflicts with PTT.
|
|
|
|
*
|
|
|
|
* @param {Object} event - The key event.
|
|
|
|
* @private
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
_onKeyDown(event) {
|
|
|
|
// If the event coming to the dialog has been subject to preventDefault
|
|
|
|
// we don't handle it here.
|
|
|
|
if (event.defaultPrevented) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (event.key === 'Enter') {
|
|
|
|
event.preventDefault();
|
|
|
|
event.stopPropagation();
|
|
|
|
this.props.onClick();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-12 14:15:44 +00:00
|
|
|
/**
|
|
|
|
* Handles rendering of the actual item. If the label is being shown, which
|
|
|
|
* is controlled with the `showLabel` prop, the item is rendered for its
|
|
|
|
* display in an overflow menu, otherwise it will only have an icon, which
|
|
|
|
* can be displayed on any toolbar.
|
|
|
|
*
|
|
|
|
* @protected
|
|
|
|
* @returns {ReactElement}
|
|
|
|
*/
|
|
|
|
_renderItem() {
|
|
|
|
const {
|
2018-06-28 13:25:58 +00:00
|
|
|
disabled,
|
2018-07-17 02:15:34 +00:00
|
|
|
elementAfter,
|
2018-04-12 14:15:44 +00:00
|
|
|
onClick,
|
2018-06-28 13:25:58 +00:00
|
|
|
showLabel,
|
2020-04-16 01:06:11 +00:00
|
|
|
tooltipPosition,
|
|
|
|
toggled
|
2018-04-12 14:15:44 +00:00
|
|
|
} = this.props;
|
2018-06-28 13:25:58 +00:00
|
|
|
const className = showLabel ? 'overflow-menu-item' : 'toolbox-button';
|
2018-04-12 14:15:44 +00:00
|
|
|
const props = {
|
2020-04-16 01:06:11 +00:00
|
|
|
'aria-pressed': toggled,
|
|
|
|
'aria-disabled': disabled,
|
2018-06-07 20:32:18 +00:00
|
|
|
'aria-label': this.accessibilityLabel,
|
2018-06-28 13:25:58 +00:00
|
|
|
className: className + (disabled ? ' disabled' : ''),
|
2020-04-16 01:06:11 +00:00
|
|
|
onClick: disabled ? undefined : onClick,
|
|
|
|
onKeyDown: this._onKeyDown,
|
|
|
|
tabIndex: 0,
|
|
|
|
role: 'button'
|
2018-04-12 14:15:44 +00:00
|
|
|
};
|
2020-04-16 01:06:11 +00:00
|
|
|
|
2018-04-12 14:15:44 +00:00
|
|
|
const elementType = showLabel ? 'li' : 'div';
|
2018-06-28 13:25:58 +00:00
|
|
|
const useTooltip = this.tooltip && this.tooltip.length > 0;
|
|
|
|
let children = (
|
2018-05-22 14:22:16 +00:00
|
|
|
<Fragment>
|
2018-04-12 14:15:44 +00:00
|
|
|
{ this._renderIcon() }
|
2018-06-28 13:25:58 +00:00
|
|
|
{ showLabel && <span>
|
|
|
|
{ this.label }
|
|
|
|
</span> }
|
2018-07-17 02:15:34 +00:00
|
|
|
{ elementAfter }
|
2018-05-22 14:22:16 +00:00
|
|
|
</Fragment>
|
2018-04-12 14:15:44 +00:00
|
|
|
);
|
|
|
|
|
2018-06-28 13:25:58 +00:00
|
|
|
if (useTooltip) {
|
|
|
|
children = (
|
|
|
|
<Tooltip
|
|
|
|
content = { this.tooltip }
|
|
|
|
position = { tooltipPosition }>
|
|
|
|
{ children }
|
|
|
|
</Tooltip>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-04-12 14:15:44 +00:00
|
|
|
return React.createElement(elementType, props, children);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper function to render the item's icon.
|
|
|
|
*
|
|
|
|
* @private
|
|
|
|
* @returns {ReactElement}
|
|
|
|
*/
|
|
|
|
_renderIcon() {
|
2020-04-01 07:47:51 +00:00
|
|
|
const { customClass, disabled, icon, showLabel, toggled } = this.props;
|
2019-08-30 16:39:06 +00:00
|
|
|
const iconComponent = <Icon src = { icon } />;
|
2018-04-12 14:15:44 +00:00
|
|
|
const elementType = showLabel ? 'span' : 'div';
|
2019-08-30 16:39:06 +00:00
|
|
|
const className = `${showLabel ? 'overflow-menu-item-icon' : 'toolbox-icon'} ${
|
2020-04-01 07:47:51 +00:00
|
|
|
toggled ? 'toggled' : ''} ${disabled ? 'disabled' : ''} ${customClass ?? ''}`;
|
2018-04-12 14:15:44 +00:00
|
|
|
|
2019-08-30 16:39:06 +00:00
|
|
|
return React.createElement(elementType, { className }, iconComponent);
|
2018-04-12 14:15:44 +00:00
|
|
|
}
|
|
|
|
}
|