Skip to content

Commit fea40f5

Browse files
committed
bump 0.0.1 - added base component
- added base component - added base documentation - added coming up section
1 parent 35fcc4c commit fea40f5

File tree

3 files changed

+189
-1
lines changed

3 files changed

+189
-1
lines changed

README.md

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,71 @@
11
# react-google-recaptcha
2-
Component wrapper for Google reCAPTCHA
2+
Component wrapper for [Google reCAPTCHA][reCAPTCHA]
3+
4+
## Installation
5+
6+
```shell
7+
npm install --save react-google-recaptcha
8+
```
9+
10+
## Usage
11+
12+
First of all [sign up for an API key pair][signup]. Then add the Google reCAPTCHA script tag to your html.
13+
14+
The property set after `onload` is important and will be needed at render. This props is used to define a global callback, called by the Google script once it is loaded.
15+
16+
See the [Google reCAPTCHA docs][docs] for more info.
17+
18+
```html
19+
<script src="https://www.google.com/recaptcha/api.js?render=explicit&onload=onloadCallback" async defer></script>
20+
```
21+
22+
You can then use the reCAPTCHA
23+
24+
```jsx
25+
var React = require("react");
26+
var ReCATPCHA = require("react-google-recaptcha");
27+
28+
function onChange(value) {
29+
console.log("Captcha value:", value);
30+
}
31+
32+
React.render(
33+
<ReCATPCHA
34+
sitekey="Your sitekey"
35+
onChange={onChange}
36+
onloadCallbackName="onloadcallback"
37+
/>, document.body);
38+
```
39+
40+
###Rendering Props
41+
42+
Other properties can be used to customised the rendering.
43+
44+
| Name | Type | Description |
45+
|:---- | ---- | ------ |
46+
| sitekey | string | The API client key |
47+
| onChange | func | The function to be called when the user completes successfully the captcha |
48+
| onloadCallbackName | string | The name the script will call onload. **This must be the same provided on script tag.**
49+
| theme | enum | *optional* `light` or `dark` The them of the widget *(__defaults:__ light)*
50+
| type | enum | *optional* `image` or `audio` The type of initial captcha *(__defaults:__ image)*
51+
| tabindex | number | *optional* The tabindex on the element *(__default:__ 0)*
52+
| onLoad | func | *optional* callback called when the widget has rendered
53+
| onExpired | func | *optional* callback when the challenge is expired and has to be redone by user. By default it will call the onChange with null to signify expired callback. |
54+
55+
## Component API
56+
57+
The component also has some utility functions that can be called.
58+
59+
- `getValue()` returns the value of the captcha field
60+
- `reset()` forces reset. See the [JavaScript API doc][js_api]
61+
62+
## To Come Soon
63+
- tests
64+
- examples
65+
- code coverage
66+
- eslint
67+
68+
[reCAPTCHA]: https://www.google.com/recaptcha
69+
[signup]: http://www.google.com/recaptcha/admin
70+
[docs]: https://developers.google.com/recaptcha
71+
[js_api]: https://developers.google.com/recaptcha/docs/display#js_api

lib/recaptcha.jsx

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
"use strict";
2+
var React = require("react");
3+
var PropTypes = React.PropTypes;
4+
5+
var ReCAPTCHA = React.createClass({
6+
displayName: "react-reCAPTCHA",
7+
propTypes: {
8+
sitekey: PropTypes.string.isRequired,
9+
onChange: PropTypes.func.isRequired,
10+
onloadCallbackName: PropTypes.string.isRequired, // Name on the script tag onload query parameter
11+
theme: PropTypes.oneOf(["dark", "light"]),
12+
type: PropTypes.oneOf(["image", "audio"]),
13+
tabindex: PropTypes.number,
14+
onLoad: PropTypes.func,
15+
onExpired: PropTypes.func,
16+
},
17+
18+
getDefaultState: function () {
19+
return {};
20+
},
21+
22+
getDefaultProps: function () {
23+
return {
24+
theme: "light",
25+
type: "image",
26+
tabindex: 0,
27+
elementId: "react-reCAPTCHA",
28+
};
29+
},
30+
31+
getValue: function () {
32+
if (typeof grecaptcha !== "undefined" && this.state.widgetId) {
33+
return grecaptcha.getResponse(this.state.widgetId);
34+
}
35+
return null;
36+
},
37+
38+
reset: function () {
39+
if (typeof grecaptcha !== "undefined" && this.state.widgetId) {
40+
grecaptcha.reset(this.props.widgetId);
41+
}
42+
},
43+
44+
handleExpired: function () {
45+
if (this.props.onExpired) {
46+
this.props.onExpired();
47+
} else if (this.props.onChange) {
48+
this.props.onChange(null);
49+
}
50+
},
51+
52+
explicitRender: function () {
53+
var id = grecaptcha.render(this.refs.captcha.getDOMNode(), {
54+
sitekey: this.props.sitekey,
55+
callback: this.props.onChange,
56+
theme: this.props.theme,
57+
type: this.props.type,
58+
tabindex: this.props.tabindex,
59+
"expired-callback": this.handleExpired,
60+
});
61+
this.setState({
62+
widgetId: id,
63+
});
64+
},
65+
66+
67+
handleLoad: function () {
68+
this.explicitRender();
69+
if (this.props.onLoad) {
70+
this.props.onLoad();
71+
}
72+
},
73+
74+
componentDidMount: function () {
75+
// If script is not loaded, set the callback on window.
76+
if (typeof grecaptcha === "undefined") {
77+
window[this.props.onloadCallbackName] = this.handleLoad;
78+
} else {
79+
this.handleLoad();
80+
}
81+
},
82+
83+
render: function () {
84+
return (
85+
<div ref="captcha" id={this.props.elementId} />
86+
);
87+
}
88+
});
89+
90+
module.exports = ReCAPTCHA;

package.json

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"name": "react-google-recaptcha",
3+
"version": "0.0.1",
4+
"description": "React Component Wrapper for Google reCAPTCHA",
5+
"main": "lib/recaptcha.jsx",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"repository": {
10+
"type": "git",
11+
"url": "https://github.com/dozoisch/react-google-recaptcha.git"
12+
},
13+
"keywords": [
14+
"react",
15+
"captcha",
16+
"recaptcha",
17+
"google-recaptcha"
18+
],
19+
"author": "Hugo Dozois <hugo@dozoisch.com>",
20+
"license": "MIT",
21+
"bugs": {
22+
"url": "https://github.com/dozoisch/react-google-recaptcha/issues"
23+
},
24+
"homepage": "https://github.com/dozoisch/react-google-recaptcha",
25+
"peerDependencies": {
26+
"react": ">=0.13"
27+
},
28+
"devDependencies": {}
29+
}

0 commit comments

Comments
 (0)