Skip to content

Commit 1ff4eae

Browse files
authored
Merge pull request dozoisch#94 from dozoisch/1.x
1.0.0 branch PR
2 parents b6bfe36 + c5f10df commit 1ff4eae

21 files changed

+8300
-354
lines changed

.babelrc

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
{
2-
32
"env": {
43
"development": {
54
"presets": [

.eslintrc

Lines changed: 0 additions & 39 deletions
This file was deleted.

.eslintrc.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
"use strict";
2+
module.exports = {
3+
extends: ["eslint:recommended", "prettier", "prettier/react"],
4+
parserOptions: {
5+
sourceType: "module",
6+
ecmaVersion: "2018",
7+
ecmaFeatures: {
8+
jsx: true,
9+
},
10+
},
11+
env: {
12+
es6: true,
13+
browser: true,
14+
},
15+
plugins: ["prettier"],
16+
rules: {
17+
"prettier/prettier": "error",
18+
},
19+
};

.prettierrc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"printWidth": 100,
3+
"trailingComma": "all",
4+
"parser": "babylon"
5+
}

.travis.yml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
sudo: false
22
language: node_js
33
node_js:
4-
- "7.0"
5-
- "6.0"
6-
- "5.0"
7-
- "4.0"
4+
- "10.0"
5+
- "8.0"
86

97
branches:
108
only:

CHANGELOG.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,15 @@
1+
v1.0.0 - Fri, 17 Aug 2018 18:11:00 PST
2+
---------------------------------------
3+
4+
- Update to react-async-script 1.0 [(#94)](https://github.com/dozoisch/react-google-recaptcha/pull/94)
5+
- Add on Error [(#97)](https://github.com/dozoisch/react-google-recaptcha/pull/97)
6+
- Update build tools
7+
- Node 8 [(#95)](https://github.com/dozoisch/react-google-recaptcha/pull/95)
8+
- Jest [(#95)](https://github.com/dozoisch/react-google-recaptcha/pull/95)
9+
- Eslint/Prettier [(#96)](https://github.com/dozoisch/react-google-recaptcha/pull/96)
10+
11+
12+
113
v0.14.0 - Sun, 29 Jul 2018 19:20:03 GMT
214
---------------------------------------
315

README.md

Lines changed: 68 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
[![Build Status][travis.img]][travis.url] [![npm version][npm.img]][npm.url] [![npm downloads][npm.dl.img]][npm.dl.url] [![Dependencies][deps.img]][deps.url]
44

5-
Component wrapper for [Google reCAPTCHA v2][reCAPTCHA]
5+
React component for [Google reCAPTCHA v2][reCAPTCHA].
66

77
## Installation
88

@@ -12,96 +12,96 @@ npm install --save react-google-recaptcha
1212

1313
## Usage
1414

15-
All you need to do is [sign up for an API key pair][signup]. You will need the client key.
15+
All you need to do is [sign up for an API key pair][signup]. You will need the client key then you can use `<ReCAPTCHA />`.
1616

17-
You can then use the reCAPTCHA. The default require imports a wrapped component that loads the reCAPTCHA script asynchronously.
17+
The default usage imports a wrapped component that loads the google recaptcha script asynchronously then instantiates a `reCAPTCHA` the user can then interact with.
1818

19+
Here is a simple working [example on codesandbox.io](https://codesandbox.io/s/1y4zzjq37l).
20+
21+
Code Example:
1922
```jsx
20-
var React = require("react").default;
21-
var render = require("react-dom").render
22-
var ReCAPTCHA = require("react-google-recaptcha");
23+
import ReCAPTCHA from "react-google-recaptcha";
2324

2425
function onChange(value) {
2526
console.log("Captcha value:", value);
2627
}
2728

28-
render(
29+
ReactDOM.render(
2930
<ReCAPTCHA
30-
ref="recaptcha"
3131
sitekey="Your client site key"
3232
onChange={onChange}
3333
/>,
3434
document.body
3535
);
3636
```
3737

38-
### Rendering Props
38+
### Component Props
3939

40-
Other properties can be used to customise the rendering.
40+
Properties used to customise the rendering:
4141

4242
| Name | Type | Description |
4343
|:---- | ---- | ------ |
4444
| sitekey | string | The API client key |
4545
| onChange | func | The function to be called when the user successfully completes the captcha |
46-
| theme | enum | *optional* `light` or `dark` The theme of the widget *(__defaults:__ light)*. See [example][docs_theme]
47-
| type | enum | *optional* `image` or `audio` The type of initial captcha *(__defaults:__ image)*
48-
| tabindex | number | *optional* The tabindex on the element *(__default:__ 0)*
46+
| theme | enum | *optional* `light` or `dark` The theme of the widget *(__defaults:__ `light`)*. See [example][docs_theme]
47+
| type | enum | *optional* `image` or `audio` The type of initial captcha *(__defaults:__ `image`)*
48+
| tabindex | number | *optional* The tabindex on the element *(__default:__ `0`)*
4949
| 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. |
50+
| onErrored | func | *optional* callback when the challenge errored, most likely due to network issues. |
5051
| stoken | string | *optional* set the stoken parameter, which allows the captcha to be used from different domains, see [reCAPTCHA secure-token] |
5152
| size | enum | *optional* `compact`, `normal` or `invisible`. This allows you to change the size or do an invisible captcha |
5253
| badge | enum | *optional* `bottomright`, `bottomleft` or `inline`. Positions reCAPTCHA badge. *Only for invisible reCAPTCHA* |
5354

55+
### Component Instance API
5456

55-
__lang__: In order to translate the reCaptcha widget, you should create a global variable configuring the desired language. If you don't provide it, reCaptcha will pick up the user's interface language.
56-
57-
__useRecaptchaNet__: If google.com is blocked, you can set useRecaptchaNet to `true` so that the component uses recaptcha.net instead.
58-
59-
__removeOnMount__: If you plan to change the lang dynamically, removeOnMount should probably be true. This will allow you to unmount the reCAPTCHA component and remount it with a new language.
60-
61-
```js
62-
window.recaptchaOptions = {
63-
lang: 'fr',
64-
useRecaptchaNet: true,
65-
removeOnMount: false,
66-
};
67-
```
68-
69-
## Component API
70-
71-
The component also has some utility functions that can be called.
57+
The component instance also has some utility functions that can be called. These can be accessed via `ref`.
7258

7359
- `getValue()` returns the value of the captcha field
7460
- `getWidgetId()` returns the recaptcha widget Id
7561
- `reset()` forces reset. See the [JavaScript API doc][js_api]
7662
- `execute()` programmatically invoke the challenge
7763
- need to call when using `"invisible"` reCAPTCHA - [example below](#invisible-recaptcha)
7864

65+
Example:
66+
```javascript
67+
const recaptchaRef = React.createRef();
68+
...
69+
onSubmit = () ={
70+
const recaptchaValue = recaptchaRef.getValue();
71+
this.props.onSubmit(recaptchaValue);
72+
}
73+
render() {
74+
return (
75+
<form onSubmit={this.onSubmit} >
76+
<ReCAPTCHA
77+
ref={recaptchaRef}
78+
sitekey="Your client site key"
79+
onChange={onChange}
80+
/>
81+
</form>
82+
)
83+
}
84+
```
85+
7986
### Invisible reCAPTCHA
8087

8188
[Invisible reCAPTCHA](https://developers.google.com/recaptcha/docs/versions)
8289

83-
Starting with 0.7.0, the component now supports invisible options. See the [reCAPTCHA documentation](https://developers.google.com/recaptcha/docs/invisible) to see how to configure it.
90+
See the [reCAPTCHA documentation](https://developers.google.com/recaptcha/docs/invisible) to see how to configure it.
8491

8592
With the invisible option, you need to handle things a bit differently. You will need to call the `execute` method yourself.
8693

8794
```jsx
88-
var React = require("react").default;
89-
var render = require("react-dom").render
90-
var ReCAPTCHA = require("react-google-recaptcha");
95+
import ReCAPTCHA from "react-google-recaptcha";
9196

92-
function onChange(value) {
93-
console.log("Captcha value:", value);
94-
}
95-
96-
let captcha;
97+
const recaptchaRef = React.createRef();
9798

98-
render(
99+
ReactDOM.render(
99100
<form onSubmit={() => { captcha.execute(); }}>
100101
<ReCAPTCHA
101-
ref={(el) => { captcha = el; }}
102+
ref={recaptchaRef}
102103
size="invisible"
103104
sitekey="Your client site key"
104-
onChange={onChange}
105105
/>
106106
</form>,
107107
document.body
@@ -111,43 +111,48 @@ render(
111111

112112
### Advanced usage
113113

114+
#### Global properties used by reCaptcha
115+
116+
__lang__: By default google reCaptcha infers the user's interface language. In order to overwrite the language and update the translation for the reCaptcha widget, you can create a global variable configuring the desired language via `lang`.
117+
118+
__useRecaptchaNet__: If google.com is blocked, you can set `useRecaptchaNet` to `true` so that the component uses recaptcha.net instead.
119+
120+
__removeOnUnmount__: If you plan to change the lang dynamically, `removeOnUnmount` should probably be `true`. This unloads the google recaptcha script on `componetWillUnmount` to allow for a new google recaptcha script to load next time the reCAPTCHA component is used to facilitate a new language if needed.
121+
122+
Example global properties:
123+
```js
124+
window.recaptchaOptions = {
125+
lang: 'fr',
126+
useRecaptchaNet: true,
127+
removeOnUnmount: false,
128+
};
129+
```
130+
131+
#### ReCaptcha loading google recaptcha script manually
132+
114133
You can also use the barebone components doing the following. Using that component will oblige you to manage the grecaptcha dep and load the script by yourself.
115134

116135
```jsx
117-
var React = require("react").default;
118-
var render = require("react-dom").render
119-
var ReCAPTCHA = require("react-google-recaptcha/lib/recaptcha");
136+
import { ReCAPTCHA } from "react-google-recaptcha";
120137

121-
var grecaptchaObject = grecaptcha // You must provide access to the google grecaptcha object.
122-
123-
function onChange(value) {
124-
console.log("Captcha value:", value);
125-
}
138+
const grecaptchaObject = window.grecaptcha // You must provide access to the google grecaptcha object.
126139

127140
render(
128141
<ReCAPTCHA
129-
ref="recaptcha"
142+
ref={(r) => this.recaptcha = r}
130143
sitekey="Your client site key"
131-
onChange={onChange}
132144
grecaptcha={grecaptchaObject}
133145
/>,
134146
document.body
135147
);
136148
```
137149

138-
## Notes
150+
## Notes on Requirements
151+
At least `[email protected]` is required due to `forwardRef` usage in the dependency [react-async-script](https://github.com/dozoisch/react-async-script).
139152

140-
### React < 15.5
141-
With version prior to 15.5
142-
```shell
143-
npm install --save [email protected]
144-
```
153+
## Notes
145154

146-
### React 0.13
147-
With 0.13, install version 0.4.1
148-
```shell
149-
npm install --save [email protected]
150-
```
155+
Pre `1.0.0` and `React < 16.4.1` support details in [0.14.0](https://github.com/dozoisch/react-google-recaptcha/tree/v0.14.0).
151156

152157
[travis.img]: https://travis-ci.org/dozoisch/react-google-recaptcha.svg?branch=master
153158
[travis.url]: https://travis-ci.org/dozoisch/react-google-recaptcha

0 commit comments

Comments
 (0)