2017-08-25 07:47:20 +08:00
|
|
|
import React, { Component } from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
2017-11-16 08:34:50 +08:00
|
|
|
import _ from 'lodash';
|
2021-11-03 01:11:41 +08:00
|
|
|
import Styled from './styles';
|
2017-08-25 07:47:20 +08:00
|
|
|
|
2017-09-23 14:27:55 +08:00
|
|
|
export default class ToolbarMenuItem extends Component {
|
2017-08-25 07:47:20 +08:00
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
|
2018-01-23 07:07:29 +08:00
|
|
|
// a flag to keep track of whether the menu item was actually clicked
|
|
|
|
this.clicked = false;
|
2020-07-16 03:52:34 +08:00
|
|
|
|
2017-11-16 08:34:50 +08:00
|
|
|
this.handleTouchStart = this.handleTouchStart.bind(this);
|
|
|
|
this.handleOnMouseUp = this.handleOnMouseUp.bind(this);
|
2018-01-23 07:07:29 +08:00
|
|
|
this.handleOnMouseDown = this.handleOnMouseDown.bind(this);
|
2017-11-16 08:34:50 +08:00
|
|
|
this.setRef = this.setRef.bind(this);
|
2017-08-25 07:47:20 +08:00
|
|
|
|
2020-07-16 03:52:34 +08:00
|
|
|
this.uniqueRef = _.uniqueId('toolbar-menu-item');
|
|
|
|
}
|
|
|
|
|
2017-11-16 08:34:50 +08:00
|
|
|
componentDidMount() {
|
|
|
|
// adding and removing touchstart events can be done via standard React way
|
|
|
|
// by passing onTouchStart={this.funcName} once they stop triggering mousedown events
|
|
|
|
// see https://github.com/facebook/react/issues/9809
|
|
|
|
this[this.uniqueRef].addEventListener('touchstart', this.handleTouchStart);
|
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
|
|
|
this[this.uniqueRef].removeEventListener('touchstart', this.handleTouchStart);
|
|
|
|
}
|
|
|
|
|
|
|
|
setRef(ref) {
|
|
|
|
this[this.uniqueRef] = ref;
|
|
|
|
}
|
|
|
|
|
2017-11-22 08:51:40 +08:00
|
|
|
// we have to use touchStart and on mouseUp in order to be able to use the toolbar
|
|
|
|
// with the text shape on mobile devices
|
|
|
|
// (using the toolbar while typing text shouldn't move focus out of the textarea)
|
2017-11-16 08:34:50 +08:00
|
|
|
handleTouchStart(event) {
|
|
|
|
event.preventDefault();
|
|
|
|
const { objectToReturn, onItemClick } = this.props;
|
|
|
|
// if there is a submenu name, then pass it to onClick
|
|
|
|
// if not - it's probably "Undo", "Clear All", "Multi-user", etc.
|
|
|
|
// in the second case we'll pass undefined and it will work fine anyway
|
|
|
|
onItemClick(objectToReturn);
|
|
|
|
}
|
|
|
|
|
2018-01-23 07:07:29 +08:00
|
|
|
handleOnMouseDown() {
|
|
|
|
this.clicked = true;
|
|
|
|
}
|
|
|
|
|
2017-11-16 08:34:50 +08:00
|
|
|
handleOnMouseUp() {
|
2018-01-23 07:07:29 +08:00
|
|
|
// checks whether the button was actually clicked
|
|
|
|
// or if a person was drawing and just release their mouse above the menu item
|
|
|
|
if (!this.clicked) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.clicked = false;
|
|
|
|
|
2017-08-25 07:47:20 +08:00
|
|
|
const { objectToReturn, onItemClick } = this.props;
|
|
|
|
// if there is a submenu name, then pass it to onClick
|
2017-09-21 05:05:17 +08:00
|
|
|
// if not - it's probably "Undo", "Clear All", "Multi-user", etc.
|
|
|
|
// in the second case we'll pass undefined and it will work fine anyway
|
|
|
|
onItemClick(objectToReturn);
|
2017-08-25 07:47:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
2019-05-28 00:34:37 +08:00
|
|
|
const {
|
|
|
|
disabled,
|
|
|
|
label,
|
|
|
|
icon,
|
|
|
|
customIcon,
|
|
|
|
onBlur,
|
|
|
|
children,
|
2019-06-14 06:42:20 +08:00
|
|
|
showCornerTriangle,
|
2021-09-13 21:40:22 +08:00
|
|
|
expanded,
|
2021-09-13 22:03:40 +08:00
|
|
|
haspopup,
|
2022-01-20 21:03:18 +08:00
|
|
|
'data-test': dataTest,
|
2019-05-28 00:34:37 +08:00
|
|
|
} = this.props;
|
|
|
|
|
2017-08-25 07:47:20 +08:00
|
|
|
return (
|
2021-11-03 01:11:41 +08:00
|
|
|
<Styled.ButtonWrapper
|
|
|
|
showCornerTriangle={showCornerTriangle}
|
2021-11-11 19:34:45 +08:00
|
|
|
className={"toolbarButtonWrapper"}
|
2019-06-14 06:42:20 +08:00
|
|
|
hidden={disabled}
|
|
|
|
>
|
2021-11-03 01:11:41 +08:00
|
|
|
<Styled.ToolbarButton
|
2021-11-03 04:05:49 +08:00
|
|
|
state={expanded ? 'active' : 'inactive'}
|
2021-11-11 19:34:45 +08:00
|
|
|
className={expanded ? 'toolbarActive' : ''}
|
2021-09-13 21:40:22 +08:00
|
|
|
aria-expanded={expanded}
|
2021-09-13 22:03:40 +08:00
|
|
|
aria-haspopup={haspopup}
|
2022-01-20 21:03:18 +08:00
|
|
|
data-test={dataTest}
|
2017-08-25 07:47:20 +08:00
|
|
|
hideLabel
|
|
|
|
role="button"
|
2017-11-16 08:34:50 +08:00
|
|
|
color="default"
|
|
|
|
size="md"
|
2019-05-28 00:34:37 +08:00
|
|
|
label={label}
|
|
|
|
icon={icon || null}
|
|
|
|
customIcon={customIcon || null}
|
2018-01-23 07:07:29 +08:00
|
|
|
onMouseDown={this.handleOnMouseDown}
|
2017-11-16 08:34:50 +08:00
|
|
|
onMouseUp={this.handleOnMouseUp}
|
2019-05-28 00:34:37 +08:00
|
|
|
onKeyPress={this.handleOnMouseDown}
|
|
|
|
onKeyUp={this.handleOnMouseUp}
|
|
|
|
onBlur={onBlur}
|
2017-11-16 08:34:50 +08:00
|
|
|
setRef={this.setRef}
|
2019-05-28 00:34:37 +08:00
|
|
|
disabled={disabled}
|
2017-08-25 07:47:20 +08:00
|
|
|
/>
|
2019-05-28 00:34:37 +08:00
|
|
|
{children}
|
2021-11-03 01:11:41 +08:00
|
|
|
</Styled.ButtonWrapper>
|
2017-08-25 07:47:20 +08:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-23 14:27:55 +08:00
|
|
|
ToolbarMenuItem.propTypes = {
|
|
|
|
// objectToReturn, children and onBlur are passed only with menu items that have submenus
|
|
|
|
// thus they are optional
|
|
|
|
onBlur: PropTypes.func,
|
|
|
|
children: PropTypes.node,
|
2017-08-25 07:47:20 +08:00
|
|
|
objectToReturn: PropTypes.oneOfType([
|
|
|
|
PropTypes.string,
|
|
|
|
PropTypes.object,
|
2017-09-06 06:55:18 +08:00
|
|
|
PropTypes.number,
|
2017-08-25 07:47:20 +08:00
|
|
|
]),
|
|
|
|
onItemClick: PropTypes.func.isRequired,
|
2017-09-23 14:27:55 +08:00
|
|
|
// we can have either icon from the bigbluebutton-font or our custom svg/html
|
|
|
|
// thus they are optional
|
2017-08-25 07:47:20 +08:00
|
|
|
icon: PropTypes.string,
|
|
|
|
customIcon: PropTypes.node,
|
|
|
|
label: PropTypes.string.isRequired,
|
2021-11-03 01:11:41 +08:00
|
|
|
toolbarActive: PropTypes.bool,
|
2019-05-28 00:34:37 +08:00
|
|
|
disabled: PropTypes.bool,
|
2019-06-14 06:42:20 +08:00
|
|
|
showCornerTriangle: PropTypes.bool,
|
2017-08-25 07:47:20 +08:00
|
|
|
};
|
|
|
|
|
2017-09-23 14:27:55 +08:00
|
|
|
ToolbarMenuItem.defaultProps = {
|
2017-08-25 07:47:20 +08:00
|
|
|
objectToReturn: null,
|
|
|
|
icon: '',
|
2017-09-23 14:27:55 +08:00
|
|
|
customIcon: null,
|
2017-08-25 07:47:20 +08:00
|
|
|
onBlur: null,
|
2017-09-23 14:27:55 +08:00
|
|
|
children: null,
|
2019-05-28 00:34:37 +08:00
|
|
|
disabled: false,
|
2019-06-14 06:42:20 +08:00
|
|
|
showCornerTriangle: false,
|
2021-11-03 01:11:41 +08:00
|
|
|
toolbarActive: false,
|
2017-08-25 07:47:20 +08:00
|
|
|
};
|