2019-04-01 11:23:25 +02:00
|
|
|
/**
|
|
|
|
* Basic stateless button that automatically re-renders itself every X seconds
|
|
|
|
* Uses the 'std-button' css class
|
|
|
|
*
|
|
|
|
* NOT recommended for usage - only if you really have to
|
|
|
|
*/
|
|
|
|
import * as React from "react";
|
|
|
|
|
|
|
|
interface IProps {
|
|
|
|
disabled?: boolean;
|
|
|
|
intervalTime?: number;
|
|
|
|
onClick?: (e: React.MouseEvent<HTMLElement>) => any;
|
|
|
|
style?: object;
|
2021-03-31 06:45:21 +02:00
|
|
|
text: string | JSX.Element;
|
2019-04-01 11:23:25 +02:00
|
|
|
tooltip?: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface IState {
|
|
|
|
i: number;
|
|
|
|
}
|
|
|
|
|
2019-04-04 02:08:11 +02:00
|
|
|
type IInnerHTMLMarkup = {
|
|
|
|
__html: string;
|
|
|
|
}
|
|
|
|
|
2019-04-01 11:23:25 +02:00
|
|
|
export class AutoupdatingStdButton extends React.Component<IProps, IState> {
|
|
|
|
/**
|
|
|
|
* Timer ID for auto-updating implementation (returned value from setInterval())
|
|
|
|
*/
|
2021-04-30 05:52:56 +02:00
|
|
|
interval = 0;
|
2019-04-01 11:23:25 +02:00
|
|
|
|
|
|
|
constructor(props: IProps) {
|
|
|
|
super(props);
|
|
|
|
this.state = {
|
|
|
|
i: 0,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount() {
|
|
|
|
const time = this.props.intervalTime ? this.props.intervalTime : 1000;
|
|
|
|
this.interval = setInterval(() => this.tick(), time);
|
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
|
|
|
clearInterval(this.interval);
|
|
|
|
}
|
|
|
|
|
|
|
|
tick() {
|
|
|
|
this.setState(prevState => ({
|
2021-04-30 05:52:56 +02:00
|
|
|
i: prevState.i + 1,
|
2019-04-01 11:23:25 +02:00
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
2019-04-04 02:08:11 +02:00
|
|
|
const hasTooltip = this.props.tooltip != null && this.props.tooltip !== "";
|
2019-04-01 11:23:25 +02:00
|
|
|
|
|
|
|
let className = this.props.disabled ? "std-button-disabled" : "std-button";
|
|
|
|
if (hasTooltip) {
|
|
|
|
className += " tooltip"
|
|
|
|
}
|
|
|
|
|
2019-04-04 02:08:11 +02:00
|
|
|
// Tooltip will eb set using inner HTML
|
|
|
|
let tooltipMarkup: IInnerHTMLMarkup | null;
|
|
|
|
if (hasTooltip) {
|
|
|
|
tooltipMarkup = {
|
2021-04-30 05:52:56 +02:00
|
|
|
__html: this.props.tooltip!,
|
2019-04-04 02:08:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-01 11:23:25 +02:00
|
|
|
return (
|
|
|
|
<button className={className} onClick={this.props.onClick} style={this.props.style}>
|
|
|
|
{this.props.text}
|
|
|
|
{
|
|
|
|
hasTooltip &&
|
2019-04-04 02:08:11 +02:00
|
|
|
<span className={"tooltiptext"} dangerouslySetInnerHTML={tooltipMarkup!}></span>
|
2019-04-01 11:23:25 +02:00
|
|
|
}
|
|
|
|
</button>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|