forked from sourcegraph/sourcegraph-public-snapshot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCopyableText.tsx
More file actions
73 lines (62 loc) · 2.37 KB
/
Copy pathCopyableText.tsx
File metadata and controls
73 lines (62 loc) · 2.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import copy from 'copy-to-clipboard'
import ContentCopyIcon from 'mdi-react/ContentCopyIcon'
import * as React from 'react'
interface Props {
/** The text to present and to copy. */
text: string
/** An optional class name. */
className?: string
/** The size of the input element. */
size?: number
/** Whether or not the text to be copied is a password. */
password?: boolean
}
interface State {
/** Whether the text was just copied. */
copied: boolean
}
/**
* A component that displays a single line of text and a copy-to-clipboard button. There are other
* niceties, such as triple-clicking selects only the text and not other adjacent components' text
* labels.
*/
export class CopyableText extends React.PureComponent<Props, State> {
public state: State = { copied: false }
public render(): JSX.Element | null {
return (
<div className={`copyable-text form-inline ${this.props.className || ''}`}>
<div className="input-group">
<input
type={this.props.password ? 'password' : 'text'}
className="copyable-text__input form-control"
value={this.props.text}
size={this.props.size}
readOnly={true}
onClick={this.onClickInput}
/>
<div className="input-group-append">
<button
type="button"
className="btn btn-secondary"
onClick={this.onClickButton}
disabled={this.state.copied}
>
<ContentCopyIcon className="icon-inline" /> {this.state.copied ? 'Copied' : 'Copy'}
</button>
</div>
</div>
</div>
)
}
private onClickInput: React.MouseEventHandler<HTMLInputElement> = e => {
e.currentTarget.focus()
e.currentTarget.setSelectionRange(0, this.props.text.length)
this.copyToClipboard()
}
private onClickButton = (): void => this.copyToClipboard()
private copyToClipboard(): void {
copy(this.props.text)
this.setState({ copied: true })
setTimeout(() => this.setState({ copied: false }), 1000)
}
}