import React, { PropTypes } from 'react'; import Clipboard from 'clipboard'; class CopyableInput extends React.Component { constructor(props) { super(props); this.onMouseLeaveHandler = this.onMouseLeaveHandler.bind(this); } componentDidMount() { this.clipboard = new Clipboard(this.input, { target: () => this.input }); this.clipboard.on('success', (e) => { this.tooltip.classList.add('tooltipped'); this.tooltip.classList.add('tooltipped-n'); }); } componentWillUnmount() { this.clipboard.destroy(); } onMouseLeaveHandler() { this.tooltip.classList.remove('tooltipped'); this.tooltip.classList.remove('tooltipped-n'); } render() { const { label, value } = this.props; return (
{ this.tooltip = element; }} onMouseLeave={this.onMouseLeaveHandler} > { this.input = element; }} readOnly />
); } } CopyableInput.propTypes = { label: PropTypes.string.isRequired, value: PropTypes.string.isRequired }; export default CopyableInput;