@globalbrain/sefirot 2.24.0 → 2.25.0

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,10 +1,7 @@
1
- import type { Ref } from 'vue'
2
- import { computed } from 'vue'
1
+ import { type Ref, computed, reactive, toRefs } from 'vue'
3
2
  import { useSnackbars } from '../stores/Snackbars'
4
- import type { UseDataInput } from './Data'
5
- import { useData } from './Data'
6
- import type { Validation } from './Validation'
7
- import { useValidation } from './Validation'
3
+ import { type UseDataInput, useData } from './Data'
4
+ import { type Validation, useValidation } from './Validation'
8
5
 
9
6
  export interface Form<T extends Record<string, any>> {
10
7
  data: T
@@ -15,25 +12,49 @@ export interface Form<T extends Record<string, any>> {
15
12
  validateAndNotify(): Promise<boolean>
16
13
  }
17
14
 
18
- export interface UseFormOptions<T extends Record<string, any>> {
19
- data: UseDataInput<T>
20
- rules?: Record<string, any> | ((state: T) => Record<string, any>)
15
+ export type ComputedData<T extends Record<string, () => any>> = {
16
+ [K in keyof T]: ReturnType<T[K]>
17
+ }
18
+
19
+ export type AllData<
20
+ D extends Record<string, any>,
21
+ C extends Record<string, () => any>
22
+ > = D & ComputedData<C>
23
+
24
+ export interface UseFormOptions<
25
+ D extends Record<string, any>,
26
+ C extends Record<string, () => any>
27
+ > {
28
+ data: UseDataInput<D>
29
+ computed?: (data: D) => C
30
+ rules?: Record<string, any> | ((data: AllData<D, C>) => Record<string, any>)
21
31
  }
22
32
 
23
33
  export function useForm<
24
- T extends Record<string, any>
25
- >(options: UseFormOptions<T>): Form<T> {
34
+ D extends Record<string, any> = Record<string, any>,
35
+ C extends Record<string, () => any> = Record<string, () => any>
36
+ >(options: UseFormOptions<D, C>): Form<AllData<D, C>> {
26
37
  const snackbars = useSnackbars()
27
38
 
28
39
  const data = useData(options.data)
40
+ const dataStateRef = toRefs(data.state)
41
+
42
+ const computedData = options.computed
43
+ ? createComputedData(options.computed(data.state))
44
+ : {}
45
+
46
+ const allData = reactive({
47
+ ...dataStateRef,
48
+ ...computedData
49
+ }) as AllData<D, C>
29
50
 
30
51
  const rules = computed(() => {
31
52
  return options.rules
32
- ? typeof options.rules === 'function' ? options.rules(data.state) : options.rules
53
+ ? typeof options.rules === 'function' ? options.rules(allData) : options.rules
33
54
  : {}
34
55
  })
35
56
 
36
- const validation = useValidation(data.state, rules)
57
+ const validation = useValidation(allData, rules)
37
58
 
38
59
  function init(): void {
39
60
  data.init()
@@ -62,7 +83,7 @@ export function useForm<
62
83
  }
63
84
 
64
85
  return {
65
- data: data.state,
86
+ data: allData,
66
87
  init,
67
88
  reset,
68
89
  validation,
@@ -70,3 +91,15 @@ export function useForm<
70
91
  validateAndNotify
71
92
  }
72
93
  }
94
+
95
+ function createComputedData<
96
+ C extends Record<string, () => any>
97
+ >(input: C): ComputedData<C> {
98
+ const computedData = {} as any
99
+
100
+ for (const [key, fn] of Object.entries(input)) {
101
+ computedData[key] = computed(() => fn())
102
+ }
103
+
104
+ return computedData
105
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@globalbrain/sefirot",
3
- "version": "2.24.0",
3
+ "version": "2.25.0",
4
4
  "packageManager": "pnpm@7.26.2",
5
5
  "description": "Vue Components for Global Brain Design System.",
6
6
  "author": "Kia Ishii <ka.ishii@globalbrains.com>",