@teamnovu/kit-vue-forms 0.1.20 → 0.1.22
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/composables/useFieldArray.d.ts +13 -0
- package/dist/composables/useFieldRegistry.d.ts +1 -1
- package/dist/composables/useSubform.d.ts +1 -1
- package/dist/index.js +406 -346
- package/dist/types/form.d.ts +16 -0
- package/dist/{composables/useSubmitHandler.d.ts → utils/submitHandler.d.ts} +1 -1
- package/package.json +1 -1
- package/src/composables/useField.ts +3 -1
- package/src/composables/useFieldArray.ts +144 -0
- package/src/composables/useFieldRegistry.ts +60 -60
- package/src/composables/useForm.ts +41 -47
- package/src/composables/useSubform.ts +66 -70
- package/src/types/form.ts +26 -0
- package/src/{composables/useSubmitHandler.ts → utils/submitHandler.ts} +2 -2
- package/tests/useFieldArray.test.ts +381 -0
- package/tests/useForm.test.ts +286 -257
|
@@ -1,47 +1,47 @@
|
|
|
1
|
-
import { computed, isRef, unref, type Ref } from
|
|
1
|
+
import { computed, isRef, unref, type Ref } from 'vue'
|
|
2
2
|
import type {
|
|
3
3
|
FieldsTuple,
|
|
4
4
|
Form,
|
|
5
5
|
FormDataDefault,
|
|
6
6
|
FormField,
|
|
7
|
-
} from
|
|
8
|
-
import type { EntityPaths, Paths, PickEntity, PickProps } from
|
|
9
|
-
import type { ValidationResult, Validator } from
|
|
7
|
+
} from '../types/form'
|
|
8
|
+
import type { EntityPaths, Paths, PickEntity, PickProps } from '../types/util'
|
|
9
|
+
import type { ValidationResult, Validator } from '../types/validation'
|
|
10
10
|
import {
|
|
11
11
|
filterErrorsForPath,
|
|
12
12
|
getLens,
|
|
13
13
|
getNestedValue,
|
|
14
14
|
joinPath,
|
|
15
|
-
} from
|
|
16
|
-
import
|
|
17
|
-
import
|
|
18
|
-
import {
|
|
15
|
+
} from '../utils/path'
|
|
16
|
+
import { makeSubmitHandler } from '../utils/submitHandler'
|
|
17
|
+
import { useFieldArray } from './useFieldArray'
|
|
18
|
+
import type { DefineFieldOptions } from './useFieldRegistry'
|
|
19
|
+
import type { UseFormOptions } from './useForm'
|
|
19
20
|
import {
|
|
20
21
|
createValidator,
|
|
21
22
|
SuccessValidationResult,
|
|
22
23
|
type ValidatorOptions,
|
|
23
|
-
} from
|
|
24
|
+
} from './useValidation'
|
|
24
25
|
|
|
25
26
|
export interface SubformOptions<_T extends FormDataDefault> {
|
|
26
27
|
// Additional subform-specific options can be added here
|
|
27
28
|
}
|
|
28
29
|
|
|
29
30
|
class NestedValidator<T extends FormDataDefault, P extends Paths<T>>
|
|
30
|
-
|
|
31
|
-
{
|
|
31
|
+
implements Validator<T> {
|
|
32
32
|
constructor(
|
|
33
33
|
private path: P,
|
|
34
34
|
private validator: Validator<PickEntity<T, P>> | undefined,
|
|
35
35
|
) {}
|
|
36
36
|
|
|
37
37
|
async validate(data: T): Promise<ValidationResult> {
|
|
38
|
-
const subFormData = getNestedValue(data, this.path) as PickEntity<T, P
|
|
38
|
+
const subFormData = getNestedValue(data, this.path) as PickEntity<T, P>
|
|
39
39
|
|
|
40
40
|
if (!this.validator) {
|
|
41
|
-
return SuccessValidationResult
|
|
41
|
+
return SuccessValidationResult
|
|
42
42
|
}
|
|
43
43
|
|
|
44
|
-
const validationResult = await this.validator.validate(subFormData)
|
|
44
|
+
const validationResult = await this.validator.validate(subFormData)
|
|
45
45
|
|
|
46
46
|
return {
|
|
47
47
|
isValid: validationResult.isValid,
|
|
@@ -55,7 +55,7 @@ class NestedValidator<T extends FormDataDefault, P extends Paths<T>>
|
|
|
55
55
|
)
|
|
56
56
|
: {},
|
|
57
57
|
},
|
|
58
|
-
}
|
|
58
|
+
}
|
|
59
59
|
}
|
|
60
60
|
}
|
|
61
61
|
|
|
@@ -63,22 +63,22 @@ export function createSubformInterface<
|
|
|
63
63
|
T extends FormDataDefault,
|
|
64
64
|
K extends EntityPaths<T>,
|
|
65
65
|
>(
|
|
66
|
-
mainForm:
|
|
66
|
+
mainForm: Form<T>,
|
|
67
67
|
path: K,
|
|
68
68
|
formOptions?: UseFormOptions<T>,
|
|
69
69
|
_options?: SubformOptions<PickEntity<T, K>>,
|
|
70
70
|
): Form<PickEntity<T, K>> {
|
|
71
|
-
type ST = PickEntity<T, K
|
|
72
|
-
type SP = Paths<ST
|
|
73
|
-
type MP<P extends SP> = `${K}.${P}
|
|
74
|
-
type ScopedMainPaths = Paths<T> & MP<SP
|
|
71
|
+
type ST = PickEntity<T, K>
|
|
72
|
+
type SP = Paths<ST>
|
|
73
|
+
type MP<P extends SP> = `${K}.${P}`
|
|
74
|
+
type ScopedMainPaths = Paths<T> & MP<SP>
|
|
75
75
|
|
|
76
76
|
// Create reactive data scoped to subform path
|
|
77
|
-
const data = getLens(mainForm.data, path) as Ref<ST
|
|
77
|
+
const data = getLens(mainForm.data, path) as Ref<ST>
|
|
78
78
|
|
|
79
79
|
const initialData = computed(() => {
|
|
80
|
-
return getNestedValue(mainForm.initialData.value, path) as ST
|
|
81
|
-
})
|
|
80
|
+
return getNestedValue(mainForm.initialData.value, path) as ST
|
|
81
|
+
})
|
|
82
82
|
|
|
83
83
|
const adaptMainFormField = <S extends SP>(
|
|
84
84
|
field: FormField<PickProps<T, ScopedMainPaths>, ScopedMainPaths>,
|
|
@@ -86,37 +86,37 @@ export function createSubformInterface<
|
|
|
86
86
|
// Where P ist the full path in the main form, we need to adapt it to the subform's path
|
|
87
87
|
return {
|
|
88
88
|
...field,
|
|
89
|
-
path: computed(() => unref(field.path).replace(path +
|
|
89
|
+
path: computed(() => unref(field.path).replace(path + '.', '')),
|
|
90
90
|
setData: (newData: PickProps<ST, S>) => {
|
|
91
|
-
field.setData(newData as PickProps<T, ScopedMainPaths>)
|
|
91
|
+
field.setData(newData as PickProps<T, ScopedMainPaths>)
|
|
92
92
|
},
|
|
93
|
-
} as unknown as FormField<PickProps<ST, S>, S
|
|
94
|
-
}
|
|
93
|
+
} as unknown as FormField<PickProps<ST, S>, S>
|
|
94
|
+
}
|
|
95
95
|
|
|
96
96
|
const getField = <P extends SP>(fieldPath: P) => {
|
|
97
|
-
const fullPath = joinPath(path, fieldPath)
|
|
98
|
-
const mainFormField = mainForm.getField(fullPath as ScopedMainPaths)
|
|
97
|
+
const fullPath = joinPath(path, fieldPath)
|
|
98
|
+
const mainFormField = mainForm.getField(fullPath as ScopedMainPaths)
|
|
99
99
|
|
|
100
100
|
if (!mainFormField) {
|
|
101
|
-
return {} as FormField<PickProps<ST, P>, P
|
|
101
|
+
return {} as FormField<PickProps<ST, P>, P>
|
|
102
102
|
}
|
|
103
103
|
|
|
104
|
-
return adaptMainFormField<P>(mainFormField)
|
|
105
|
-
}
|
|
104
|
+
return adaptMainFormField<P>(mainFormField)
|
|
105
|
+
}
|
|
106
106
|
|
|
107
107
|
// Field operations with path transformation
|
|
108
108
|
const defineField = <P extends SP>(
|
|
109
109
|
fieldOptions: DefineFieldOptions<ST, P>,
|
|
110
110
|
) => {
|
|
111
|
-
const fullPath = joinPath(path, fieldOptions.path)
|
|
111
|
+
const fullPath = joinPath(path, fieldOptions.path)
|
|
112
112
|
|
|
113
113
|
const mainField = mainForm.defineField({
|
|
114
114
|
...fieldOptions,
|
|
115
115
|
path: fullPath as ScopedMainPaths,
|
|
116
|
-
})
|
|
116
|
+
})
|
|
117
117
|
|
|
118
|
-
return adaptMainFormField<P>(mainField)
|
|
119
|
-
}
|
|
118
|
+
return adaptMainFormField<P>(mainField)
|
|
119
|
+
}
|
|
120
120
|
|
|
121
121
|
const fields = computed(<P extends SP>() => {
|
|
122
122
|
return (
|
|
@@ -126,11 +126,11 @@ export function createSubformInterface<
|
|
|
126
126
|
>[]
|
|
127
127
|
)
|
|
128
128
|
.filter((field) => {
|
|
129
|
-
const fieldPath = field.path.value
|
|
130
|
-
return fieldPath.startsWith(path +
|
|
129
|
+
const fieldPath = field.path.value
|
|
130
|
+
return fieldPath.startsWith(path + '.') || fieldPath === path
|
|
131
131
|
})
|
|
132
|
-
.map(
|
|
133
|
-
})
|
|
132
|
+
.map(field => adaptMainFormField(field)) as FieldsTuple<ST, P>
|
|
133
|
+
})
|
|
134
134
|
|
|
135
135
|
// Helper function to get all fields without type parameter
|
|
136
136
|
const getAllSubformFields = () => {
|
|
@@ -140,57 +140,54 @@ export function createSubformInterface<
|
|
|
140
140
|
ScopedMainPaths
|
|
141
141
|
>[]
|
|
142
142
|
).filter((field) => {
|
|
143
|
-
const fieldPath = field.path.value
|
|
144
|
-
return fieldPath.startsWith(path +
|
|
145
|
-
})
|
|
146
|
-
}
|
|
143
|
+
const fieldPath = field.path.value
|
|
144
|
+
return fieldPath.startsWith(path + '.') || fieldPath === path
|
|
145
|
+
})
|
|
146
|
+
}
|
|
147
147
|
|
|
148
148
|
// State computed from main form with path filtering
|
|
149
149
|
const isDirty = computed(() =>
|
|
150
|
-
getAllSubformFields().some(
|
|
151
|
-
);
|
|
150
|
+
getAllSubformFields().some(field => field.dirty.value))
|
|
152
151
|
const isTouched = computed(() =>
|
|
153
|
-
getAllSubformFields().some(
|
|
154
|
-
);
|
|
152
|
+
getAllSubformFields().some(field => field.touched.value))
|
|
155
153
|
|
|
156
154
|
// Validation delegates to main form
|
|
157
|
-
const isValid = computed(() => mainForm.isValid.value)
|
|
158
|
-
const isValidated = computed(() => mainForm.isValidated.value)
|
|
155
|
+
const isValid = computed(() => mainForm.isValid.value)
|
|
156
|
+
const isValidated = computed(() => mainForm.isValidated.value)
|
|
159
157
|
const errors = computed(() =>
|
|
160
|
-
filterErrorsForPath(unref(mainForm.errors), path)
|
|
161
|
-
);
|
|
158
|
+
filterErrorsForPath(unref(mainForm.errors), path))
|
|
162
159
|
|
|
163
|
-
const validateForm = () => mainForm.validateForm()
|
|
160
|
+
const validateForm = () => mainForm.validateForm()
|
|
164
161
|
|
|
165
162
|
// Nested subforms
|
|
166
163
|
const getSubForm = <P extends EntityPaths<ST>>(
|
|
167
164
|
subPath: P,
|
|
168
165
|
subOptions?: SubformOptions<PickEntity<ST, P>>,
|
|
169
166
|
) => {
|
|
170
|
-
const fullPath = joinPath(path, subPath) as EntityPaths<T
|
|
167
|
+
const fullPath = joinPath(path, subPath) as EntityPaths<T>
|
|
171
168
|
return mainForm.getSubForm(
|
|
172
169
|
fullPath,
|
|
173
170
|
subOptions as SubformOptions<PickEntity<T, typeof fullPath>>,
|
|
174
|
-
) as Form<PickEntity<ST, P
|
|
175
|
-
}
|
|
171
|
+
) as Form<PickEntity<ST, P>>
|
|
172
|
+
}
|
|
176
173
|
|
|
177
174
|
// Reset scoped to this subform
|
|
178
|
-
const reset = () => getAllSubformFields().forEach(
|
|
175
|
+
const reset = () => getAllSubformFields().forEach(field => field.reset())
|
|
179
176
|
|
|
180
177
|
const defineValidator = (
|
|
181
178
|
options: ValidatorOptions<ST> | Ref<Validator<ST>>,
|
|
182
179
|
) => {
|
|
183
|
-
const subValidator = isRef(options) ? options : createValidator(options)
|
|
180
|
+
const subValidator = isRef(options) ? options : createValidator(options)
|
|
184
181
|
const validator = computed(
|
|
185
182
|
() => new NestedValidator<T, K>(path, unref(subValidator)),
|
|
186
|
-
)
|
|
183
|
+
)
|
|
187
184
|
|
|
188
|
-
mainForm.defineValidator(validator)
|
|
185
|
+
mainForm.defineValidator(validator)
|
|
189
186
|
|
|
190
|
-
return subValidator
|
|
191
|
-
}
|
|
187
|
+
return subValidator
|
|
188
|
+
}
|
|
192
189
|
|
|
193
|
-
const
|
|
190
|
+
const subForm: Form<ST> = {
|
|
194
191
|
data: data,
|
|
195
192
|
fields,
|
|
196
193
|
initialData,
|
|
@@ -205,12 +202,11 @@ export function createSubformInterface<
|
|
|
205
202
|
reset,
|
|
206
203
|
validateForm,
|
|
207
204
|
getSubForm,
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
205
|
+
submitHandler: onSubmit => makeSubmitHandler(subForm, formOptions ?? {})(onSubmit),
|
|
206
|
+
useFieldArray: (fieldArrayPath, fieldArrayOptions) => {
|
|
207
|
+
return useFieldArray(subForm, fieldArrayPath, fieldArrayOptions)
|
|
208
|
+
},
|
|
209
|
+
}
|
|
211
210
|
|
|
212
|
-
return
|
|
213
|
-
...subFormInterface,
|
|
214
|
-
submitHandler,
|
|
215
|
-
};
|
|
211
|
+
return subForm
|
|
216
212
|
}
|
package/src/types/form.ts
CHANGED
|
@@ -14,6 +14,24 @@ import type {
|
|
|
14
14
|
|
|
15
15
|
export type FormDataDefault = object
|
|
16
16
|
|
|
17
|
+
export type HashFn<H, I> = (item: I) => H
|
|
18
|
+
|
|
19
|
+
export interface FieldArrayOptions<Item> {
|
|
20
|
+
hashFn?: HashFn<unknown, Item>
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export interface FieldArray<Item> {
|
|
24
|
+
fields: Ref<{
|
|
25
|
+
id: string
|
|
26
|
+
item: Item
|
|
27
|
+
}[]>
|
|
28
|
+
push: (item: Item) => void
|
|
29
|
+
remove: (item: Item) => void
|
|
30
|
+
removeByIndex: (index: number) => void
|
|
31
|
+
errors: Ref<ValidationErrors>
|
|
32
|
+
dirty: Ref<boolean>
|
|
33
|
+
}
|
|
34
|
+
|
|
17
35
|
export interface FormField<T, P extends string> {
|
|
18
36
|
data: Ref<T>
|
|
19
37
|
path: Ref<P>
|
|
@@ -81,4 +99,12 @@ export interface Form<T extends FormDataDefault> {
|
|
|
81
99
|
path: P,
|
|
82
100
|
options?: SubformOptions<PickEntity<T, P>>,
|
|
83
101
|
) => Form<PickEntity<T, P>>
|
|
102
|
+
|
|
103
|
+
// Field arrays
|
|
104
|
+
useFieldArray: <K extends Paths<T>>(
|
|
105
|
+
path: PickProps<T, K> extends unknown[] ? K : never,
|
|
106
|
+
options?: FieldArrayOptions<
|
|
107
|
+
PickProps<T, K> extends (infer U)[] ? U : never
|
|
108
|
+
>,
|
|
109
|
+
) => FieldArray<PickProps<T, K> extends (infer U)[] ? U : never>
|
|
84
110
|
}
|
|
@@ -7,8 +7,8 @@ interface SubmitHandlerOptions {
|
|
|
7
7
|
validationStrategy?: MaybeRef<ValidationStrategy>
|
|
8
8
|
}
|
|
9
9
|
|
|
10
|
-
export function
|
|
11
|
-
form:
|
|
10
|
+
export function makeSubmitHandler<T extends FormDataDefault>(
|
|
11
|
+
form: Form<T>,
|
|
12
12
|
options: SubmitHandlerOptions,
|
|
13
13
|
) {
|
|
14
14
|
return (onSubmit: (data: T) => Awaitable<void>) => {
|
|
@@ -0,0 +1,381 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest'
|
|
2
|
+
import { nextTick } from 'vue'
|
|
3
|
+
import { useForm } from '../src/composables/useForm'
|
|
4
|
+
import { HashStore, useFieldArray } from '../src/composables/useFieldArray'
|
|
5
|
+
|
|
6
|
+
describe('useFieldArray', () => {
|
|
7
|
+
describe('HashStore', () => {
|
|
8
|
+
it('should store and retrieve primitive values with identity hash', () => {
|
|
9
|
+
const store = new HashStore<string[]>()
|
|
10
|
+
|
|
11
|
+
store.set(5, ['id-1'])
|
|
12
|
+
expect(store.has(5)).toBe(true)
|
|
13
|
+
expect(store.get(5)).toEqual(['id-1'])
|
|
14
|
+
|
|
15
|
+
store.set(10, ['id-2'])
|
|
16
|
+
expect(store.get(10)).toEqual(['id-2'])
|
|
17
|
+
expect(store.get(5)).toEqual(['id-1'])
|
|
18
|
+
})
|
|
19
|
+
|
|
20
|
+
it('should store objects by reference with identity hash', () => {
|
|
21
|
+
const store = new HashStore<string[]>()
|
|
22
|
+
const obj1 = {
|
|
23
|
+
id: 1,
|
|
24
|
+
name: 'A',
|
|
25
|
+
}
|
|
26
|
+
const obj2 = {
|
|
27
|
+
id: 1,
|
|
28
|
+
name: 'A',
|
|
29
|
+
} // Same content, different reference
|
|
30
|
+
|
|
31
|
+
store.set(obj1, ['uuid-1'])
|
|
32
|
+
|
|
33
|
+
expect(store.has(obj1)).toBe(true)
|
|
34
|
+
expect(store.has(obj2)).toBe(false)
|
|
35
|
+
expect(store.get(obj1)).toEqual(['uuid-1'])
|
|
36
|
+
expect(store.get(obj2)).toBeUndefined()
|
|
37
|
+
})
|
|
38
|
+
|
|
39
|
+
it('should use custom hash function for semantic equality', () => {
|
|
40
|
+
const store = new HashStore<string[], { id: number }>(item => item.id)
|
|
41
|
+
const obj1 = {
|
|
42
|
+
id: 1,
|
|
43
|
+
name: 'A',
|
|
44
|
+
}
|
|
45
|
+
const obj2 = {
|
|
46
|
+
id: 1,
|
|
47
|
+
name: 'B',
|
|
48
|
+
} // Same ID, different name
|
|
49
|
+
const obj3 = {
|
|
50
|
+
id: 2,
|
|
51
|
+
name: 'A',
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
store.set(obj1, ['uuid-1'])
|
|
55
|
+
|
|
56
|
+
expect(store.has(obj2)).toBe(true)
|
|
57
|
+
expect(store.get(obj2)).toEqual(['uuid-1'])
|
|
58
|
+
expect(store.has(obj3)).toBe(false)
|
|
59
|
+
})
|
|
60
|
+
|
|
61
|
+
it('should overwrite value on subsequent set calls', () => {
|
|
62
|
+
const store = new HashStore<string[]>()
|
|
63
|
+
const item = { id: 1 }
|
|
64
|
+
|
|
65
|
+
store.set(item, ['id-1'])
|
|
66
|
+
expect(store.get(item)).toEqual(['id-1'])
|
|
67
|
+
|
|
68
|
+
store.set(item, ['id-1', 'id-2'])
|
|
69
|
+
expect(store.get(item)).toEqual(['id-1', 'id-2'])
|
|
70
|
+
})
|
|
71
|
+
})
|
|
72
|
+
|
|
73
|
+
describe('Core Functionality', () => {
|
|
74
|
+
it('should initialize with array data and generate IDs', () => {
|
|
75
|
+
const form = useForm({
|
|
76
|
+
initialData: {
|
|
77
|
+
items: [
|
|
78
|
+
{
|
|
79
|
+
id: 1,
|
|
80
|
+
name: 'Item 1',
|
|
81
|
+
},
|
|
82
|
+
{
|
|
83
|
+
id: 2,
|
|
84
|
+
name: 'Item 2',
|
|
85
|
+
},
|
|
86
|
+
],
|
|
87
|
+
},
|
|
88
|
+
})
|
|
89
|
+
const fieldArray = useFieldArray(form, 'items')
|
|
90
|
+
|
|
91
|
+
expect(fieldArray.fields.value).toHaveLength(2)
|
|
92
|
+
expect(fieldArray.fields.value[0]).toHaveProperty('id')
|
|
93
|
+
expect(fieldArray.fields.value[0]).toHaveProperty('item')
|
|
94
|
+
expect(fieldArray.fields.value[0].item).toEqual({
|
|
95
|
+
id: 1,
|
|
96
|
+
name: 'Item 1',
|
|
97
|
+
})
|
|
98
|
+
expect(typeof fieldArray.fields.value[0].id).toBe('string')
|
|
99
|
+
expect(fieldArray.errors.value).toEqual([])
|
|
100
|
+
expect(fieldArray.dirty.value).toBe(false)
|
|
101
|
+
})
|
|
102
|
+
|
|
103
|
+
it('should push new items to the array', async () => {
|
|
104
|
+
const form = useForm({
|
|
105
|
+
initialData: {
|
|
106
|
+
items: [
|
|
107
|
+
{
|
|
108
|
+
id: 1,
|
|
109
|
+
name: 'First',
|
|
110
|
+
},
|
|
111
|
+
],
|
|
112
|
+
},
|
|
113
|
+
})
|
|
114
|
+
const fieldArray = useFieldArray(form, 'items')
|
|
115
|
+
|
|
116
|
+
expect(fieldArray.fields.value).toHaveLength(1)
|
|
117
|
+
|
|
118
|
+
fieldArray.push({
|
|
119
|
+
id: 2,
|
|
120
|
+
name: 'Second',
|
|
121
|
+
})
|
|
122
|
+
await nextTick()
|
|
123
|
+
|
|
124
|
+
expect(fieldArray.fields.value).toHaveLength(2)
|
|
125
|
+
expect(fieldArray.fields.value[1].item).toEqual({
|
|
126
|
+
id: 2,
|
|
127
|
+
name: 'Second',
|
|
128
|
+
})
|
|
129
|
+
expect(fieldArray.dirty.value).toBe(true)
|
|
130
|
+
})
|
|
131
|
+
|
|
132
|
+
it('should remove item by reference', async () => {
|
|
133
|
+
const form = useForm({
|
|
134
|
+
initialData: {
|
|
135
|
+
items: [
|
|
136
|
+
{
|
|
137
|
+
id: 1,
|
|
138
|
+
name: 'Keep',
|
|
139
|
+
},
|
|
140
|
+
{
|
|
141
|
+
id: 2,
|
|
142
|
+
name: 'Remove',
|
|
143
|
+
},
|
|
144
|
+
],
|
|
145
|
+
},
|
|
146
|
+
})
|
|
147
|
+
const fieldArray = useFieldArray(form, 'items')
|
|
148
|
+
const itemToRemove = fieldArray.fields.value[1].item
|
|
149
|
+
|
|
150
|
+
fieldArray.remove(itemToRemove)
|
|
151
|
+
await nextTick()
|
|
152
|
+
|
|
153
|
+
expect(fieldArray.fields.value).toHaveLength(1)
|
|
154
|
+
expect(fieldArray.fields.value[0].item.name).toBe('Keep')
|
|
155
|
+
expect(fieldArray.dirty.value).toBe(true)
|
|
156
|
+
})
|
|
157
|
+
|
|
158
|
+
it('should remove item by index', async () => {
|
|
159
|
+
const form = useForm({
|
|
160
|
+
initialData: {
|
|
161
|
+
items: [
|
|
162
|
+
{
|
|
163
|
+
id: 1,
|
|
164
|
+
name: 'First',
|
|
165
|
+
},
|
|
166
|
+
{
|
|
167
|
+
id: 2,
|
|
168
|
+
name: 'Second',
|
|
169
|
+
},
|
|
170
|
+
{
|
|
171
|
+
id: 3,
|
|
172
|
+
name: 'Third',
|
|
173
|
+
},
|
|
174
|
+
],
|
|
175
|
+
},
|
|
176
|
+
})
|
|
177
|
+
const fieldArray = useFieldArray(form, 'items')
|
|
178
|
+
|
|
179
|
+
fieldArray.removeByIndex(1)
|
|
180
|
+
await nextTick()
|
|
181
|
+
|
|
182
|
+
expect(fieldArray.fields.value).toHaveLength(2)
|
|
183
|
+
expect(fieldArray.fields.value[0].item.name).toBe('First')
|
|
184
|
+
expect(fieldArray.fields.value[1].item.name).toBe('Third')
|
|
185
|
+
})
|
|
186
|
+
})
|
|
187
|
+
|
|
188
|
+
describe('ID Persistence', () => {
|
|
189
|
+
it('should maintain IDs when items are reordered', async () => {
|
|
190
|
+
const form = useForm({
|
|
191
|
+
initialData: {
|
|
192
|
+
items: [
|
|
193
|
+
{
|
|
194
|
+
id: 1,
|
|
195
|
+
name: 'First',
|
|
196
|
+
},
|
|
197
|
+
{
|
|
198
|
+
id: 2,
|
|
199
|
+
name: 'Second',
|
|
200
|
+
},
|
|
201
|
+
{
|
|
202
|
+
id: 3,
|
|
203
|
+
name: 'Third',
|
|
204
|
+
},
|
|
205
|
+
],
|
|
206
|
+
},
|
|
207
|
+
})
|
|
208
|
+
const fieldArray = useFieldArray(form, 'items', {
|
|
209
|
+
hashFn: (item: { id: number }) => item.id,
|
|
210
|
+
})
|
|
211
|
+
|
|
212
|
+
// Capture initial IDs
|
|
213
|
+
const id1 = fieldArray.fields.value[0].id
|
|
214
|
+
const id2 = fieldArray.fields.value[1].id
|
|
215
|
+
const id3 = fieldArray.fields.value[2].id
|
|
216
|
+
|
|
217
|
+
// Reverse the array
|
|
218
|
+
const arrayField = form.getField('items')
|
|
219
|
+
arrayField.setData([...arrayField.data.value].reverse())
|
|
220
|
+
await nextTick()
|
|
221
|
+
|
|
222
|
+
// Verify order changed but IDs followed the items
|
|
223
|
+
expect(fieldArray.fields.value[0].item.name).toBe('Third')
|
|
224
|
+
expect(fieldArray.fields.value[0].id).toBe(id3)
|
|
225
|
+
expect(fieldArray.fields.value[1].item.name).toBe('Second')
|
|
226
|
+
expect(fieldArray.fields.value[1].id).toBe(id2)
|
|
227
|
+
expect(fieldArray.fields.value[2].item.name).toBe('First')
|
|
228
|
+
expect(fieldArray.fields.value[2].id).toBe(id1)
|
|
229
|
+
})
|
|
230
|
+
|
|
231
|
+
it('should maintain IDs based on custom hash function', async () => {
|
|
232
|
+
const form = useForm({
|
|
233
|
+
initialData: {
|
|
234
|
+
products: [
|
|
235
|
+
{
|
|
236
|
+
sku: 'ABC',
|
|
237
|
+
name: 'Widget',
|
|
238
|
+
price: 10,
|
|
239
|
+
},
|
|
240
|
+
{
|
|
241
|
+
sku: 'DEF',
|
|
242
|
+
name: 'Gadget',
|
|
243
|
+
price: 20,
|
|
244
|
+
},
|
|
245
|
+
],
|
|
246
|
+
},
|
|
247
|
+
})
|
|
248
|
+
const fieldArray = useFieldArray(form, 'products', {
|
|
249
|
+
hashFn: (item: { sku: string }) => item.sku,
|
|
250
|
+
})
|
|
251
|
+
|
|
252
|
+
const idABC = fieldArray.fields.value[0].id
|
|
253
|
+
const idDEF = fieldArray.fields.value[1].id
|
|
254
|
+
|
|
255
|
+
// Update price and swap order
|
|
256
|
+
const arrayField = form.getField('products')
|
|
257
|
+
arrayField.setData([
|
|
258
|
+
{
|
|
259
|
+
sku: 'DEF',
|
|
260
|
+
name: 'Gadget',
|
|
261
|
+
price: 25,
|
|
262
|
+
},
|
|
263
|
+
{
|
|
264
|
+
sku: 'ABC',
|
|
265
|
+
name: 'Widget',
|
|
266
|
+
price: 15,
|
|
267
|
+
},
|
|
268
|
+
])
|
|
269
|
+
await nextTick()
|
|
270
|
+
|
|
271
|
+
// IDs should persist because SKUs didn't change
|
|
272
|
+
expect(fieldArray.fields.value[0].id).toBe(idDEF)
|
|
273
|
+
expect(fieldArray.fields.value[1].id).toBe(idABC)
|
|
274
|
+
|
|
275
|
+
// Change SKU - should get new ID
|
|
276
|
+
arrayField.setData([
|
|
277
|
+
{
|
|
278
|
+
sku: 'XYZ',
|
|
279
|
+
name: 'Widget',
|
|
280
|
+
price: 15,
|
|
281
|
+
},
|
|
282
|
+
{
|
|
283
|
+
sku: 'DEF',
|
|
284
|
+
name: 'Gadget',
|
|
285
|
+
price: 25,
|
|
286
|
+
},
|
|
287
|
+
])
|
|
288
|
+
await nextTick()
|
|
289
|
+
|
|
290
|
+
expect(fieldArray.fields.value[0].id).not.toBe(idABC)
|
|
291
|
+
expect(fieldArray.fields.value[1].id).toBe(idDEF)
|
|
292
|
+
})
|
|
293
|
+
|
|
294
|
+
it('should assign IDs in order for items with same hash', async () => {
|
|
295
|
+
// When multiple items share the same hash, IDs are assigned in order
|
|
296
|
+
// from the stored ID list. This means reordering items with same hash
|
|
297
|
+
// will NOT preserve ID-to-item mapping (IDs follow position, not identity).
|
|
298
|
+
const form = useForm({
|
|
299
|
+
initialData: {
|
|
300
|
+
items: [
|
|
301
|
+
{
|
|
302
|
+
type: 'tag',
|
|
303
|
+
value: 'vue',
|
|
304
|
+
},
|
|
305
|
+
{
|
|
306
|
+
type: 'tag',
|
|
307
|
+
value: 'react',
|
|
308
|
+
},
|
|
309
|
+
{
|
|
310
|
+
type: 'tag',
|
|
311
|
+
value: 'svelte',
|
|
312
|
+
},
|
|
313
|
+
],
|
|
314
|
+
},
|
|
315
|
+
})
|
|
316
|
+
// Hash by type only - all items have same hash 'tag'
|
|
317
|
+
const fieldArray = useFieldArray(form, 'items', {
|
|
318
|
+
hashFn: (item: { type: string }) => item.type,
|
|
319
|
+
})
|
|
320
|
+
|
|
321
|
+
// All items get unique IDs despite same hash
|
|
322
|
+
const [id1, id2, id3] = fieldArray.fields.value.map(f => f.id)
|
|
323
|
+
expect(new Set([id1, id2, id3]).size).toBe(3)
|
|
324
|
+
|
|
325
|
+
// Reorder: move last item to first position
|
|
326
|
+
const arrayField = form.getField('items')
|
|
327
|
+
arrayField.setData([
|
|
328
|
+
{
|
|
329
|
+
type: 'tag',
|
|
330
|
+
value: 'svelte',
|
|
331
|
+
},
|
|
332
|
+
{
|
|
333
|
+
type: 'tag',
|
|
334
|
+
value: 'vue',
|
|
335
|
+
},
|
|
336
|
+
{
|
|
337
|
+
type: 'tag',
|
|
338
|
+
value: 'react',
|
|
339
|
+
},
|
|
340
|
+
])
|
|
341
|
+
await nextTick()
|
|
342
|
+
|
|
343
|
+
// IDs are assigned in order from stored list, NOT following items
|
|
344
|
+
// This is expected behavior with hash collisions
|
|
345
|
+
expect(fieldArray.fields.value[0].id).toBe(id1) // svelte gets id1 (was vue's)
|
|
346
|
+
expect(fieldArray.fields.value[1].id).toBe(id2) // vue gets id2 (was react's)
|
|
347
|
+
expect(fieldArray.fields.value[2].id).toBe(id3) // react gets id3 (was svelte's)
|
|
348
|
+
|
|
349
|
+
// The values confirm the reorder happened
|
|
350
|
+
expect(fieldArray.fields.value[0].item.value).toBe('svelte')
|
|
351
|
+
expect(fieldArray.fields.value[1].item.value).toBe('vue')
|
|
352
|
+
expect(fieldArray.fields.value[2].item.value).toBe('react')
|
|
353
|
+
})
|
|
354
|
+
})
|
|
355
|
+
|
|
356
|
+
describe('Form Integration', () => {
|
|
357
|
+
it('should track dirty state correctly', async () => {
|
|
358
|
+
const form = useForm({
|
|
359
|
+
initialData: { items: [{ name: 'Item' }] },
|
|
360
|
+
})
|
|
361
|
+
const fieldArray = useFieldArray(form, 'items')
|
|
362
|
+
|
|
363
|
+
expect(form.isDirty.value).toBe(false)
|
|
364
|
+
expect(fieldArray.dirty.value).toBe(false)
|
|
365
|
+
|
|
366
|
+
// Make a change
|
|
367
|
+
fieldArray.push({ name: 'New Item' })
|
|
368
|
+
await nextTick()
|
|
369
|
+
|
|
370
|
+
expect(form.isDirty.value).toBe(true)
|
|
371
|
+
expect(fieldArray.dirty.value).toBe(true)
|
|
372
|
+
|
|
373
|
+
// Reset form
|
|
374
|
+
form.reset()
|
|
375
|
+
await nextTick()
|
|
376
|
+
|
|
377
|
+
expect(form.isDirty.value).toBe(false)
|
|
378
|
+
expect(fieldArray.dirty.value).toBe(false)
|
|
379
|
+
})
|
|
380
|
+
})
|
|
381
|
+
})
|