bitburner-src/src/ui/React/AutoupdatingStdButton.tsx

75 lines
1.7 KiB
TypeScript
Raw Normal View History

/**
* 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 {
2021-09-05 01:09:30 +02:00
disabled?: boolean;
intervalTime?: number;
onClick?: (e: React.MouseEvent<HTMLElement>) => any;
style?: any;
text: string | JSX.Element;
tooltip?: string;
}
interface IState {
2021-09-05 01:09:30 +02:00
i: number;
}
2019-04-04 02:08:11 +02:00
type IInnerHTMLMarkup = {
2021-09-05 01:09:30 +02:00
__html: string;
};
2019-04-04 02:08:11 +02:00
export class AutoupdatingStdButton extends React.Component<IProps, IState> {
2021-09-05 01:09:30 +02:00
/**
* Timer ID for auto-updating implementation (returned value from setInterval())
*/
interval = 0;
2021-09-05 01:09:30 +02:00
constructor(props: IProps) {
super(props);
this.state = {
i: 0,
};
}
2021-09-05 01:09:30 +02:00
componentDidMount(): void {
const time = this.props.intervalTime ? this.props.intervalTime : 1000;
this.interval = window.setInterval(() => this.tick(), time);
}
2021-09-05 01:09:30 +02:00
componentWillUnmount(): void {
clearInterval(this.interval);
}
2021-09-05 01:09:30 +02:00
tick(): void {
this.setState((prevState) => ({
i: prevState.i + 1,
}));
}
2021-09-05 01:09:30 +02:00
render(): React.ReactNode {
const hasTooltip = this.props.tooltip != null && this.props.tooltip !== "";
2021-09-05 01:09:30 +02:00
let className = this.props.disabled ? "std-button-disabled" : "std-button";
if (hasTooltip) {
className += " tooltip";
}
2021-09-05 01:09:30 +02:00
// Tooltip will eb set using inner HTML
const tooltipMarkup: IInnerHTMLMarkup = {
__html: this.props.tooltip ? this.props.tooltip : "",
};
2019-04-04 02:08:11 +02:00
2021-09-05 01:09:30 +02:00
return (
2021-09-09 05:47:34 +02:00
<button className={className} onClick={this.props.onClick} style={this.props.style}>
2021-09-05 01:09:30 +02:00
{this.props.text}
2021-09-09 05:47:34 +02:00
{hasTooltip && <span className={"tooltiptext"} dangerouslySetInnerHTML={tooltipMarkup}></span>}
2021-09-05 01:09:30 +02:00
</button>
);
}
}