This repository was archived by the owner on Mar 27, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 314
Expand file tree
/
Copy pathPaymentRequestButtonElement.js
More file actions
125 lines (113 loc) · 3.05 KB
/
PaymentRequestButtonElement.js
File metadata and controls
125 lines (113 loc) · 3.05 KB
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
// @flow
import React from 'react';
import PropTypes from 'prop-types';
import shallowEqual from '../utils/shallowEqual';
import {type ElementContext, elementContextTypes} from './Elements';
type Props = {
id?: string,
className?: string,
onBlur: Function,
onClick: Function,
onFocus: Function,
onReady: Function,
paymentRequest: {
canMakePayment: Function,
on: Function,
show: Function,
},
};
const noop = () => {};
const _extractOptions = (props: Props): Object => {
const {
id,
className,
onBlur,
onClick,
onFocus,
onReady,
paymentRequest,
...options
} = props;
return options;
};
class PaymentRequestButtonElement extends React.Component<Props> {
static propTypes = {
id: PropTypes.string,
className: PropTypes.string,
onBlur: PropTypes.func,
onClick: PropTypes.func,
onFocus: PropTypes.func,
onReady: PropTypes.func,
paymentRequest: PropTypes.shape({
canMakePayment: PropTypes.func.isRequired,
on: PropTypes.func.isRequired,
show: PropTypes.func.isRequired,
}).isRequired,
};
static defaultProps = {
id: undefined,
className: undefined,
onBlur: noop,
onClick: noop,
onFocus: noop,
onReady: noop,
};
static contextTypes = elementContextTypes;
constructor(props: Props, context: ElementContext) {
super(props, context);
const options = _extractOptions(props);
// We keep track of the extracted options on this._options to avoid re-rendering.
// (We would unnecessarily re-render if we were tracking them with state.)
this._options = options;
}
componentDidMount() {
this.context.addElementsLoadListener((elements: ElementsShape) => {
this._element = elements.create('paymentRequestButton', {
paymentRequest: this.props.paymentRequest,
...this._options,
});
this._element.on('ready', () => {
this.props.onReady(this._element);
});
this._element.on('focus', (...args) => this.props.onFocus(...args));
this._element.on('click', (...args) => this.props.onClick(...args));
this._element.on('blur', (...args) => this.props.onBlur(...args));
this._element.mount(this._ref);
});
}
componentDidUpdate(prevProps: Props) {
if (this.props.paymentRequest !== prevProps.paymentRequest) {
console.warn(
'Unsupported prop change: paymentRequest is not a customizable property.'
);
}
const options = _extractOptions(this.props);
if (
Object.keys(options).length !== 0 &&
!shallowEqual(options, this._options)
) {
this._options = options;
this._element.update(options);
}
}
componentWillUnmount() {
this._element.destroy();
}
context: ElementContext;
_element: ElementShape;
_ref: ?HTMLElement;
_options: Object;
handleRef = (ref: ?HTMLElement) => {
this._ref = ref;
};
render() {
return (
<div
id={this.props.id}
className={this.props.className}
ref={this.handleRef}
/>
);
}
}
export default PaymentRequestButtonElement;