fix: fix field select options positions after option removal (#5350)
- Adds an util `toSpliced`. We cannot used the native Javascript `Array.prototype.toSpliced` method as Chromatic servers don't support it. - Makes sure Select field options have sequential positions after removing an option (form validation schema checks that positions are sequential and considers options invalid otherwise).
This commit is contained in:
@ -1,3 +1,5 @@
|
||||
import { toSpliced } from '~/utils/array/toSpliced';
|
||||
|
||||
/**
|
||||
* Moves an item in an array from one index to another.
|
||||
*
|
||||
@ -20,9 +22,14 @@ export const moveArrayItem = <ArrayItem>(
|
||||
return array;
|
||||
}
|
||||
|
||||
const arrayWithMovedItem = [...array];
|
||||
const [itemToMove] = arrayWithMovedItem.splice(fromIndex, 1);
|
||||
arrayWithMovedItem.splice(toIndex, 0, itemToMove);
|
||||
const itemToMove = array[fromIndex];
|
||||
const arrayWithoutItem = toSpliced(array, fromIndex, 1);
|
||||
const arrayWithMovedItem = toSpliced(
|
||||
arrayWithoutItem,
|
||||
toIndex,
|
||||
0,
|
||||
itemToMove,
|
||||
);
|
||||
|
||||
return arrayWithMovedItem;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user