feature(clipboard): add Clipboard button component

This commit is contained in:
George Lima 2019-01-12 16:29:50 -03:00
parent e75b1b0801
commit 3cf4d058f7
1 changed files with 50 additions and 0 deletions

View File

@ -0,0 +1,50 @@
// @flow
import React, { PureComponent } from 'react';
import { Button } from './button';
type Props = {
text: string,
className?: string,
};
type State = { copied: boolean };
export class Clipboard extends PureComponent<Props, State> {
static defaultProps = {
className: '',
};
state = {
copied: false,
};
handleClick = () => {
const { text } = this.props;
const el = document.createElement('textarea');
el.value = text;
if (document.body) document.body.appendChild(el);
el.select();
document.execCommand('copy');
if (document.body) document.body.removeChild(el);
this.setState({ copied: true });
};
render() {
const { className } = this.props;
const { copied } = this.state;
return (
<Button
label={copied ? 'Copied!' : 'Copy!'}
className={className}
onClick={this.handleClick}
disabled={copied}
/>
);
}
}