2017-02-28 21:32:23 +00:00
|
|
|
import React, {PureComponent, PropTypes} from 'react';
|
2017-03-03 01:44:07 +00:00
|
|
|
import autobind from 'autobind-decorator';
|
2016-07-22 20:02:17 +00:00
|
|
|
const {clipboard} = require('electron');
|
|
|
|
|
2017-03-03 01:44:07 +00:00
|
|
|
@autobind
|
2017-02-28 21:32:23 +00:00
|
|
|
class CopyButton extends PureComponent {
|
2017-03-03 01:44:07 +00:00
|
|
|
constructor (props) {
|
|
|
|
super(props);
|
|
|
|
this.state = {
|
2017-03-03 20:09:08 +00:00
|
|
|
showConfirmation: false
|
2017-03-03 01:44:07 +00:00
|
|
|
};
|
|
|
|
}
|
2016-11-26 08:29:16 +00:00
|
|
|
|
2017-03-03 01:44:07 +00:00
|
|
|
_handleClick (e) {
|
2016-07-22 20:02:17 +00:00
|
|
|
e.preventDefault();
|
2017-02-27 21:00:13 +00:00
|
|
|
e.stopPropagation();
|
2016-07-22 20:02:17 +00:00
|
|
|
|
|
|
|
clipboard.writeText(this.props.content);
|
|
|
|
|
|
|
|
this.setState({showConfirmation: true});
|
|
|
|
|
2016-11-22 19:42:10 +00:00
|
|
|
this._triggerTimeout = setTimeout(() => {
|
2016-07-22 20:02:17 +00:00
|
|
|
this.setState({showConfirmation: false});
|
|
|
|
}, 2000);
|
2017-03-03 20:09:08 +00:00
|
|
|
}
|
2016-07-22 20:02:17 +00:00
|
|
|
|
2016-11-26 08:29:16 +00:00
|
|
|
componentWillUnmount () {
|
2016-11-22 19:42:10 +00:00
|
|
|
clearTimeout(this._triggerTimeout);
|
2016-07-22 20:02:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
render () {
|
2017-03-03 20:09:08 +00:00
|
|
|
const {
|
|
|
|
content, // eslint-disable-line no-unused-vars
|
|
|
|
children,
|
|
|
|
...other
|
|
|
|
} = this.props;
|
2016-07-22 20:02:17 +00:00
|
|
|
const {showConfirmation} = this.state;
|
|
|
|
|
|
|
|
return (
|
2017-02-27 21:00:13 +00:00
|
|
|
<button {...other} onClick={this._handleClick}>
|
2017-03-03 20:09:08 +00:00
|
|
|
{showConfirmation
|
|
|
|
? <span>Copied <i className="fa fa-check-circle-o"/></span>
|
|
|
|
: (children || 'Copy to Clipboard')
|
2016-11-29 21:28:22 +00:00
|
|
|
}
|
2016-07-22 20:02:17 +00:00
|
|
|
</button>
|
2017-03-03 20:09:08 +00:00
|
|
|
);
|
2016-07-22 20:02:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
CopyButton.propTypes = {
|
|
|
|
content: PropTypes.string.isRequired
|
|
|
|
};
|
|
|
|
|
|
|
|
export default CopyButton;
|