@forms.expert/sdk 0.2.8 → 0.2.9
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/react/index.cjs +14 -13
- package/dist/react/index.cjs.map +1 -1
- package/dist/react/index.js +14 -13
- package/dist/react/index.js.map +1 -1
- package/package.json +1 -1
package/dist/react/index.js
CHANGED
|
@@ -378,24 +378,25 @@ function useForm(options) {
|
|
|
378
378
|
const [values, setValues] = useState({});
|
|
379
379
|
const [error, setError] = useState(null);
|
|
380
380
|
const [uploadProgress, setUploadProgress] = useState(null);
|
|
381
|
+
const { slug, trackViews, lang, autoInit, onSuccess, onError, onValidationError } = options;
|
|
381
382
|
const initialize = useCallback(async () => {
|
|
382
383
|
setIsInitializing(true);
|
|
383
384
|
try {
|
|
384
|
-
const formConfig = await sdk.isActive(
|
|
385
|
+
const formConfig = await sdk.isActive(slug, lang);
|
|
385
386
|
setConfig(formConfig);
|
|
386
|
-
if (
|
|
387
|
-
sdk.trackView(
|
|
387
|
+
if (trackViews) {
|
|
388
|
+
void sdk.trackView(slug);
|
|
388
389
|
}
|
|
389
390
|
return formConfig;
|
|
390
391
|
} finally {
|
|
391
392
|
setIsInitializing(false);
|
|
392
393
|
}
|
|
393
|
-
}, [sdk,
|
|
394
|
+
}, [sdk, slug, trackViews, lang]);
|
|
394
395
|
useEffect(() => {
|
|
395
|
-
if (
|
|
396
|
+
if (autoInit !== false) {
|
|
396
397
|
initialize();
|
|
397
398
|
}
|
|
398
|
-
}, [initialize,
|
|
399
|
+
}, [initialize, autoInit]);
|
|
399
400
|
const setValue = useCallback((name, value) => {
|
|
400
401
|
setValues((prev) => ({ ...prev, [name]: value }));
|
|
401
402
|
setErrors((prev) => {
|
|
@@ -410,7 +411,7 @@ function useForm(options) {
|
|
|
410
411
|
setValues((prev) => ({ ...prev, ...newValues }));
|
|
411
412
|
}, []);
|
|
412
413
|
const validate = useCallback(async () => {
|
|
413
|
-
const result = await sdk.validate(
|
|
414
|
+
const result = await sdk.validate(slug, values);
|
|
414
415
|
if (!result.valid) {
|
|
415
416
|
const errorMap = result.errors.reduce(
|
|
416
417
|
(acc, err) => ({ ...acc, [err.field]: err.message }),
|
|
@@ -419,7 +420,7 @@ function useForm(options) {
|
|
|
419
420
|
setErrors(errorMap);
|
|
420
421
|
}
|
|
421
422
|
return result.valid;
|
|
422
|
-
}, [sdk,
|
|
423
|
+
}, [sdk, slug, values]);
|
|
423
424
|
const submit = useCallback(
|
|
424
425
|
async (captchaToken) => {
|
|
425
426
|
setIsLoading(true);
|
|
@@ -429,12 +430,12 @@ function useForm(options) {
|
|
|
429
430
|
setUploadProgress(null);
|
|
430
431
|
try {
|
|
431
432
|
const submitData = config?.settings?.honeypot ? { ...values, _hp: "" } : values;
|
|
432
|
-
const response = await sdk.submit(
|
|
433
|
+
const response = await sdk.submit(slug, submitData, {
|
|
433
434
|
captchaToken,
|
|
434
435
|
onProgress: setUploadProgress
|
|
435
436
|
});
|
|
436
437
|
setIsSubmitted(true);
|
|
437
|
-
|
|
438
|
+
onSuccess?.(response);
|
|
438
439
|
return response;
|
|
439
440
|
} catch (err) {
|
|
440
441
|
if (err instanceof FormValidationError) {
|
|
@@ -443,10 +444,10 @@ function useForm(options) {
|
|
|
443
444
|
{}
|
|
444
445
|
);
|
|
445
446
|
setErrors(errorMap);
|
|
446
|
-
|
|
447
|
+
onValidationError?.(err.errors);
|
|
447
448
|
} else {
|
|
448
449
|
setError(err);
|
|
449
|
-
|
|
450
|
+
onError?.(err);
|
|
450
451
|
}
|
|
451
452
|
return null;
|
|
452
453
|
} finally {
|
|
@@ -454,7 +455,7 @@ function useForm(options) {
|
|
|
454
455
|
setUploadProgress(null);
|
|
455
456
|
}
|
|
456
457
|
},
|
|
457
|
-
[sdk,
|
|
458
|
+
[sdk, slug, values, config?.settings?.honeypot, onSuccess, onError, onValidationError]
|
|
458
459
|
);
|
|
459
460
|
const reset = useCallback(() => {
|
|
460
461
|
setValues({});
|