2017-05-04 13:55:52 +01:00
|
|
|
/*
|
2017-05-04 15:02:21 +01:00
|
|
|
Copyright 2017 Vector Creations Ltd
|
2017-05-04 13:55:52 +01:00
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
|
limitations under the License.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
import React from 'react';
|
2021-10-22 17:23:32 -05:00
|
|
|
|
2017-05-04 13:55:52 +01:00
|
|
|
import AccessibleButton from './AccessibleButton';
|
2020-05-13 20:41:41 -06:00
|
|
|
import dis from '../../../dispatcher/dispatcher';
|
2017-09-20 17:16:49 +01:00
|
|
|
import Analytics from '../../../Analytics';
|
2021-06-29 13:11:58 +01:00
|
|
|
import { replaceableComponent } from "../../../utils/replaceableComponent";
|
2021-07-20 12:42:21 +02:00
|
|
|
import Tooltip from './Tooltip';
|
2017-05-04 13:55:52 +01:00
|
|
|
|
2021-07-20 10:59:34 +02:00
|
|
|
interface IProps {
|
|
|
|
|
size?: string;
|
|
|
|
|
tooltip?: boolean;
|
|
|
|
|
action: string;
|
|
|
|
|
mouseOverAction?: string;
|
|
|
|
|
label: string;
|
|
|
|
|
iconPath?: string;
|
|
|
|
|
className?: string;
|
|
|
|
|
children?: JSX.Element;
|
|
|
|
|
}
|
2017-05-04 13:55:52 +01:00
|
|
|
|
2021-07-20 10:59:34 +02:00
|
|
|
interface IState {
|
|
|
|
|
showTooltip: boolean;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@replaceableComponent("views.elements.ActionButton")
|
|
|
|
|
export default class ActionButton extends React.Component<IProps, IState> {
|
2021-07-20 12:30:41 +02:00
|
|
|
static defaultProps: Partial<IProps> = {
|
2020-08-29 12:14:16 +01:00
|
|
|
size: "25",
|
|
|
|
|
tooltip: false,
|
|
|
|
|
};
|
2017-05-04 15:38:09 +01:00
|
|
|
|
2021-07-20 10:59:34 +02:00
|
|
|
constructor(props: IProps) {
|
|
|
|
|
super(props);
|
2017-05-04 13:55:52 +01:00
|
|
|
|
2021-07-20 10:59:34 +02:00
|
|
|
this.state = {
|
|
|
|
|
showTooltip: false,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private onClick = (ev: React.MouseEvent): void => {
|
2017-05-04 13:55:52 +01:00
|
|
|
ev.stopPropagation();
|
2017-09-20 17:16:49 +01:00
|
|
|
Analytics.trackEvent('Action Button', 'click', this.props.action);
|
2021-06-29 13:11:58 +01:00
|
|
|
dis.dispatch({ action: this.props.action });
|
2020-08-29 12:14:16 +01:00
|
|
|
};
|
2017-05-04 13:55:52 +01:00
|
|
|
|
2021-07-20 10:59:34 +02:00
|
|
|
private onMouseEnter = (): void => {
|
2021-12-09 11:47:50 +01:00
|
|
|
this.showTooltip();
|
2017-05-25 13:54:59 +01:00
|
|
|
if (this.props.mouseOverAction) {
|
2021-06-29 13:11:58 +01:00
|
|
|
dis.dispatch({ action: this.props.mouseOverAction });
|
2017-05-25 13:54:59 +01:00
|
|
|
}
|
2020-08-29 12:14:16 +01:00
|
|
|
};
|
2017-05-04 15:38:09 +01:00
|
|
|
|
2021-12-09 11:47:50 +01:00
|
|
|
private showTooltip = (): void => {
|
|
|
|
|
if (this.props.tooltip) this.setState({ showTooltip: true });
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
private hideTooltip = (): void => {
|
2021-06-29 13:11:58 +01:00
|
|
|
this.setState({ showTooltip: false });
|
2020-08-29 12:14:16 +01:00
|
|
|
};
|
2017-05-04 15:38:09 +01:00
|
|
|
|
2020-08-29 12:14:16 +01:00
|
|
|
render() {
|
2017-05-04 15:38:09 +01:00
|
|
|
let tooltip;
|
|
|
|
|
if (this.state.showTooltip) {
|
2019-02-01 00:36:19 +01:00
|
|
|
tooltip = <Tooltip className="mx_RoleButton_tooltip" label={this.props.label} />;
|
2017-05-04 15:38:09 +01:00
|
|
|
}
|
|
|
|
|
|
2018-12-10 15:33:35 +01:00
|
|
|
const icon = this.props.iconPath ?
|
2021-06-28 16:30:48 +01:00
|
|
|
(<img src={this.props.iconPath} width={this.props.size} height={this.props.size} />) :
|
2021-04-27 17:23:27 +02:00
|
|
|
undefined;
|
2018-12-10 15:33:35 +01:00
|
|
|
|
2018-12-19 12:11:21 +01:00
|
|
|
const classNames = ["mx_RoleButton"];
|
|
|
|
|
if (this.props.className) {
|
|
|
|
|
classNames.push(this.props.className);
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-04 13:55:52 +01:00
|
|
|
return (
|
2021-04-27 17:01:22 +01:00
|
|
|
<AccessibleButton
|
|
|
|
|
className={classNames.join(" ")}
|
2021-07-20 10:59:34 +02:00
|
|
|
onClick={this.onClick}
|
|
|
|
|
onMouseEnter={this.onMouseEnter}
|
2021-12-09 11:47:50 +01:00
|
|
|
onMouseLeave={this.hideTooltip}
|
|
|
|
|
onFocus={this.showTooltip}
|
|
|
|
|
onBlur={this.hideTooltip}
|
2017-11-17 14:33:39 +01:00
|
|
|
aria-label={this.props.label}
|
2017-05-04 15:38:09 +01:00
|
|
|
>
|
2018-12-10 15:33:35 +01:00
|
|
|
{ icon }
|
2017-10-11 17:56:17 +01:00
|
|
|
{ tooltip }
|
2021-04-27 17:01:22 +01:00
|
|
|
{ this.props.children }
|
2017-05-04 13:55:52 +01:00
|
|
|
</AccessibleButton>
|
|
|
|
|
);
|
2020-08-29 12:14:16 +01:00
|
|
|
}
|
|
|
|
|
}
|