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:27:25 +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 ToolbarSubmenuItem extends Component {
|
2017-08-25 07:47:20 +08:00
|
|
|
constructor() {
|
|
|
|
super();
|
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);
|
|
|
|
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-submenu-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;
|
|
|
|
// 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 {
|
|
|
|
customIcon,
|
|
|
|
icon,
|
|
|
|
label,
|
2021-11-03 01:27:25 +08:00
|
|
|
selected,
|
2017-12-16 01:14:21 +08:00
|
|
|
} = this.props;
|
2017-12-08 21:28:02 +08:00
|
|
|
|
2017-08-25 07:47:20 +08:00
|
|
|
return (
|
2021-11-03 01:27:25 +08:00
|
|
|
<Styled.ButtonWrapper>
|
|
|
|
<Styled.SubmenuButton
|
|
|
|
state={selected ? 'selected' : 'unselected'}
|
2017-08-25 07:47:20 +08:00
|
|
|
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-11-16 08:34:50 +08:00
|
|
|
setRef={this.setRef}
|
2017-08-25 07:47:20 +08:00
|
|
|
/>
|
2021-11-03 01:27:25 +08:00
|
|
|
</Styled.ButtonWrapper>
|
2017-08-25 07:47:20 +08:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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,
|
2021-11-03 01:27:25 +08:00
|
|
|
selected: PropTypes.bool,
|
2017-08-25 07:47:20 +08:00
|
|
|
};
|
|
|
|
|
2017-09-23 14:27:55 +08:00
|
|
|
ToolbarSubmenuItem.defaultProps = {
|
|
|
|
icon: null,
|
|
|
|
customIcon: null,
|
2021-11-03 01:27:25 +08:00
|
|
|
selected: false,
|
2017-08-25 07:47:20 +08:00
|
|
|
};
|