@hcaptcha/react-hcaptcha 0.3.9 → 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 +59 -0
- package/dist/index.js +8 -10
- package/package.json +1 -1
- package/src/index.js +10 -7
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|
|
package/dist/index.js
CHANGED
|
@@ -4,8 +4,6 @@ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefau
|
|
|
4
4
|
|
|
5
5
|
var _typeof2 = _interopRequireDefault(require("@babel/runtime/helpers/typeof"));
|
|
6
6
|
|
|
7
|
-
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
|
|
8
|
-
|
|
9
7
|
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
|
|
10
8
|
|
|
11
9
|
var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
|
|
@@ -18,10 +16,6 @@ var _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime
|
|
|
18
16
|
|
|
19
17
|
var _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf"));
|
|
20
18
|
|
|
21
|
-
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }
|
|
22
|
-
|
|
23
|
-
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { (0, _defineProperty2["default"])(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
|
|
24
|
-
|
|
25
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; } }
|
|
@@ -172,13 +166,17 @@ var HCaptcha = /*#__PURE__*/function (_React$Component) {
|
|
|
172
166
|
key: "renderCaptcha",
|
|
173
167
|
value: function renderCaptcha(onReady) {
|
|
174
168
|
var isApiReady = this.state.isApiReady;
|
|
175
|
-
if (!isApiReady) return;
|
|
176
|
-
|
|
177
|
-
var captchaId = hcaptcha.render(this.ref.current, _objectSpread(_objectSpread({}, this.props), {}, {
|
|
169
|
+
if (!isApiReady) return;
|
|
170
|
+
var renderParams = Object.assign({
|
|
178
171
|
"error-callback": this.handleError,
|
|
179
172
|
"expired-callback": this.handleExpire,
|
|
180
173
|
"callback": this.handleSubmit
|
|
181
|
-
}
|
|
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);
|
|
182
180
|
this.setState({
|
|
183
181
|
isRemoved: false,
|
|
184
182
|
captchaId: captchaId
|
package/package.json
CHANGED
package/src/index.js
CHANGED
|
@@ -123,14 +123,17 @@ class HCaptcha extends React.Component {
|
|
|
123
123
|
const { isApiReady } = this.state;
|
|
124
124
|
if (!isApiReady) return;
|
|
125
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
|
+
|
|
126
135
|
//Render hCaptcha widget and provide necessary callbacks - hCaptcha
|
|
127
|
-
const captchaId = hcaptcha.render(this.ref.current,
|
|
128
|
-
{
|
|
129
|
-
...this.props,
|
|
130
|
-
"error-callback" : this.handleError,
|
|
131
|
-
"expired-callback" : this.handleExpire,
|
|
132
|
-
"callback" : this.handleSubmit,
|
|
133
|
-
});
|
|
136
|
+
const captchaId = hcaptcha.render(this.ref.current, renderParams);
|
|
134
137
|
|
|
135
138
|
this.setState({ isRemoved: false, captchaId }, () => {
|
|
136
139
|
onReady && onReady();
|