forked from twentyhq/twenty
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: fix field select options positions after option removal (twentyh…
…q#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).
- Loading branch information
1 parent
72521d5
commit 0fb416d
Showing
5 changed files
with
99 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
packages/twenty-front/src/utils/array/__tests__/toSpliced.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { toSpliced } from '../toSpliced'; | ||
|
||
describe('toSpliced', () => { | ||
it('removes elements from the array starting at the given index', () => { | ||
// Given | ||
const array = ['a', 'b', 'c', 'd', 'e']; | ||
|
||
// When | ||
const result = toSpliced(array, 2, 2); | ||
|
||
// Then | ||
expect(result).toEqual(['a', 'b', 'e']); | ||
}); | ||
|
||
it('replaces elements in the array starting at the given index', () => { | ||
// Given | ||
const array = ['a', 'b', 'c', 'd', 'e']; | ||
|
||
// When | ||
const result = toSpliced(array, 1, 2, 'x', 'y'); | ||
|
||
// Then | ||
expect(result).toEqual(['a', 'x', 'y', 'd', 'e']); | ||
}); | ||
|
||
it('returns a new array without modifying the original array', () => { | ||
// Given | ||
const array = ['a', 'b', 'c']; | ||
|
||
// When | ||
const result = toSpliced(array, 0, 1); | ||
|
||
// Then | ||
expect(result).toEqual(['b', 'c']); | ||
expect(array).toEqual(['a', 'b', 'c']); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
type ToSplicedFn = { | ||
<T>(array: T[], start: number, deleteCount?: number): T[]; | ||
<T>(array: T[], start: number, deleteCount: number, ...items: T[]): T[]; | ||
}; | ||
|
||
/** | ||
* Returns a new array with some elements removed and/or replaced at a given index. | ||
* This does the same as `Array.prototype.toSpliced`. | ||
* We cannot use the native `Array.prototype.toSpliced` method as of now because Chromatic's runners do not support it. | ||
* | ||
* @param array - The array to remove and/or replace elements from. | ||
* @param start - The index at which to start changing the array. | ||
* @param deleteCount - The number of elements in the array to remove from `start`. | ||
* @param items - The elements to add to the array at `start`. | ||
* | ||
* @returns A new array with elements removed and/or replaced at a given index. | ||
* | ||
* @example | ||
* toSpliced(['a', 'b', 'c'], 0, 1) | ||
* => ['b', 'c'] | ||
* toSpliced(['a', 'b', 'c'], 0, 1, 'd') | ||
* => ['d', 'b', 'c'] | ||
*/ | ||
export const toSpliced: ToSplicedFn = (array, ...args) => { | ||
const arrayCopy = [...array]; | ||
arrayCopy.splice(...(args as [number, number, ...any[]])); | ||
return arrayCopy; | ||
}; |