-
Notifications
You must be signed in to change notification settings - Fork 37
/
index.js
125 lines (109 loc) · 3.64 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import React from 'react';
import ReactDOM from 'react-dom';
import PropTypes from 'prop-types';
class ClipboardButton extends React.Component {
static propTypes = {
options: function(props, propName, componentName) {
const options = props[propName];
if (options && typeof options !== 'object' || Array.isArray(options)) {
return new Error(`Invalid props '${propName}' supplied to '${componentName}'. ` +
`'${propName}' is not an object.`);
}
if (props['option-text'] !== undefined) {
const optionText = props['option-text'];
if (typeof optionText !== 'function') {
return new Error(`Invalid props 'option-text' supplied to '${componentName}'. ` +
`'option-text' is not a function.`);
}
}
},
title: PropTypes.string,
type: PropTypes.string,
className: PropTypes.string,
style: PropTypes.object,
component: PropTypes.any,
children: PropTypes.any,
}
static defaultProps = {
isVisibleWhenUnsupported: true,
onClick: function() {},
}
/* Returns a object with all props that fulfill a certain naming pattern
*
* @param {RegExp} regexp - Regular expression representing which pattern
* you'll be searching for.
* @param {Boolean} remove - Determines if the regular expression should be
* removed when transmitting the key from the props
* to the new object.
*
* e.g:
*
* // Considering:
* // this.props = {option-foo: 1, onBar: 2, data-foobar: 3 data-baz: 4};
*
* // *RegExps not using // so that this comment doesn't break up
* this.propsWith(option-*, true); // returns {foo: 1}
* this.propsWith(on*, true); // returns {Bar: 2}
* this.propsWith(data-*); // returns {data-foobar: 1, data-baz: 4}
*/
propsWith(regexp, remove=false) {
const object = {};
Object.keys(this.props).forEach(function(key) {
if (key.search(regexp) !== -1) {
const objectKey = remove ? key.replace(regexp, '') : key;
object[objectKey] = this.props[key];
}
}, this);
return object;
}
componentWillUnmount() {
this.clipboard && this.clipboard.destroy();
}
componentDidMount() {
// Support old API by trying to assign this.props.options first;
const options = this.props.options || this.propsWith(/^option-/, true);
const element = ReactDOM.findDOMNode(this.element);
if (!element) {
return;
}
const Clipboard = require('clipboard');
this.clipboard = new Clipboard(element, options);
const callbacks = this.propsWith(/^on/, true);
Object.keys(callbacks).forEach(function(callback) {
this.clipboard.on(callback.toLowerCase(), this.props['on' + callback]);
}, this);
}
render() {
const attributes = {
title: this.props.title || '',
type: this.getType(),
className: this.props.className || '',
style: this.props.style || {},
ref: element => this.element = element,
onClick: this.props.onClick,
...this.propsWith(/^data-/),
...this.propsWith(/^button-/, true),
};
const Clipboard = require('clipboard');
if (!this.props.isVisibleWhenUnsupported && !Clipboard.isSupported()) {
return null;
}
return React.createElement(
this.getComponent(),
attributes,
this.props.children
);
}
getType() {
if (this.getComponent() === 'button' || this.getComponent() === 'input') {
return this.props.type || 'button';
}
else {
return undefined;
}
}
getComponent() {
return this.props.component || 'button';
}
}
export default ClipboardButton;