mirror of
https://github.com/getredash/redash.git
synced 2025-12-25 01:03:20 -05:00
* Add PlainButton * refactor close icons * reorder import * refactor remaining anchors * refactor: replace remaining <button> and TODOs * refactor: changed applicable elements to type link * fix: minor details * bug: fix tooltip ternary * refactor: improve interactivity and semantics of schema list item
56 lines
1.4 KiB
JavaScript
56 lines
1.4 KiB
JavaScript
import React from "react";
|
|
import Input from "antd/lib/input";
|
|
import CopyOutlinedIcon from "@ant-design/icons/CopyOutlined";
|
|
import Tooltip from "@/components/Tooltip";
|
|
import PlainButton from "./PlainButton";
|
|
|
|
export default class InputWithCopy extends React.Component {
|
|
constructor(props) {
|
|
super(props);
|
|
this.state = { copied: null };
|
|
this.ref = React.createRef();
|
|
this.copyFeatureSupported = document.queryCommandSupported("copy");
|
|
this.resetCopyState = null;
|
|
}
|
|
|
|
componentWillUnmount() {
|
|
if (this.resetCopyState) {
|
|
clearTimeout(this.resetCopyState);
|
|
}
|
|
}
|
|
|
|
copy = () => {
|
|
// select text
|
|
this.ref.current.select();
|
|
|
|
// copy
|
|
try {
|
|
const success = document.execCommand("copy");
|
|
if (!success) {
|
|
throw new Error();
|
|
}
|
|
this.setState({ copied: "Copied!" });
|
|
} catch (err) {
|
|
this.setState({
|
|
copied: "Copy failed",
|
|
});
|
|
}
|
|
|
|
// reset tooltip
|
|
this.resetCopyState = setTimeout(() => this.setState({ copied: null }), 2000);
|
|
};
|
|
|
|
render() {
|
|
const copyButton = (
|
|
<Tooltip title={this.state.copied || "Copy"}>
|
|
<PlainButton onClick={this.copy}>
|
|
{/* TODO: lacks visual feedback */}
|
|
<CopyOutlinedIcon />
|
|
</PlainButton>
|
|
</Tooltip>
|
|
);
|
|
|
|
return <Input {...this.props} ref={this.ref} addonAfter={this.copyFeatureSupported && copyButton} />;
|
|
}
|
|
}
|