@trackunit/react-form-wizard 0.1.70 → 0.1.72
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/index.cjs.js +24 -1
- package/index.esm.js +25 -2
- package/package.json +1 -1
package/index.cjs.js
CHANGED
|
@@ -275,6 +275,15 @@ const FormWizardStepWrapper = ({ stepKey, onCancel, description, title, componen
|
|
|
275
275
|
});
|
|
276
276
|
setSubmitHandlerInternal(() => wrappedCallbackFunction);
|
|
277
277
|
}, [setSubmitHandlerInternal]);
|
|
278
|
+
const isEmptyStep = sharedUtils.objectKeys(form.getValues()).length === 0;
|
|
279
|
+
const emptyStepSubmitHandler = react.useCallback(() => {
|
|
280
|
+
if (isLastStep) {
|
|
281
|
+
submitHandlerInternal === null || submitHandlerInternal === void 0 ? void 0 : submitHandlerInternal(fullFormState);
|
|
282
|
+
}
|
|
283
|
+
else if (nextStepPath) {
|
|
284
|
+
navigate({ to: basePath, params: { stepPath: nextStepPath } });
|
|
285
|
+
}
|
|
286
|
+
}, [basePath, fullFormState, isLastStep, navigate, nextStepPath, submitHandlerInternal]);
|
|
278
287
|
const stepSubmitHandler = react.useMemo(() => form.handleSubmit((data) => __awaiter(void 0, void 0, void 0, function* () {
|
|
279
288
|
//? This state is not updated for the submit handler below; therefore, there is duplication.
|
|
280
289
|
setFullFormState(prev => (Object.assign(Object.assign({}, prev), { [stepKey]: data })));
|
|
@@ -296,9 +305,23 @@ const FormWizardStepWrapper = ({ stepKey, onCancel, description, title, componen
|
|
|
296
305
|
stepKey,
|
|
297
306
|
submitHandlerInternal,
|
|
298
307
|
]);
|
|
308
|
+
const handleSubmit = react.useMemo(() => (isEmptyStep ? emptyStepSubmitHandler : stepSubmitHandler), [emptyStepSubmitHandler, isEmptyStep, stepSubmitHandler]);
|
|
309
|
+
react.useEffect(() => {
|
|
310
|
+
// submit on cmd/ctrl + enter, always
|
|
311
|
+
const handleKeyDown = (event) => {
|
|
312
|
+
if (event.key === "Enter" && (event.ctrlKey || event.metaKey)) {
|
|
313
|
+
event.preventDefault();
|
|
314
|
+
stepSubmitHandler();
|
|
315
|
+
}
|
|
316
|
+
};
|
|
317
|
+
document.addEventListener("keydown", handleKeyDown);
|
|
318
|
+
return () => {
|
|
319
|
+
document.removeEventListener("keydown", handleKeyDown);
|
|
320
|
+
};
|
|
321
|
+
}, [stepSubmitHandler]);
|
|
299
322
|
return (jsxRuntime.jsxs("form", { className: cvaFormWizardComponentStepForm(), noValidate: true, onSubmit: e => {
|
|
300
323
|
e.preventDefault();
|
|
301
|
-
|
|
324
|
+
handleSubmit();
|
|
302
325
|
}, ref: formRef, children: [jsxRuntime.jsx(FormWizardHeader, { title, description }), jsxRuntime.jsx("div", { className: cvaFormWizardComponentContainer({ className: containerClassName }), children: component({
|
|
303
326
|
form,
|
|
304
327
|
fullForm,
|
package/index.esm.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { jsxs, jsx } from 'react/jsx-runtime';
|
|
2
2
|
import { registerTranslations, useNamespaceTranslation } from '@trackunit/i18n-library-translation';
|
|
3
3
|
import { Link, useNavigate, useParams } from '@tanstack/react-router';
|
|
4
|
-
import { objectFromEntries, objectEntries,
|
|
4
|
+
import { objectKeys, objectFromEntries, objectEntries, objectValues } from '@trackunit/shared-utils';
|
|
5
5
|
import { useState, useRef, useEffect, useCallback, useMemo } from 'react';
|
|
6
6
|
import { cvaMerge } from '@trackunit/css-class-variance-utilities';
|
|
7
7
|
import { Icon, Button, Heading } from '@trackunit/react-components';
|
|
@@ -271,6 +271,15 @@ const FormWizardStepWrapper = ({ stepKey, onCancel, description, title, componen
|
|
|
271
271
|
});
|
|
272
272
|
setSubmitHandlerInternal(() => wrappedCallbackFunction);
|
|
273
273
|
}, [setSubmitHandlerInternal]);
|
|
274
|
+
const isEmptyStep = objectKeys(form.getValues()).length === 0;
|
|
275
|
+
const emptyStepSubmitHandler = useCallback(() => {
|
|
276
|
+
if (isLastStep) {
|
|
277
|
+
submitHandlerInternal === null || submitHandlerInternal === void 0 ? void 0 : submitHandlerInternal(fullFormState);
|
|
278
|
+
}
|
|
279
|
+
else if (nextStepPath) {
|
|
280
|
+
navigate({ to: basePath, params: { stepPath: nextStepPath } });
|
|
281
|
+
}
|
|
282
|
+
}, [basePath, fullFormState, isLastStep, navigate, nextStepPath, submitHandlerInternal]);
|
|
274
283
|
const stepSubmitHandler = useMemo(() => form.handleSubmit((data) => __awaiter(void 0, void 0, void 0, function* () {
|
|
275
284
|
//? This state is not updated for the submit handler below; therefore, there is duplication.
|
|
276
285
|
setFullFormState(prev => (Object.assign(Object.assign({}, prev), { [stepKey]: data })));
|
|
@@ -292,9 +301,23 @@ const FormWizardStepWrapper = ({ stepKey, onCancel, description, title, componen
|
|
|
292
301
|
stepKey,
|
|
293
302
|
submitHandlerInternal,
|
|
294
303
|
]);
|
|
304
|
+
const handleSubmit = useMemo(() => (isEmptyStep ? emptyStepSubmitHandler : stepSubmitHandler), [emptyStepSubmitHandler, isEmptyStep, stepSubmitHandler]);
|
|
305
|
+
useEffect(() => {
|
|
306
|
+
// submit on cmd/ctrl + enter, always
|
|
307
|
+
const handleKeyDown = (event) => {
|
|
308
|
+
if (event.key === "Enter" && (event.ctrlKey || event.metaKey)) {
|
|
309
|
+
event.preventDefault();
|
|
310
|
+
stepSubmitHandler();
|
|
311
|
+
}
|
|
312
|
+
};
|
|
313
|
+
document.addEventListener("keydown", handleKeyDown);
|
|
314
|
+
return () => {
|
|
315
|
+
document.removeEventListener("keydown", handleKeyDown);
|
|
316
|
+
};
|
|
317
|
+
}, [stepSubmitHandler]);
|
|
295
318
|
return (jsxs("form", { className: cvaFormWizardComponentStepForm(), noValidate: true, onSubmit: e => {
|
|
296
319
|
e.preventDefault();
|
|
297
|
-
|
|
320
|
+
handleSubmit();
|
|
298
321
|
}, ref: formRef, children: [jsx(FormWizardHeader, { title, description }), jsx("div", { className: cvaFormWizardComponentContainer({ className: containerClassName }), children: component({
|
|
299
322
|
form,
|
|
300
323
|
fullForm,
|