-
-
Notifications
You must be signed in to change notification settings - Fork 646
Expand file tree
/
Copy pathmetaHelper.ts
More file actions
238 lines (211 loc) · 6.86 KB
/
metaHelper.ts
File metadata and controls
238 lines (211 loc) · 6.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
import type {
FormApi,
FormAsyncValidateOrFn,
FormValidateOrFn,
} from './FormApi'
import type { AnyFieldMeta } from './FieldApi'
import type { DeepKeys } from './util-types'
type ValueFieldMode = 'insert' | 'remove' | 'swap' | 'move'
export const defaultFieldMeta: AnyFieldMeta = {
isValidating: false,
isTouched: false,
isBlurred: false,
isDirty: false,
isPristine: true,
isValid: true,
isDefaultValue: true,
errors: [],
errorMap: {},
errorSourceMap: {},
validationCount: 0,
}
export function metaHelper<
TFormData,
TOnMount extends undefined | FormValidateOrFn<TFormData>,
TOnChange extends undefined | FormValidateOrFn<TFormData>,
TOnChangeAsync extends undefined | FormAsyncValidateOrFn<TFormData>,
TOnBlur extends undefined | FormValidateOrFn<TFormData>,
TOnBlurAsync extends undefined | FormAsyncValidateOrFn<TFormData>,
TOnSubmit extends undefined | FormValidateOrFn<TFormData>,
TOnSubmitAsync extends undefined | FormAsyncValidateOrFn<TFormData>,
TOnDynamic extends undefined | FormValidateOrFn<TFormData>,
TOnDynamicAsync extends undefined | FormAsyncValidateOrFn<TFormData>,
TOnServer extends undefined | FormAsyncValidateOrFn<TFormData>,
TSubmitMeta = never,
>(
formApi: FormApi<
TFormData,
TOnMount,
TOnChange,
TOnChangeAsync,
TOnBlur,
TOnBlurAsync,
TOnSubmit,
TOnSubmitAsync,
TOnDynamic,
TOnDynamicAsync,
TOnServer,
TSubmitMeta
>,
) {
/**
* Handle the meta shift caused from moving a field from one index to another.
*/
function handleArrayMove(
field: DeepKeys<TFormData>,
fromIndex: number,
toIndex: number,
) {
const affectedFields = getAffectedFields(field, fromIndex, 'move', toIndex)
const startIndex = Math.min(fromIndex, toIndex)
const endIndex = Math.max(fromIndex, toIndex)
for (let i = startIndex; i <= endIndex; i++) {
affectedFields.push(getFieldPath(field, i))
}
// Store the original field meta that will be reapplied at the destination index
const fromFields = Object.keys(formApi.fieldInfo).reduce(
(fieldMap, fieldKey) => {
if (fieldKey.startsWith(getFieldPath(field, fromIndex))) {
fieldMap.set(
fieldKey as DeepKeys<TFormData>,
formApi.getFieldMeta(fieldKey as DeepKeys<TFormData>),
)
}
return fieldMap
},
new Map<DeepKeys<TFormData>, AnyFieldMeta | undefined>(),
)
shiftMeta(affectedFields, fromIndex < toIndex ? 'up' : 'down')
// Reapply the stored field meta at the destination index
Object.keys(formApi.fieldInfo)
.filter((fieldKey) => fieldKey.startsWith(getFieldPath(field, toIndex)))
.forEach((fieldKey) => {
const fromKey = fieldKey.replace(
getFieldPath(field, toIndex),
getFieldPath(field, fromIndex),
) as DeepKeys<TFormData>
const fromMeta = fromFields.get(fromKey)
if (fromMeta) {
formApi.setFieldMeta(fieldKey as DeepKeys<TFormData>, fromMeta)
}
})
}
/**
* Handle the meta shift from removing a field at the specified index.
*/
function handleArrayRemove(field: DeepKeys<TFormData>, index: number) {
const affectedFields = getAffectedFields(field, index, 'remove')
shiftMeta(affectedFields, 'up')
}
/**
* Handle the meta shift from swapping two fields at the specified indeces.
*/
function handleArraySwap(
field: DeepKeys<TFormData>,
index: number,
secondIndex: number,
) {
const affectedFields = getAffectedFields(field, index, 'swap', secondIndex)
affectedFields.forEach((fieldKey) => {
if (!fieldKey.toString().startsWith(getFieldPath(field, index))) {
return
}
const swappedKey = fieldKey
.toString()
.replace(
getFieldPath(field, index),
getFieldPath(field, secondIndex),
) as DeepKeys<TFormData>
const [meta1, meta2] = [
formApi.getFieldMeta(fieldKey),
formApi.getFieldMeta(swappedKey),
]
if (meta1) formApi.setFieldMeta(swappedKey, meta1)
if (meta2) formApi.setFieldMeta(fieldKey, meta2)
})
}
/**
* Handle the meta shift from inserting a field at the specified index.
*/
function handleArrayInsert(field: DeepKeys<TFormData>, insertIndex: number) {
const affectedFields = getAffectedFields(field, insertIndex, 'insert')
shiftMeta(affectedFields, 'down')
affectedFields.forEach((fieldKey) => {
if (fieldKey.toString().startsWith(getFieldPath(field, insertIndex))) {
formApi.setFieldMeta(fieldKey, getEmptyFieldMeta())
}
})
}
function getFieldPath(
field: DeepKeys<TFormData>,
index: number,
): DeepKeys<TFormData> {
return `${field}[${index}]` as DeepKeys<TFormData>
}
function getAffectedFields(
field: DeepKeys<TFormData>,
index: number,
mode: ValueFieldMode,
secondIndex?: number,
): DeepKeys<TFormData>[] {
const affectedFieldKeys = [getFieldPath(field, index)]
switch (mode) {
case 'swap':
affectedFieldKeys.push(getFieldPath(field, secondIndex!))
break
case 'move': {
const [startIndex, endIndex] = [
Math.min(index, secondIndex!),
Math.max(index, secondIndex!),
]
for (let i = startIndex; i <= endIndex; i++) {
affectedFieldKeys.push(getFieldPath(field, i))
}
break
}
default: {
const currentValue = formApi.getFieldValue(field)
const fieldItems = Array.isArray(currentValue)
? (currentValue as Array<unknown>).length
: 0
for (let i = index + 1; i < fieldItems; i++) {
affectedFieldKeys.push(getFieldPath(field, i))
}
break
}
}
return Object.keys(formApi.fieldInfo).filter((fieldKey) =>
affectedFieldKeys.some((key) => fieldKey.startsWith(key)),
) as DeepKeys<TFormData>[]
}
function updateIndex(
fieldKey: string,
direction: 'up' | 'down',
): DeepKeys<TFormData> {
return fieldKey.replace(/\[(\d+)\]/, (_, num) => {
const currIndex = parseInt(num, 10)
const newIndex =
direction === 'up' ? currIndex + 1 : Math.max(0, currIndex - 1)
return `[${newIndex}]`
}) as DeepKeys<TFormData>
}
function shiftMeta(fields: DeepKeys<TFormData>[], direction: 'up' | 'down') {
const sortedFields = direction === 'up' ? fields : [...fields].reverse()
sortedFields.forEach((fieldKey) => {
const nextFieldKey = updateIndex(fieldKey.toString(), direction)
const nextFieldMeta = formApi.getFieldMeta(nextFieldKey)
if (nextFieldMeta) {
formApi.setFieldMeta(fieldKey, nextFieldMeta)
} else {
formApi.setFieldMeta(fieldKey, getEmptyFieldMeta())
}
})
}
const getEmptyFieldMeta = (): AnyFieldMeta => defaultFieldMeta
return {
handleArrayMove,
handleArrayRemove,
handleArraySwap,
handleArrayInsert,
}
}