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
109 lines (97 loc) · 2.87 KB
/
recaptcha.js
File metadata and controls
109 lines (97 loc) · 2.87 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
import React from "react";
import PropTypes from "prop-types";
export default class ReCAPTCHA extends React.Component {
constructor() {
super();
this.state = {};
this.handleExpired = this.handleExpired.bind(this);
this.handleRecaptchaRef = this.handleRecaptchaRef.bind(this);
}
getValue() {
if (this.props.grecaptcha && this.state.widgetId !== undefined) {
return this.props.grecaptcha.getResponse(this.state.widgetId);
}
return null;
}
execute() {
const { grecaptcha } = this.props;
const { widgetId } = this.state;
if (grecaptcha && widgetId !== undefined) {
return grecaptcha.execute(widgetId);
} else {
this._executeRequested = true;
}
}
reset() {
if (this.props.grecaptcha && this.state.widgetId !== undefined) {
this.props.grecaptcha.reset(this.state.widgetId);
}
}
handleExpired() {
if (this.props.onExpired) {
this.props.onExpired();
} else if (this.props.onChange) {
this.props.onChange(null);
}
}
explicitRender(cb) {
if (this.props.grecaptcha && this.state.widgetId === undefined) {
const id = this.props.grecaptcha.render(this.captcha, {
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.setState({
widgetId: id,
}, cb);
}
if (this._executeRequested && this.props.grecaptcha && this.state.widgetId !== undefined) {
this._executeRequested = false;
this.execute();
}
}
componentDidMount() {
this.explicitRender();
}
componentDidUpdate() {
this.explicitRender();
}
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",
};