@hcaptcha/react-hcaptcha 0.3.6 → 0.3.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +61 -1
- package/dist/index.js +47 -42
- package/dist/utils.js +4 -12
- package/package.json +3 -1
- package/src/index.js +30 -19
- package/types/index.d.ts +8 -1
package/README.md
CHANGED
|
@@ -47,12 +47,71 @@ import { default as RenamedCaptcha } from '../utils/captcha';
|
|
|
47
47
|
</FormComponent>
|
|
48
48
|
```
|
|
49
49
|
|
|
50
|
+
#### Programmatic Usage
|
|
51
|
+
In the event you want to call the hCaptcha client API directly, you can do so by using the hook `useRef` and waiting for `onLoad` to be called. By waiting for `onLoad` the hCaptcha API will be ready and the hCaptcha client will have been setup. See the following example:
|
|
52
|
+
|
|
53
|
+
```js
|
|
54
|
+
import { useEffect, useRef, useState } from "react";
|
|
55
|
+
import HCaptcha from "@hcaptcha/react-hcaptcha";
|
|
56
|
+
|
|
57
|
+
export default function Form() {
|
|
58
|
+
const [token, setToken] = useState(null);
|
|
59
|
+
const captchaRef = useRef(null);
|
|
60
|
+
|
|
61
|
+
const onLoad = () => {
|
|
62
|
+
// this reaches out to the hCaptcha JS API and runs the
|
|
63
|
+
// execute function on it. you can use other functions as
|
|
64
|
+
// documented here:
|
|
65
|
+
// https://docs.hcaptcha.com/configuration#jsapi
|
|
66
|
+
captchaRef.current.execute();
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
useEffect(() => {
|
|
70
|
+
|
|
71
|
+
if (token)
|
|
72
|
+
console.log(`hCaptcha Token: ${token}`);
|
|
73
|
+
|
|
74
|
+
}, [token]);
|
|
75
|
+
|
|
76
|
+
return (
|
|
77
|
+
<form>
|
|
78
|
+
<HCaptcha
|
|
79
|
+
sitekey="your-sitekey"
|
|
80
|
+
onLoad={onLoad}
|
|
81
|
+
onVerify={setToken}
|
|
82
|
+
ref={captchaRef}
|
|
83
|
+
/>
|
|
84
|
+
</form>
|
|
85
|
+
);
|
|
86
|
+
}
|
|
87
|
+
```
|
|
88
|
+
|
|
50
89
|
#### Advanced usage
|
|
51
90
|
|
|
52
91
|
In most real-world implementations, you'll probably be using a form library such as [Formik](https://github.com/jaredpalmer/formik) or [React Hook Form](https://github.com/react-hook-form/react-hook-form).
|
|
53
92
|
|
|
54
93
|
In these instances, you'll most likely want to use `ref` to handle the callbacks as well as handle field-level validation of a `captcha` field. For an example of this, you can view this [CodeSandbox](https://codesandbox.io/s/react-hcaptchaform-example-forked-ngxge?file=/src/Form.jsx). This `ref` will point to an instance of the [hCaptcha API](https://docs.hcaptcha.com/configuration#jsapi) where can you interact directly with it.
|
|
55
94
|
|
|
95
|
+
#### Passing in fields like `rqdata` to `execute()`
|
|
96
|
+
|
|
97
|
+
Passing an object into the `execute(yourObj)` call will send it through to the underlying JS API. This enables support for Enterprise features like `rqdata`. A simple example is below:
|
|
98
|
+
|
|
99
|
+
```
|
|
100
|
+
const {sitekey, rqdata} = props;
|
|
101
|
+
const captchaRef = React.useRef<HCaptcha>(null);
|
|
102
|
+
|
|
103
|
+
const onLoad = () => {
|
|
104
|
+
const executePayload = {};
|
|
105
|
+
if (rqdata) {
|
|
106
|
+
executePayload['rqdata'] = rqdata;
|
|
107
|
+
}
|
|
108
|
+
captchaRef.current?.execute(executePayload);
|
|
109
|
+
};
|
|
110
|
+
|
|
111
|
+
return <HCaptcha ref={captchaRef} onLoad={onLoad} sitekey={sitekey} {...props} />;
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
|
|
56
115
|
### Props
|
|
57
116
|
|
|
58
117
|
|Name|Values/Type|Required|Default|Description|
|
|
@@ -71,6 +130,7 @@ In these instances, you'll most likely want to use `ref` to handle the callbacks
|
|
|
71
130
|
|`imghost`|String|No|`-`|See enterprise docs.|
|
|
72
131
|
|`reportapi`|String|No|`-`|See enterprise docs.|
|
|
73
132
|
|`sentry`|String|No|`-`|See enterprise docs.|
|
|
133
|
+
|`custom`|Boolean|No|`-`|See enterprise docs.|
|
|
74
134
|
|
|
75
135
|
### Events
|
|
76
136
|
|
|
@@ -85,7 +145,7 @@ In these instances, you'll most likely want to use `ref` to handle the callbacks
|
|
|
85
145
|
|
|
86
146
|
|Method|Description|
|
|
87
147
|
|---|---|
|
|
88
|
-
|`execute()`|Programmatically trigger a challenge request
|
|
148
|
+
|`execute()`|Programmatically trigger a challenge request. Additionally, this method can be run asynchronously and returns a promise with the `token` and `eKey` when the challenge is completed.|
|
|
89
149
|
|`resetCaptcha()`|Reset the current challenge|
|
|
90
150
|
|
|
91
151
|
|
package/dist/index.js
CHANGED
|
@@ -1,33 +1,25 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
var _typeof2 = _interopRequireDefault(require("@babel/runtime/helpers/typeof"));
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
|
|
8
8
|
|
|
9
|
-
|
|
9
|
+
var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
|
|
10
10
|
|
|
11
|
-
|
|
11
|
+
var _assertThisInitialized2 = _interopRequireDefault(require("@babel/runtime/helpers/assertThisInitialized"));
|
|
12
12
|
|
|
13
|
-
|
|
13
|
+
var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits"));
|
|
14
14
|
|
|
15
|
-
|
|
15
|
+
var _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn"));
|
|
16
16
|
|
|
17
|
-
|
|
17
|
+
var _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf"));
|
|
18
18
|
|
|
19
|
-
function
|
|
20
|
-
|
|
21
|
-
function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
|
|
22
|
-
|
|
23
|
-
function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }
|
|
24
|
-
|
|
25
|
-
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
|
|
19
|
+
function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = (0, _getPrototypeOf2["default"])(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = (0, _getPrototypeOf2["default"])(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return (0, _possibleConstructorReturn2["default"])(this, result); }; }
|
|
26
20
|
|
|
27
21
|
function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Date.prototype.toString.call(Reflect.construct(Date, [], function () {})); return true; } catch (e) { return false; } }
|
|
28
22
|
|
|
29
|
-
function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
|
|
30
|
-
|
|
31
23
|
var React = require('react');
|
|
32
24
|
|
|
33
25
|
var _require = require("./utils.js"),
|
|
@@ -60,25 +52,24 @@ var mountCaptchaScript = function mountCaptchaScript() {
|
|
|
60
52
|
};
|
|
61
53
|
|
|
62
54
|
var HCaptcha = /*#__PURE__*/function (_React$Component) {
|
|
63
|
-
|
|
55
|
+
(0, _inherits2["default"])(HCaptcha, _React$Component);
|
|
64
56
|
|
|
65
57
|
var _super = _createSuper(HCaptcha);
|
|
66
58
|
|
|
67
59
|
function HCaptcha(props) {
|
|
68
60
|
var _this;
|
|
69
61
|
|
|
70
|
-
|
|
71
|
-
|
|
62
|
+
(0, _classCallCheck2["default"])(this, HCaptcha);
|
|
72
63
|
_this = _super.call(this, props); // API Methods
|
|
73
64
|
|
|
74
|
-
_this.renderCaptcha = _this.renderCaptcha.bind(
|
|
75
|
-
_this.resetCaptcha = _this.resetCaptcha.bind(
|
|
76
|
-
_this.removeCaptcha = _this.removeCaptcha.bind(
|
|
65
|
+
_this.renderCaptcha = _this.renderCaptcha.bind((0, _assertThisInitialized2["default"])(_this));
|
|
66
|
+
_this.resetCaptcha = _this.resetCaptcha.bind((0, _assertThisInitialized2["default"])(_this));
|
|
67
|
+
_this.removeCaptcha = _this.removeCaptcha.bind((0, _assertThisInitialized2["default"])(_this)); // Event Handlers
|
|
77
68
|
|
|
78
|
-
_this.handleOnLoad = _this.handleOnLoad.bind(
|
|
79
|
-
_this.handleSubmit = _this.handleSubmit.bind(
|
|
80
|
-
_this.handleExpire = _this.handleExpire.bind(
|
|
81
|
-
_this.handleError = _this.handleError.bind(
|
|
69
|
+
_this.handleOnLoad = _this.handleOnLoad.bind((0, _assertThisInitialized2["default"])(_this));
|
|
70
|
+
_this.handleSubmit = _this.handleSubmit.bind((0, _assertThisInitialized2["default"])(_this));
|
|
71
|
+
_this.handleExpire = _this.handleExpire.bind((0, _assertThisInitialized2["default"])(_this));
|
|
72
|
+
_this.handleError = _this.handleError.bind((0, _assertThisInitialized2["default"])(_this));
|
|
82
73
|
var isApiReady = typeof hcaptcha !== 'undefined';
|
|
83
74
|
_this.ref = React.createRef();
|
|
84
75
|
_this.state = {
|
|
@@ -90,7 +81,7 @@ var HCaptcha = /*#__PURE__*/function (_React$Component) {
|
|
|
90
81
|
return _this;
|
|
91
82
|
}
|
|
92
83
|
|
|
93
|
-
|
|
84
|
+
(0, _createClass2["default"])(HCaptcha, [{
|
|
94
85
|
key: "componentDidMount",
|
|
95
86
|
value: function componentDidMount() {
|
|
96
87
|
//Once captcha is mounted intialize hCaptcha - hCaptcha
|
|
@@ -103,7 +94,8 @@ var HCaptcha = /*#__PURE__*/function (_React$Component) {
|
|
|
103
94
|
hl = _this$props.languageOverride,
|
|
104
95
|
reCaptchaCompat = _this$props.reCaptchaCompat,
|
|
105
96
|
reportapi = _this$props.reportapi,
|
|
106
|
-
sentry = _this$props.sentry
|
|
97
|
+
sentry = _this$props.sentry,
|
|
98
|
+
custom = _this$props.custom;
|
|
107
99
|
var isApiReady = this.state.isApiReady;
|
|
108
100
|
|
|
109
101
|
if (!isApiReady) {
|
|
@@ -119,7 +111,8 @@ var HCaptcha = /*#__PURE__*/function (_React$Component) {
|
|
|
119
111
|
imghost: imghost,
|
|
120
112
|
recaptchacompat: reCaptchaCompat === false ? "off" : null,
|
|
121
113
|
reportapi: reportapi,
|
|
122
|
-
sentry: sentry
|
|
114
|
+
sentry: sentry,
|
|
115
|
+
custom: custom
|
|
123
116
|
});
|
|
124
117
|
} // Add onload callback to global onload listeners
|
|
125
118
|
|
|
@@ -171,18 +164,24 @@ var HCaptcha = /*#__PURE__*/function (_React$Component) {
|
|
|
171
164
|
}
|
|
172
165
|
}, {
|
|
173
166
|
key: "renderCaptcha",
|
|
174
|
-
value: function renderCaptcha() {
|
|
167
|
+
value: function renderCaptcha(onReady) {
|
|
175
168
|
var isApiReady = this.state.isApiReady;
|
|
176
|
-
if (!isApiReady) return;
|
|
177
|
-
|
|
178
|
-
var captchaId = hcaptcha.render(this.ref.current, _objectSpread(_objectSpread({}, this.props), {}, {
|
|
169
|
+
if (!isApiReady) return;
|
|
170
|
+
var renderParams = Object.assign({
|
|
179
171
|
"error-callback": this.handleError,
|
|
180
172
|
"expired-callback": this.handleExpire,
|
|
181
173
|
"callback": this.handleSubmit
|
|
182
|
-
}
|
|
174
|
+
}, this.props, {
|
|
175
|
+
hl: this.props.hl || this.props.languageOverride,
|
|
176
|
+
languageOverride: undefined
|
|
177
|
+
}); //Render hCaptcha widget and provide necessary callbacks - hCaptcha
|
|
178
|
+
|
|
179
|
+
var captchaId = hcaptcha.render(this.ref.current, renderParams);
|
|
183
180
|
this.setState({
|
|
184
181
|
isRemoved: false,
|
|
185
182
|
captchaId: captchaId
|
|
183
|
+
}, function () {
|
|
184
|
+
onReady && onReady();
|
|
186
185
|
});
|
|
187
186
|
}
|
|
188
187
|
}, {
|
|
@@ -219,11 +218,12 @@ var HCaptcha = /*#__PURE__*/function (_React$Component) {
|
|
|
219
218
|
this.setState({
|
|
220
219
|
isApiReady: true
|
|
221
220
|
}, function () {
|
|
222
|
-
//
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
221
|
+
// render captcha and wait for captcha id
|
|
222
|
+
_this3.renderCaptcha(function () {
|
|
223
|
+
// trigger onLoad if it exists
|
|
224
|
+
var onLoad = _this3.props.onLoad;
|
|
225
|
+
if (onLoad) onLoad();
|
|
226
|
+
});
|
|
227
227
|
});
|
|
228
228
|
}
|
|
229
229
|
}, {
|
|
@@ -269,12 +269,18 @@ var HCaptcha = /*#__PURE__*/function (_React$Component) {
|
|
|
269
269
|
}, {
|
|
270
270
|
key: "execute",
|
|
271
271
|
value: function execute() {
|
|
272
|
+
var opts = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : null;
|
|
272
273
|
var _this$state7 = this.state,
|
|
273
274
|
isApiReady = _this$state7.isApiReady,
|
|
274
275
|
isRemoved = _this$state7.isRemoved,
|
|
275
276
|
captchaId = _this$state7.captchaId;
|
|
276
277
|
if (!isApiReady || isRemoved) return;
|
|
277
|
-
|
|
278
|
+
|
|
279
|
+
if (opts && (0, _typeof2["default"])(opts) !== "object") {
|
|
280
|
+
opts = null;
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
return hcaptcha.execute(captchaId, opts);
|
|
278
284
|
}
|
|
279
285
|
}, {
|
|
280
286
|
key: "render",
|
|
@@ -286,7 +292,6 @@ var HCaptcha = /*#__PURE__*/function (_React$Component) {
|
|
|
286
292
|
});
|
|
287
293
|
}
|
|
288
294
|
}]);
|
|
289
|
-
|
|
290
295
|
return HCaptcha;
|
|
291
296
|
}(React.Component);
|
|
292
297
|
|
package/dist/utils.js
CHANGED
|
@@ -1,26 +1,18 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
|
|
8
|
-
|
|
9
|
-
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
|
|
10
|
-
|
|
11
|
-
function _iterableToArrayLimit(arr, i) { if (typeof Symbol === "undefined" || !(Symbol.iterator in Object(arr))) return; var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }
|
|
12
|
-
|
|
13
|
-
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
|
|
5
|
+
var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
|
|
14
6
|
|
|
15
7
|
function generateQuery(params) {
|
|
16
8
|
return Object.entries(params).filter(function (_ref) {
|
|
17
|
-
var _ref2 =
|
|
9
|
+
var _ref2 = (0, _slicedToArray2["default"])(_ref, 2),
|
|
18
10
|
key = _ref2[0],
|
|
19
11
|
value = _ref2[1];
|
|
20
12
|
|
|
21
13
|
return value || value === false;
|
|
22
14
|
}).map(function (_ref3) {
|
|
23
|
-
var _ref4 =
|
|
15
|
+
var _ref4 = (0, _slicedToArray2["default"])(_ref3, 2),
|
|
24
16
|
key = _ref4[0],
|
|
25
17
|
value = _ref4[1];
|
|
26
18
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hcaptcha/react-hcaptcha",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.10",
|
|
4
4
|
"types": "types/index.d.ts",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"files": [
|
|
@@ -12,6 +12,7 @@
|
|
|
12
12
|
"scripts": {
|
|
13
13
|
"start": "webpack-dev-server --config ./webpack.config.js --mode development",
|
|
14
14
|
"test": "jest",
|
|
15
|
+
"watch": "babel src -d dist --copy-files --watch",
|
|
15
16
|
"transpile": "babel src -d dist --copy-files",
|
|
16
17
|
"build": "npm run transpile",
|
|
17
18
|
"prepublishOnly": "npm run transpile"
|
|
@@ -31,6 +32,7 @@
|
|
|
31
32
|
"devDependencies": {
|
|
32
33
|
"@babel/cli": "^7.12.10",
|
|
33
34
|
"@babel/core": "^7.12.10",
|
|
35
|
+
"@babel/plugin-transform-runtime": "^7.14.5",
|
|
34
36
|
"@babel/preset-env": "^7.12.11",
|
|
35
37
|
"@babel/preset-react": "^7.12.10",
|
|
36
38
|
"babel-loader": "^8.2.2",
|
package/src/index.js
CHANGED
|
@@ -59,7 +59,7 @@ class HCaptcha extends React.Component {
|
|
|
59
59
|
}
|
|
60
60
|
|
|
61
61
|
componentDidMount () { //Once captcha is mounted intialize hCaptcha - hCaptcha
|
|
62
|
-
const { apihost, assethost, endpoint, host, imghost, languageOverride:hl, reCaptchaCompat, reportapi, sentry } = this.props;
|
|
62
|
+
const { apihost, assethost, endpoint, host, imghost, languageOverride:hl, reCaptchaCompat, reportapi, sentry, custom } = this.props;
|
|
63
63
|
const { isApiReady } = this.state;
|
|
64
64
|
|
|
65
65
|
if (!isApiReady) { //Check if hCaptcha has already been loaded, if not create script tag and wait to render captcha
|
|
@@ -75,7 +75,8 @@ class HCaptcha extends React.Component {
|
|
|
75
75
|
imghost,
|
|
76
76
|
recaptchacompat: reCaptchaCompat === false? "off" : null,
|
|
77
77
|
reportapi,
|
|
78
|
-
sentry
|
|
78
|
+
sentry,
|
|
79
|
+
custom
|
|
79
80
|
});
|
|
80
81
|
}
|
|
81
82
|
|
|
@@ -118,20 +119,25 @@ class HCaptcha extends React.Component {
|
|
|
118
119
|
}
|
|
119
120
|
}
|
|
120
121
|
|
|
121
|
-
renderCaptcha() {
|
|
122
|
+
renderCaptcha(onReady) {
|
|
122
123
|
const { isApiReady } = this.state;
|
|
123
124
|
if (!isApiReady) return;
|
|
124
125
|
|
|
126
|
+
const renderParams = Object.assign({
|
|
127
|
+
"error-callback" : this.handleError,
|
|
128
|
+
"expired-callback" : this.handleExpire,
|
|
129
|
+
"callback" : this.handleSubmit,
|
|
130
|
+
}, this.props, {
|
|
131
|
+
hl: this.props.hl || this.props.languageOverride,
|
|
132
|
+
languageOverride: undefined
|
|
133
|
+
});
|
|
134
|
+
|
|
125
135
|
//Render hCaptcha widget and provide necessary callbacks - hCaptcha
|
|
126
|
-
const captchaId = hcaptcha.render(this.ref.current,
|
|
127
|
-
{
|
|
128
|
-
...this.props,
|
|
129
|
-
"error-callback" : this.handleError,
|
|
130
|
-
"expired-callback": this.handleExpire,
|
|
131
|
-
"callback" : this.handleSubmit
|
|
132
|
-
});
|
|
136
|
+
const captchaId = hcaptcha.render(this.ref.current, renderParams);
|
|
133
137
|
|
|
134
|
-
this.setState({ isRemoved: false, captchaId })
|
|
138
|
+
this.setState({ isRemoved: false, captchaId }, () => {
|
|
139
|
+
onReady && onReady();
|
|
140
|
+
});
|
|
135
141
|
}
|
|
136
142
|
|
|
137
143
|
resetCaptcha() {
|
|
@@ -155,12 +161,13 @@ class HCaptcha extends React.Component {
|
|
|
155
161
|
|
|
156
162
|
handleOnLoad () {
|
|
157
163
|
this.setState({ isApiReady: true }, () => {
|
|
158
|
-
// trigger onLoad if it exists
|
|
159
|
-
const { onLoad } = this.props;
|
|
160
|
-
if (onLoad) onLoad();
|
|
161
164
|
|
|
162
|
-
// render captcha
|
|
163
|
-
this.renderCaptcha()
|
|
165
|
+
// render captcha and wait for captcha id
|
|
166
|
+
this.renderCaptcha(() => {
|
|
167
|
+
// trigger onLoad if it exists
|
|
168
|
+
const { onLoad } = this.props;
|
|
169
|
+
if (onLoad) onLoad();
|
|
170
|
+
});
|
|
164
171
|
});
|
|
165
172
|
}
|
|
166
173
|
|
|
@@ -195,12 +202,16 @@ class HCaptcha extends React.Component {
|
|
|
195
202
|
if (onError) onError(event);
|
|
196
203
|
}
|
|
197
204
|
|
|
198
|
-
execute () {
|
|
205
|
+
execute (opts = null) {
|
|
199
206
|
const { isApiReady, isRemoved, captchaId } = this.state;
|
|
200
207
|
|
|
201
|
-
if (!isApiReady || isRemoved) return
|
|
208
|
+
if (!isApiReady || isRemoved) return;
|
|
209
|
+
|
|
210
|
+
if (opts && typeof opts !== "object") {
|
|
211
|
+
opts = null;
|
|
212
|
+
}
|
|
202
213
|
|
|
203
|
-
hcaptcha.execute(captchaId)
|
|
214
|
+
return hcaptcha.execute(captchaId, opts);
|
|
204
215
|
}
|
|
205
216
|
|
|
206
217
|
render () {
|
package/types/index.d.ts
CHANGED
|
@@ -27,11 +27,18 @@ interface HCaptchaProps {
|
|
|
27
27
|
reCaptchaCompat?: boolean;
|
|
28
28
|
}
|
|
29
29
|
|
|
30
|
+
interface ExecuteResponse {
|
|
31
|
+
response: string;
|
|
32
|
+
key: string;
|
|
33
|
+
}
|
|
34
|
+
|
|
30
35
|
declare class HCaptcha extends React.Component<HCaptchaProps, HCaptchaState> {
|
|
31
36
|
resetCaptcha(): void;
|
|
32
37
|
renderCaptcha(): void;
|
|
33
38
|
removeCaptcha(): void;
|
|
34
|
-
execute():
|
|
39
|
+
execute(opts: { async: true }): Promise<ExecuteResponse>
|
|
40
|
+
execute(opts?: { async: false }): void;
|
|
41
|
+
execute(opts?: { async: boolean }): Promise<ExecuteResponse> | void;
|
|
35
42
|
}
|
|
36
43
|
|
|
37
44
|
export = HCaptcha;
|