@hcaptcha/react-hcaptcha 1.11.0 → 1.11.1

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/dist/esm/index.js CHANGED
@@ -154,10 +154,17 @@ var HCaptcha = /*#__PURE__*/function (_React$Component) {
154
154
  hCaptchaLoader(mountParams).then(this.handleOnLoad, this.handleError)["catch"](this.handleError);
155
155
  this.apiScriptRequested = true;
156
156
  };
157
- _proto.renderCaptcha = function renderCaptcha(onReady) {
157
+ _proto.renderCaptcha = function renderCaptcha(onRender) {
158
158
  var _this4 = this;
159
- var isApiReady = this.state.isApiReady;
160
- if (!isApiReady) return;
159
+ var _this$state = this.state,
160
+ isApiReady = _this$state.isApiReady,
161
+ captchaId = _this$state.captchaId;
162
+ var onReady = this.props.onReady;
163
+
164
+ // Prevent calling hCaptcha render on two conditions:
165
+ // • API is not ready
166
+ // • Component has already been mounted
167
+ if (!isApiReady || captchaId) return;
161
168
  var renderParams = Object.assign({
162
169
  "open-callback": this.handleOpen,
163
170
  "close-callback": this.handleClose,
@@ -171,13 +178,14 @@ var HCaptcha = /*#__PURE__*/function (_React$Component) {
171
178
  });
172
179
  var hcaptcha = this._hcaptcha;
173
180
  //Render hCaptcha widget and provide necessary callbacks - hCaptcha
174
- var captchaId = hcaptcha.render(this.ref.current, renderParams);
181
+ var id = hcaptcha.render(this.ref.current, renderParams);
175
182
  this.setState({
176
183
  isRemoved: false,
177
- captchaId: captchaId
184
+ captchaId: id
178
185
  }, function () {
186
+ onRender && onRender();
179
187
  onReady && onReady();
180
- _this4._onReady && _this4._onReady(captchaId);
188
+ _this4._onReady && _this4._onReady(id);
181
189
  });
182
190
  };
183
191
  _proto.resetCaptcha = function resetCaptcha() {
@@ -234,9 +242,9 @@ var HCaptcha = /*#__PURE__*/function (_React$Component) {
234
242
  };
235
243
  _proto.handleSubmit = function handleSubmit(event) {
236
244
  var onVerify = this.props.onVerify;
237
- var _this$state = this.state,
238
- isRemoved = _this$state.isRemoved,
239
- captchaId = _this$state.captchaId;
245
+ var _this$state2 = this.state,
246
+ isRemoved = _this$state2.isRemoved,
247
+ captchaId = _this$state2.captchaId;
240
248
  var hcaptcha = this._hcaptcha;
241
249
  if (typeof hcaptcha === 'undefined' || isRemoved) return;
242
250
  var token = hcaptcha.getResponse(captchaId); //Get response token from hCaptcha widget
@@ -269,9 +277,9 @@ var HCaptcha = /*#__PURE__*/function (_React$Component) {
269
277
  if (onError) onError(event);
270
278
  };
271
279
  _proto.isReady = function isReady() {
272
- var _this$state2 = this.state,
273
- isApiReady = _this$state2.isApiReady,
274
- isRemoved = _this$state2.isRemoved;
280
+ var _this$state3 = this.state,
281
+ isApiReady = _this$state3.isApiReady,
282
+ isRemoved = _this$state3.isRemoved;
275
283
  return isApiReady && !isRemoved;
276
284
  };
277
285
  _proto.handleOpen = function handleOpen() {
@@ -328,6 +336,14 @@ var HCaptcha = /*#__PURE__*/function (_React$Component) {
328
336
  return null;
329
337
  }
330
338
  };
339
+ _proto.close = function close() {
340
+ var captchaId = this.state.captchaId;
341
+ var hcaptcha = this._hcaptcha;
342
+ if (!this.isReady()) {
343
+ return;
344
+ }
345
+ return hcaptcha.close(captchaId);
346
+ };
331
347
  _proto.setData = function setData(data) {
332
348
  var captchaId = this.state.captchaId;
333
349
  var hcaptcha = this._hcaptcha;
package/dist/index.js CHANGED
@@ -183,10 +183,17 @@ var HCaptcha = /*#__PURE__*/function (_React$Component) {
183
183
  }
184
184
  }, {
185
185
  key: "renderCaptcha",
186
- value: function renderCaptcha(onReady) {
186
+ value: function renderCaptcha(onRender) {
187
187
  var _this4 = this;
188
- var isApiReady = this.state.isApiReady;
189
- if (!isApiReady) return;
188
+ var _this$state = this.state,
189
+ isApiReady = _this$state.isApiReady,
190
+ captchaId = _this$state.captchaId;
191
+ var onReady = this.props.onReady;
192
+
193
+ // Prevent calling hCaptcha render on two conditions:
194
+ // • API is not ready
195
+ // • Component has already been mounted
196
+ if (!isApiReady || captchaId) return;
190
197
  var renderParams = Object.assign({
191
198
  "open-callback": this.handleOpen,
192
199
  "close-callback": this.handleClose,
@@ -200,13 +207,14 @@ var HCaptcha = /*#__PURE__*/function (_React$Component) {
200
207
  });
201
208
  var hcaptcha = this._hcaptcha;
202
209
  //Render hCaptcha widget and provide necessary callbacks - hCaptcha
203
- var captchaId = hcaptcha.render(this.ref.current, renderParams);
210
+ var id = hcaptcha.render(this.ref.current, renderParams);
204
211
  this.setState({
205
212
  isRemoved: false,
206
- captchaId: captchaId
213
+ captchaId: id
207
214
  }, function () {
215
+ onRender && onRender();
208
216
  onReady && onReady();
209
- _this4._onReady && _this4._onReady(captchaId);
217
+ _this4._onReady && _this4._onReady(id);
210
218
  });
211
219
  }
212
220
  }, {
@@ -271,9 +279,9 @@ var HCaptcha = /*#__PURE__*/function (_React$Component) {
271
279
  key: "handleSubmit",
272
280
  value: function handleSubmit(event) {
273
281
  var onVerify = this.props.onVerify;
274
- var _this$state = this.state,
275
- isRemoved = _this$state.isRemoved,
276
- captchaId = _this$state.captchaId;
282
+ var _this$state2 = this.state,
283
+ isRemoved = _this$state2.isRemoved,
284
+ captchaId = _this$state2.captchaId;
277
285
  var hcaptcha = this._hcaptcha;
278
286
  if (typeof hcaptcha === 'undefined' || isRemoved) return;
279
287
  var token = hcaptcha.getResponse(captchaId); //Get response token from hCaptcha widget
@@ -312,9 +320,9 @@ var HCaptcha = /*#__PURE__*/function (_React$Component) {
312
320
  }, {
313
321
  key: "isReady",
314
322
  value: function isReady() {
315
- var _this$state2 = this.state,
316
- isApiReady = _this$state2.isApiReady,
317
- isRemoved = _this$state2.isRemoved;
323
+ var _this$state3 = this.state,
324
+ isApiReady = _this$state3.isApiReady,
325
+ isRemoved = _this$state3.isRemoved;
318
326
  return isApiReady && !isRemoved;
319
327
  }
320
328
  }, {
@@ -377,6 +385,16 @@ var HCaptcha = /*#__PURE__*/function (_React$Component) {
377
385
  return null;
378
386
  }
379
387
  }
388
+ }, {
389
+ key: "close",
390
+ value: function close() {
391
+ var captchaId = this.state.captchaId;
392
+ var hcaptcha = this._hcaptcha;
393
+ if (!this.isReady()) {
394
+ return;
395
+ }
396
+ return hcaptcha.close(captchaId);
397
+ }
380
398
  }, {
381
399
  key: "setData",
382
400
  value: function setData(data) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hcaptcha/react-hcaptcha",
3
- "version": "1.11.0",
3
+ "version": "1.11.1",
4
4
  "types": "types/index.d.ts",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/esm/index.js",
package/src/index.js CHANGED
@@ -170,9 +170,14 @@ class HCaptcha extends React.Component {
170
170
  this.apiScriptRequested = true;
171
171
  }
172
172
 
173
- renderCaptcha(onReady) {
174
- const { isApiReady } = this.state;
175
- if (!isApiReady) return;
173
+ renderCaptcha(onRender) {
174
+ const { isApiReady, captchaId } = this.state;
175
+ const { onReady } = this.props;
176
+
177
+ // Prevent calling hCaptcha render on two conditions:
178
+ // • API is not ready
179
+ // • Component has already been mounted
180
+ if (!isApiReady || captchaId) return;
176
181
 
177
182
  const renderParams = Object.assign({
178
183
  "open-callback" : this.handleOpen,
@@ -188,11 +193,12 @@ class HCaptcha extends React.Component {
188
193
 
189
194
  const hcaptcha = this._hcaptcha;
190
195
  //Render hCaptcha widget and provide necessary callbacks - hCaptcha
191
- const captchaId = hcaptcha.render(this.ref.current, renderParams);
196
+ const id = hcaptcha.render(this.ref.current, renderParams);
192
197
 
193
- this.setState({ isRemoved: false, captchaId }, () => {
198
+ this.setState({ isRemoved: false, captchaId: id }, () => {
199
+ onRender && onRender();
194
200
  onReady && onReady();
195
- this._onReady && this._onReady(captchaId);
201
+ this._onReady && this._onReady(id);
196
202
  });
197
203
  }
198
204
 
@@ -367,6 +373,17 @@ class HCaptcha extends React.Component {
367
373
  }
368
374
  }
369
375
 
376
+ close() {
377
+ const { captchaId } = this.state;
378
+ const hcaptcha = this._hcaptcha;
379
+
380
+ if (!this.isReady()) {
381
+ return;
382
+ }
383
+
384
+ return hcaptcha.close(captchaId);
385
+ }
386
+
370
387
  setData (data) {
371
388
  const { captchaId } = this.state;
372
389
  const hcaptcha = this._hcaptcha;