|
|
@@ -1,8 +1,10 @@
|
|
|
-import { reactive, computed, toRaw, isReactive, watch } from "vue";
|
|
|
+import { reactive, computed, toRaw, isReactive, nextTick, watch } from "vue";
|
|
|
import isEqual from "fast-deep-equal";
|
|
|
+import { useScroll } from "src/composables/useScroll";
|
|
|
|
|
|
-export const useFormUpdateTracker = (initialFormValue) => {
|
|
|
+export const useFormUpdateTracker = (initialFormValue, options = {}) => {
|
|
|
const form = reactive(deepClone(initialFormValue));
|
|
|
+ const { scrollToComponent } = useScroll();
|
|
|
let originalForm = deepClone(initialFormValue);
|
|
|
const updatedFields = reactive({});
|
|
|
|
|
|
@@ -50,6 +52,13 @@ export const useFormUpdateTracker = (initialFormValue) => {
|
|
|
return formData;
|
|
|
};
|
|
|
|
|
|
+ const onValidationError = async (invalidComponent) => {
|
|
|
+ if (!invalidComponent) return;
|
|
|
+ await nextTick();
|
|
|
+ invalidComponent.focus?.();
|
|
|
+ scrollToComponent(invalidComponent, options.containerRef);
|
|
|
+ };
|
|
|
+
|
|
|
return {
|
|
|
form,
|
|
|
getUpdatedFields,
|
|
|
@@ -58,6 +67,7 @@ export const useFormUpdateTracker = (initialFormValue) => {
|
|
|
setUpdateFormAsOriginal,
|
|
|
getFormAsFormData,
|
|
|
getUpdatedFieldsAsFormData,
|
|
|
+ onValidationError,
|
|
|
};
|
|
|
};
|
|
|
|