forked from dozoisch/react-google-recaptcha
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrecaptcha.js
More file actions
132 lines (118 loc) · 3.26 KB
/
recaptcha.js
File metadata and controls
132 lines (118 loc) · 3.26 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
126
127
128
129
130
131
132
import React from "react";
import PropTypes from "prop-types";
export default class ReCAPTCHA extends React.Component {
constructor() {
super();
this.handleExpired = this.handleExpired.bind(this);
this.handleRecaptchaRef = this.handleRecaptchaRef.bind(this);
}
getValue() {
if (this.props.grecaptcha && this._widgetId !== undefined) {
return this.props.grecaptcha.getResponse(this._widgetId);
}
return null;
}
getWidgetId() {
if (this.props.grecaptcha && this._widgetId !== undefined) {
return this._widgetId;
}
return null;
}
execute() {
const { grecaptcha } = this.props;
if (grecaptcha && this._widgetId !== undefined) {
return grecaptcha.execute(this._widgetId);
} else {
this._executeRequested = true;
}
}
reset() {
if (this.props.grecaptcha && this._widgetId !== undefined) {
this.props.grecaptcha.reset(this._widgetId);
}
}
handleExpired() {
if (this.props.onExpired) {
this.props.onExpired();
} else if (this.props.onChange) {
this.props.onChange(null);
}
}
explicitRender() {
if (this.props.grecaptcha && this.props.grecaptcha.render && this._widgetId === undefined) {
const wrapper = document.createElement("div");
this._widgetId = this.props.grecaptcha.render(wrapper, {
sitekey: this.props.sitekey,
callback: this.props.onChange,
theme: this.props.theme,
type: this.props.type,
tabindex: this.props.tabindex,
"expired-callback": this.handleExpired,
size: this.props.size,
stoken: this.props.stoken,
badge: this.props.badge,
});
this.captcha.appendChild(wrapper);
}
if (this._executeRequested && this.props.grecaptcha && this._widgetId !== undefined) {
this._executeRequested = false;
this.execute();
}
}
componentDidMount() {
this.explicitRender();
}
componentDidUpdate() {
this.explicitRender();
}
componentWillUnmount() {
if (this._widgetId !== undefined) {
while (this.captcha.firstChild) {
this.captcha.removeChild(this.captcha.firstChild);
}
this.reset();
}
}
handleRecaptchaRef(elem) {
this.captcha = elem;
}
render() {
// consume properties owned by the reCATPCHA, pass the rest to the div so the user can style it.
/* eslint-disable no-unused-vars */
const {
sitekey,
onChange,
theme,
type,
tabindex,
onExpired,
size,
stoken,
grecaptcha,
badge,
...childProps
} = this.props;
/* eslint-enable no-unused-vars */
return <div {...childProps} ref={this.handleRecaptchaRef} />;
}
}
ReCAPTCHA.displayName = "ReCAPTCHA";
ReCAPTCHA.propTypes = {
sitekey: PropTypes.string.isRequired,
onChange: PropTypes.func.isRequired,
grecaptcha: PropTypes.object,
theme: PropTypes.oneOf(["dark", "light"]),
type: PropTypes.oneOf(["image", "audio"]),
tabindex: PropTypes.number,
onExpired: PropTypes.func,
size: PropTypes.oneOf(["compact", "normal", "invisible"]),
stoken: PropTypes.string,
badge: PropTypes.oneOf(["bottomright", "bottomleft", "inline"]),
};
ReCAPTCHA.defaultProps = {
theme: "light",
type: "image",
tabindex: 0,
size: "normal",
badge: "bottomright",
};