2016-06-29 23:11:12 +02:00
|
|
|
const Component = require('react').Component
|
|
|
|
const h = require('react-hyperscript')
|
|
|
|
const inherits = require('util').inherits
|
|
|
|
const copyToClipboard = require('copy-to-clipboard')
|
|
|
|
|
2016-06-30 01:21:38 +02:00
|
|
|
const Tooltip = require('./tooltip')
|
2016-06-30 00:57:59 +02:00
|
|
|
|
2016-06-29 23:11:12 +02:00
|
|
|
module.exports = CopyButton
|
|
|
|
|
|
|
|
inherits(CopyButton, Component)
|
|
|
|
function CopyButton () {
|
|
|
|
Component.call(this)
|
|
|
|
}
|
|
|
|
|
2016-06-30 00:57:59 +02:00
|
|
|
// As parameters, accepts:
|
|
|
|
// "value", which is the value to copy (mandatory)
|
|
|
|
// "title", which is the text to show on hover (optional, defaults to 'Copy')
|
2016-06-29 23:11:12 +02:00
|
|
|
CopyButton.prototype.render = function () {
|
|
|
|
const props = this.props
|
2016-06-30 00:57:59 +02:00
|
|
|
const state = this.state || {}
|
|
|
|
|
2016-06-29 23:11:12 +02:00
|
|
|
const value = props.value
|
2016-06-30 00:57:59 +02:00
|
|
|
const copied = state.copied
|
|
|
|
|
|
|
|
const message = copied ? 'Copied' : props.title || ' Copy '
|
2016-06-29 23:11:12 +02:00
|
|
|
|
2016-06-30 00:57:59 +02:00
|
|
|
return h('.copy-button', {
|
2016-06-29 23:39:25 +02:00
|
|
|
style: {
|
|
|
|
display: 'flex',
|
|
|
|
alignItems: 'center',
|
|
|
|
},
|
2016-06-30 00:57:59 +02:00
|
|
|
}, [
|
|
|
|
|
|
|
|
h(Tooltip, {
|
|
|
|
title: message,
|
|
|
|
}, [
|
|
|
|
h('i.fa.fa-clipboard.cursor-pointer.color-orange', {
|
|
|
|
style: {
|
|
|
|
margin: '5px',
|
|
|
|
},
|
|
|
|
onClick: (event) => {
|
|
|
|
event.preventDefault()
|
|
|
|
event.stopPropagation()
|
|
|
|
copyToClipboard(value)
|
|
|
|
this.debounceRestore()
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
]),
|
|
|
|
|
|
|
|
])
|
|
|
|
}
|
|
|
|
|
2016-11-11 19:26:12 +01:00
|
|
|
CopyButton.prototype.debounceRestore = function () {
|
2016-06-30 00:57:59 +02:00
|
|
|
this.setState({ copied: true })
|
|
|
|
clearTimeout(this.timeout)
|
|
|
|
this.timeout = setTimeout(() => {
|
|
|
|
this.setState({ copied: false })
|
|
|
|
}, 850)
|
2016-06-29 23:11:12 +02:00
|
|
|
}
|