main
Last change
on this file since e48199a was d24f17c, checked in by Aleksandar Panovski <apano77@…>, 15 months ago |
Initial commit
|
-
Property mode
set to
100644
|
File size:
1.2 KB
|
Rev | Line | |
---|
[d24f17c] | 1 | import React from 'react';
|
---|
| 2 | import PropTypes from 'prop-types';
|
---|
| 3 | import copy from 'copy-to-clipboard';
|
---|
| 4 |
|
---|
| 5 |
|
---|
| 6 | export class CopyToClipboard extends React.PureComponent {
|
---|
| 7 | static propTypes = {
|
---|
| 8 | text: PropTypes.string.isRequired,
|
---|
| 9 | children: PropTypes.element.isRequired,
|
---|
| 10 | onCopy: PropTypes.func,
|
---|
| 11 | options: PropTypes.shape({
|
---|
| 12 | debug: PropTypes.bool,
|
---|
| 13 | message: PropTypes.string,
|
---|
| 14 | format: PropTypes.string
|
---|
| 15 | })
|
---|
| 16 | };
|
---|
| 17 |
|
---|
| 18 |
|
---|
| 19 | static defaultProps = {
|
---|
| 20 | onCopy: undefined,
|
---|
| 21 | options: undefined
|
---|
| 22 | };
|
---|
| 23 |
|
---|
| 24 |
|
---|
| 25 | onClick = event => {
|
---|
| 26 | const {
|
---|
| 27 | text,
|
---|
| 28 | onCopy,
|
---|
| 29 | children,
|
---|
| 30 | options
|
---|
| 31 | } = this.props;
|
---|
| 32 |
|
---|
| 33 | const elem = React.Children.only(children);
|
---|
| 34 |
|
---|
| 35 | const result = copy(text, options);
|
---|
| 36 |
|
---|
| 37 | if (onCopy) {
|
---|
| 38 | onCopy(text, result);
|
---|
| 39 | }
|
---|
| 40 |
|
---|
| 41 | // Bypass onClick if it was present
|
---|
| 42 | if (elem && elem.props && typeof elem.props.onClick === 'function') {
|
---|
| 43 | elem.props.onClick(event);
|
---|
| 44 | }
|
---|
| 45 | };
|
---|
| 46 |
|
---|
| 47 |
|
---|
| 48 | render() {
|
---|
| 49 | const {
|
---|
| 50 | text: _text,
|
---|
| 51 | onCopy: _onCopy,
|
---|
| 52 | options: _options,
|
---|
| 53 | children,
|
---|
| 54 | ...props
|
---|
| 55 | } = this.props;
|
---|
| 56 | const elem = React.Children.only(children);
|
---|
| 57 |
|
---|
| 58 | return React.cloneElement(elem, {...props, onClick: this.onClick});
|
---|
| 59 | }
|
---|
| 60 | }
|
---|
Note:
See
TracBrowser
for help on using the repository browser.