2021-03-31 06:45:21 +02:00
|
|
|
import * as React from "react";
|
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
export enum ClickableTag {
|
|
|
|
Tag_span,
|
|
|
|
Tag_h1,
|
2021-05-04 01:46:04 +02:00
|
|
|
}
|
|
|
|
|
2021-03-31 06:45:21 +02:00
|
|
|
type IProps = {
|
2021-09-05 01:09:30 +02:00
|
|
|
value: string;
|
|
|
|
tag: ClickableTag;
|
|
|
|
};
|
2021-03-31 06:45:21 +02:00
|
|
|
|
|
|
|
type IState = {
|
2021-09-05 01:09:30 +02:00
|
|
|
tooltipVisible: boolean;
|
|
|
|
};
|
2021-03-31 06:45:21 +02:00
|
|
|
|
|
|
|
export class CopyableText extends React.Component<IProps, IState> {
|
2021-09-05 01:09:30 +02:00
|
|
|
public static defaultProps = {
|
|
|
|
//Default span to prevent destroying current clickables
|
|
|
|
tag: ClickableTag.Tag_span,
|
|
|
|
};
|
2021-03-31 06:45:21 +02:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
constructor(props: IProps) {
|
|
|
|
super(props);
|
2021-03-31 06:45:21 +02:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
this.copy = this.copy.bind(this);
|
|
|
|
this.tooltipClasses = this.tooltipClasses.bind(this);
|
|
|
|
this.textClasses = this.textClasses.bind(this);
|
2021-03-31 06:45:21 +02:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
this.state = {
|
|
|
|
tooltipVisible: false,
|
|
|
|
};
|
|
|
|
}
|
2021-03-31 06:45:21 +02:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
copy(): void {
|
|
|
|
const copyText = document.createElement("textarea");
|
|
|
|
copyText.value = this.props.value;
|
|
|
|
document.body.appendChild(copyText);
|
|
|
|
copyText.select();
|
|
|
|
copyText.setSelectionRange(0, 1e10);
|
|
|
|
document.execCommand("copy");
|
|
|
|
document.body.removeChild(copyText);
|
|
|
|
this.setState({ tooltipVisible: true });
|
|
|
|
setTimeout(() => this.setState({ tooltipVisible: false }), 1000);
|
|
|
|
}
|
2021-03-31 06:45:21 +02:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
tooltipClasses(): string {
|
|
|
|
let classes = "copy_tooltip_text";
|
|
|
|
if (this.state.tooltipVisible) {
|
|
|
|
classes += " copy_tooltip_text_visible";
|
2021-03-31 06:45:21 +02:00
|
|
|
}
|
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
return classes;
|
|
|
|
}
|
2021-03-31 06:45:21 +02:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
textClasses(): string {
|
|
|
|
let classes = "copy_tooltip noselect text";
|
|
|
|
if (this.state.tooltipVisible) {
|
|
|
|
classes += " copy_tooltip_copied";
|
2021-03-31 06:45:21 +02:00
|
|
|
}
|
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
return classes;
|
|
|
|
}
|
2021-03-31 06:45:21 +02:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
render(): React.ReactNode {
|
|
|
|
switch (this.props.tag) {
|
|
|
|
case ClickableTag.Tag_h1:
|
|
|
|
return (
|
|
|
|
<h1 className={this.textClasses()} onClick={this.copy}>
|
|
|
|
{this.props.value}
|
|
|
|
<span className={this.tooltipClasses()}>Copied!</span>
|
|
|
|
</h1>
|
|
|
|
);
|
|
|
|
case ClickableTag.Tag_span:
|
|
|
|
return (
|
|
|
|
<span className={this.textClasses()} onClick={this.copy}>
|
|
|
|
<b>{this.props.value}</b>
|
|
|
|
<span className={this.tooltipClasses()}>Copied!</span>
|
|
|
|
</span>
|
|
|
|
);
|
2021-03-31 06:45:21 +02:00
|
|
|
}
|
2021-09-05 01:09:30 +02:00
|
|
|
}
|
|
|
|
}
|