Skip to content

Commit 17bf6b5

Browse files
authored
feat: add array fields move method (#647)
1 parent cae8bd7 commit 17bf6b5

File tree

4 files changed

+32
-1
lines changed

4 files changed

+32
-1
lines changed

docs/framework/react/guides/basic-concepts.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ Example:
194194

195195
Array fields allow you to manage a list of values within a form, such as a list of hobbies. You can create an array field using the `form.Field` component with the `mode="array"` prop.
196196

197-
When working with array fields, you can use the fields `pushValue`, `removeValue`, and `swapValues` methods to add, remove, and swap values in the array.
197+
When working with array fields, you can use the fields `pushValue`, `removeValue`, `swapValues` and `moveValue` methods to add, remove, and swap values in the array.
198198

199199
Example:
200200

packages/form-core/src/FieldApi.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,9 @@ export class FieldApi<
482482
swapValues = (aIndex: number, bIndex: number) =>
483483
this.form.swapFieldValues(this.name, aIndex, bIndex)
484484

485+
moveValue = (aIndex: number, bIndex: number) =>
486+
this.form.moveFieldValues(this.name, aIndex, bIndex)
487+
485488
validateSync = (value = this.state.value, cause: ValidationCause) => {
486489
const validates = getSyncValidatorArray(cause, this.options)
487490

packages/form-core/src/FormApi.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -718,6 +718,17 @@ export class FormApi<
718718
return setBy(setBy(prev, `${index1}`, prev2), `${index2}`, prev1)
719719
})
720720
}
721+
722+
moveFieldValues = <TField extends DeepKeys<TFormData>>(
723+
field: TField,
724+
index1: number,
725+
index2: number,
726+
) => {
727+
this.setFieldValue(field, (prev: any) => {
728+
prev.splice(index2, 0, prev.splice(index1, 1)[0])
729+
return prev
730+
})
731+
}
721732
}
722733

723734
function normalizeError(rawError?: ValidationError) {

packages/form-core/src/tests/FieldApi.spec.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,23 @@ describe('field api', () => {
158158
expect(field.getValue()).toStrictEqual(['two', 'one'])
159159
})
160160

161+
it('should move a value from an array value correctly', () => {
162+
const form = new FormApi({
163+
defaultValues: {
164+
names: ['one', 'two', 'three', 'four'],
165+
},
166+
})
167+
168+
const field = new FieldApi({
169+
form,
170+
name: 'names',
171+
})
172+
173+
field.moveValue(2, 0)
174+
175+
expect(field.getValue()).toStrictEqual(['three', 'one', 'two', 'four'])
176+
})
177+
161178
it('should not throw errors when no meta info is stored on a field and a form re-renders', async () => {
162179
const form = new FormApi({
163180
defaultValues: {

0 commit comments

Comments
 (0)