@teamnovu/kit-vue-forms 0.1.19 → 0.1.21

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.
@@ -1,4 +1,3 @@
1
- import type { Awaitable } from "@vueuse/core";
2
1
  import {
3
2
  computed,
4
3
  reactive,
@@ -9,86 +8,88 @@ import {
9
8
  type MaybeRef,
10
9
  type MaybeRefOrGetter,
11
10
  type Ref,
12
- } from "vue";
13
- import type { Form, FormDataDefault } from "../types/form";
14
- import type { EntityPaths, PickEntity } from "../types/util";
15
- import type { ValidationStrategy } from "../types/validation";
16
- import { cloneRefValue } from "../utils/general";
17
- import { useFieldRegistry } from "./useFieldRegistry";
18
- import { useFormState } from "./useFormState";
19
- import { createSubformInterface, type SubformOptions } from "./useSubform";
20
- import { useValidation, type ValidationOptions } from "./useValidation";
21
- import { useSubmitHandler } from "./useSubmitHandler";
11
+ } from 'vue'
12
+ import type { Form, FormDataDefault } from '../types/form'
13
+ import type { EntityPaths, PickEntity } from '../types/util'
14
+ import type { ValidationStrategy } from '../types/validation'
15
+ import { cloneRefValue } from '../utils/general'
16
+ import { useFieldRegistry } from './useFieldRegistry'
17
+ import { useFormState } from './useFormState'
18
+ import { createSubformInterface, type SubformOptions } from './useSubform'
19
+ import { useSubmitHandler } from './useSubmitHandler'
20
+ import { useValidation, type ValidationOptions } from './useValidation'
22
21
 
23
22
  export interface UseFormOptions<T extends FormDataDefault>
24
23
  extends ValidationOptions<T> {
25
- initialData: MaybeRefOrGetter<T>;
26
- validationStrategy?: MaybeRef<ValidationStrategy>;
27
- keepValuesOnUnmount?: MaybeRef<boolean>;
24
+ initialData: MaybeRefOrGetter<T>
25
+ validationStrategy?: MaybeRef<ValidationStrategy>
26
+ keepValuesOnUnmount?: MaybeRef<boolean>
28
27
  }
29
28
 
30
- export function useForm<T extends FormDataDefault>(options: UseFormOptions<T>) {
31
- const initialData = computed(() => cloneRefValue(options.initialData));
29
+ export function useForm<T extends FormDataDefault>(
30
+ options: UseFormOptions<T>,
31
+ ): Form<T> {
32
+ const initialData = computed(() => cloneRefValue(options.initialData))
32
33
 
33
- const data = ref<T>(cloneRefValue(initialData)) as Ref<T>;
34
+ const data = ref<T>(cloneRefValue(initialData)) as Ref<T>
34
35
 
35
36
  const state = reactive({
36
37
  initialData,
37
38
  data,
38
- });
39
+ })
39
40
 
40
41
  watch(
41
42
  initialData,
42
43
  (newValue) => {
43
- state.data = cloneRefValue(newValue);
44
+ state.data = cloneRefValue(newValue)
44
45
  },
45
- { flush: "sync" },
46
- );
46
+ { flush: 'sync' },
47
+ )
47
48
 
48
- const validationState = useValidation(state, options);
49
+ const validationState = useValidation(state, options)
49
50
  const fieldRegistry = useFieldRegistry(state, validationState, {
50
51
  keepValuesOnUnmount: options.keepValuesOnUnmount,
51
52
  onBlur: async (path: string) => {
52
- if (unref(options.validationStrategy) === "onTouch") {
53
- validationState.validateField(path);
53
+ if (unref(options.validationStrategy) === 'onTouch') {
54
+ validationState.validateField(path)
54
55
  }
55
56
  },
56
- });
57
- const formState = useFormState(fieldRegistry);
57
+ })
58
+ const formState = useFormState(fieldRegistry)
58
59
 
59
60
  const reset = () => {
60
- data.value = cloneRefValue(initialData);
61
- validationState.reset();
61
+ data.value = cloneRefValue(initialData)
62
+ validationState.reset()
62
63
  for (const field of fieldRegistry.fields.value) {
63
- field.reset();
64
+ field.reset()
64
65
  }
65
- };
66
+ }
66
67
 
67
68
  function getSubForm<K extends EntityPaths<T>>(
68
69
  path: K,
69
70
  subformOptions?: SubformOptions<PickEntity<T, K>>,
70
71
  ): Form<PickEntity<T, K>> {
71
- return createSubformInterface(formInterface, path, options, subformOptions);
72
+ return createSubformInterface(formInterface, path, options, subformOptions)
72
73
  }
73
74
 
74
- const formInterface: Omit<Form<T>, "submitHandler"> = {
75
+ const formInterface: Omit<Form<T>, 'submitHandler'> = {
75
76
  ...fieldRegistry,
76
77
  ...validationState,
77
78
  ...formState,
78
79
  reset,
79
80
  getSubForm,
80
- initialData: toRef(state, "initialData") as Form<T>["initialData"],
81
- data: toRef(state, "data") as Form<T>["data"],
82
- };
81
+ initialData: toRef(state, 'initialData') as Form<T>['initialData'],
82
+ data: toRef(state, 'data') as Form<T>['data'],
83
+ }
83
84
 
84
- const submitHandler = useSubmitHandler(formInterface, options);
85
+ const submitHandler = useSubmitHandler(formInterface, options)
85
86
 
86
- if (unref(options.validationStrategy) === "onFormOpen") {
87
- validationState.validateForm();
87
+ if (unref(options.validationStrategy) === 'onFormOpen') {
88
+ validationState.validateForm()
88
89
  }
89
90
 
90
91
  return {
91
92
  ...formInterface,
92
93
  submitHandler,
93
- };
94
+ }
94
95
  }