2018-10-04 15:39:14 +02:00
|
|
|
// @flow
|
2018-11-26 02:21:25 +01:00
|
|
|
import * as ICONS from 'constants/icons';
|
2018-10-04 15:39:14 +02:00
|
|
|
import * as React from 'react';
|
|
|
|
import { clipboard } from 'electron';
|
|
|
|
import { FormRow } from 'component/common/form';
|
|
|
|
import Button from 'component/button';
|
2018-10-29 18:23:53 +01:00
|
|
|
|
2018-10-04 15:39:14 +02:00
|
|
|
type Props = {
|
|
|
|
copyable: string,
|
2018-10-29 18:23:53 +01:00
|
|
|
doToast: ({ message: string }) => void,
|
2018-10-04 15:39:14 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
export default class CopyableText extends React.PureComponent<Props> {
|
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
|
|
|
|
this.input = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
input: ?HTMLInputElement;
|
|
|
|
|
|
|
|
render() {
|
2019-01-14 19:40:06 +01:00
|
|
|
const { copyable, doToast } = this.props;
|
2018-10-04 15:39:14 +02:00
|
|
|
|
|
|
|
return (
|
2019-01-14 19:40:06 +01:00
|
|
|
<FormRow verticallyCentered stretch>
|
2018-10-04 15:39:14 +02:00
|
|
|
<input
|
|
|
|
className="input-copyable form-field__input"
|
|
|
|
readOnly
|
|
|
|
value={copyable || ''}
|
|
|
|
ref={input => {
|
|
|
|
this.input = input;
|
|
|
|
}}
|
|
|
|
onFocus={() => {
|
|
|
|
if (this.input) {
|
|
|
|
this.input.select();
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
|
|
|
|
<Button
|
|
|
|
noPadding
|
|
|
|
button="secondary"
|
2018-11-26 02:21:25 +01:00
|
|
|
icon={ICONS.CLIPBOARD}
|
2018-10-04 15:39:14 +02:00
|
|
|
onClick={() => {
|
|
|
|
clipboard.writeText(copyable);
|
2018-10-29 18:23:53 +01:00
|
|
|
doToast({
|
|
|
|
message: __('Text copied'),
|
|
|
|
});
|
2018-10-04 15:39:14 +02:00
|
|
|
}}
|
|
|
|
/>
|
|
|
|
</FormRow>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|