2018-04-12 14:15:44 +00:00
|
|
|
// @flow
|
|
|
|
|
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';
|
2021-02-04 13:24:25 +00:00
|
|
|
import { Tooltip } from '../../tooltip';
|
2019-08-30 16:39:06 +00:00
|
|
|
|
2018-04-12 14:15:44 +00:00
|
|
|
import AbstractToolboxItem from './AbstractToolboxItem';
|
2022-01-04 11:21:00 +00:00
|
|
|
import type { Props as AbstractToolboxItemProps } from './AbstractToolboxItem';
|
|
|
|
|
|
|
|
type Props = AbstractToolboxItemProps & {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* On key down handler.
|
|
|
|
*/
|
|
|
|
onKeyDown: Function
|
|
|
|
};
|
2018-04-12 14:15:44 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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);
|
|
|
|
|
2021-06-10 12:48:44 +00:00
|
|
|
this._onKeyPress = this._onKeyPress.bind(this);
|
2020-04-16 01:06:11 +00:00
|
|
|
}
|
|
|
|
|
2021-06-10 12:48:44 +00:00
|
|
|
_onKeyPress: (Object) => void;
|
2020-04-16 01:06:11 +00:00
|
|
|
|
|
|
|
/**
|
2021-06-10 12:48:44 +00:00
|
|
|
* Handles 'Enter' and Space key on the button to trigger onClick for accessibility.
|
2020-04-16 01:06:11 +00:00
|
|
|
*
|
|
|
|
* @param {Object} event - The key event.
|
|
|
|
* @private
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
2021-06-10 12:48:44 +00:00
|
|
|
_onKeyPress(event) {
|
|
|
|
if (event.key === 'Enter' || event.key === ' ') {
|
2020-04-16 01:06:11 +00:00
|
|
|
event.preventDefault();
|
|
|
|
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,
|
2022-01-04 11:21:00 +00:00
|
|
|
onKeyDown,
|
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,
|
2022-01-04 11:21:00 +00:00
|
|
|
onKeyDown: disabled ? undefined : onKeyDown,
|
2021-06-10 12:48:44 +00:00
|
|
|
onKeyPress: this._onKeyPress,
|
2020-04-16 01:06:11 +00:00
|
|
|
tabIndex: 0,
|
2021-06-10 12:48:44 +00:00
|
|
|
role: showLabel ? 'menuitem' : '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
|
|
|
}
|
|
|
|
}
|