forked from nihey/react-clipboard.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
react-clipboard.tsx
146 lines (132 loc) · 4.15 KB
/
react-clipboard.tsx
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import * as Clipboard from 'clipboard';
import * as PropTypes from 'prop-types';
import * as React from 'react';
interface ClipboardButtonProps {
type?: string;
className?: string;
style?: React.CSSProperties;
component?: any;
children?: React.ReactChild;
onClick?: any;
onSuccess?: (e: ClipboardJS.Event) => void;
onError?: (e: ClipboardJS.Event) => void;
options?: ClipboardJS.Options;
}
class ClipboardButton extends React.Component<ClipboardButtonProps> {
static propTypes = {
options(
props: ClipboardButtonProps,
propName: string,
componentName: string
) {
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.`
);
}
}
},
type: PropTypes.string,
className: PropTypes.string,
style: PropTypes.object,
component: PropTypes.oneOfType([PropTypes.string, PropTypes.object]),
children: PropTypes.oneOfType([
PropTypes.element,
PropTypes.arrayOf(PropTypes.element),
PropTypes.string,
PropTypes.number,
PropTypes.object,
]),
};
static defaultProps = {
onClick() {
//
},
};
element: HTMLElement;
clipboard: Clipboard;
/* 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: RegExp, remove: boolean = false) {
const object = {};
Object.keys(this.props).forEach(key => {
if (key.search(regexp) !== -1) {
const objectKey = remove ? key.replace(regexp, '') : key;
object[objectKey] = this.props[key];
}
});
return object;
}
componentWillUnmount() {
// tslint:disable-next-line:no-unused-expression
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 = React.version.match(/0\.13(.*)/)
// ? this.refs.element.getDOMNode()
// : this.element;
const element = this.element;
this.clipboard = new Clipboard(element, options);
const callbacks = this.propsWith(/^on/, true);
Object.keys(callbacks).forEach(callback => {
this.clipboard.on(callback.toLowerCase(), this.props[`on${callback}`]);
});
}
render() {
const attributes = {
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),
};
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;