2017-08-25 07:47:20 +08:00
|
|
|
import React, { Component } from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import Button from '/imports/ui/components/button/component';
|
2017-11-16 08:34:50 +08:00
|
|
|
import _ from 'lodash';
|
2018-01-08 14:17:18 +08:00
|
|
|
import { styles } from '../styles';
|
2017-08-25 07:47:20 +08:00
|
|
|
|
2017-09-23 14:27:55 +08:00
|
|
|
export default class ToolbarSubmenuItem extends Component {
|
2017-08-25 07:47:20 +08:00
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
|
2017-11-16 08:34:50 +08:00
|
|
|
this.handleTouchStart = this.handleTouchStart.bind(this);
|
|
|
|
this.handleOnMouseUp = this.handleOnMouseUp.bind(this);
|
|
|
|
this.setRef = this.setRef.bind(this);
|
2017-08-25 07:47:20 +08:00
|
|
|
}
|
|
|
|
|
2017-11-16 08:34:50 +08:00
|
|
|
// generating a unique ref string for the toolbar-item
|
|
|
|
componentWillMount() {
|
|
|
|
this.uniqueRef = _.uniqueId('toolbar-submenu-item');
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
// returning the selected object
|
|
|
|
onItemClick(objectToReturn);
|
|
|
|
}
|
|
|
|
|
|
|
|
handleOnMouseUp() {
|
2017-08-25 07:47:20 +08:00
|
|
|
const { objectToReturn, onItemClick } = this.props;
|
2017-11-16 08:34:50 +08:00
|
|
|
// returning the selected object
|
2017-09-21 05:05:17 +08:00
|
|
|
onItemClick(objectToReturn);
|
2017-08-25 07:47:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
2017-12-16 01:14:21 +08:00
|
|
|
const {
|
|
|
|
className,
|
|
|
|
customIcon,
|
|
|
|
icon,
|
|
|
|
label,
|
|
|
|
} = this.props;
|
2017-12-08 21:28:02 +08:00
|
|
|
|
2017-08-25 07:47:20 +08:00
|
|
|
return (
|
|
|
|
<div className={styles.buttonWrapper}>
|
|
|
|
<Button
|
|
|
|
hideLabel
|
|
|
|
role="button"
|
2017-12-16 01:14:21 +08:00
|
|
|
color="default"
|
|
|
|
size="md"
|
2017-12-08 21:28:02 +08:00
|
|
|
label={label}
|
|
|
|
aria-label={label}
|
|
|
|
icon={icon}
|
|
|
|
customIcon={customIcon}
|
2017-12-16 01:14:21 +08:00
|
|
|
onMouseUp={this.handleOnMouseUp}
|
2019-05-28 01:27:58 +08:00
|
|
|
onKeyPress={this.handleOnMouseUp}
|
2017-12-08 21:28:02 +08:00
|
|
|
className={className}
|
2017-11-16 08:34:50 +08:00
|
|
|
setRef={this.setRef}
|
2017-08-25 07:47:20 +08:00
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-23 14:27:55 +08:00
|
|
|
|
|
|
|
ToolbarSubmenuItem.propTypes = {
|
|
|
|
label: PropTypes.string.isRequired,
|
|
|
|
icon: PropTypes.string,
|
|
|
|
customIcon: PropTypes.node,
|
|
|
|
onItemClick: PropTypes.func.isRequired,
|
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-09-23 14:27:55 +08:00
|
|
|
]).isRequired,
|
2017-08-25 07:47:20 +08:00
|
|
|
className: PropTypes.string.isRequired,
|
|
|
|
};
|
|
|
|
|
2017-09-23 14:27:55 +08:00
|
|
|
ToolbarSubmenuItem.defaultProps = {
|
|
|
|
icon: null,
|
|
|
|
customIcon: null,
|
2017-08-25 07:47:20 +08:00
|
|
|
};
|