Skip to content

Commit 0e30504

Browse files
committed
feat(utils): add select option helpers
Signed-off-by: Vitor Mattos <1079143+vitormattos@users.noreply.github.com>
1 parent 9df8a1c commit 0e30504

1 file changed

Lines changed: 40 additions & 0 deletions

File tree

src/utils/selectFieldOptions.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// SPDX-FileCopyrightText: 2026 LibreCode coop and LibreCode contributors
2+
// SPDX-License-Identifier: AGPL-3.0-or-later
3+
4+
export interface EditableSelectOption {
5+
id: string
6+
value: string
7+
}
8+
9+
export const createEditableSelectOptions = (
10+
values: string[],
11+
createId: () => string,
12+
): EditableSelectOption[] => values.map((value) => ({
13+
id: createId(),
14+
value,
15+
}))
16+
17+
export const normalizeEditableSelectOptionValue = (value: string): string => value.trim().toLowerCase()
18+
19+
export const parseEditableSelectOptionValues = (value: string): string[] => value
20+
.split(/\r?\n/g)
21+
.map((entry) => entry.trim())
22+
.filter((entry) => entry.length > 0)
23+
24+
export const extractEditableSelectOptionValues = (options: EditableSelectOption[]): string[] => options.map(({ value }) => value)
25+
26+
export const moveEditableSelectOption = (
27+
options: EditableSelectOption[],
28+
index: number,
29+
direction: -1 | 1,
30+
): EditableSelectOption[] => {
31+
const targetIndex = index + direction
32+
if (index < 0 || index >= options.length || targetIndex < 0 || targetIndex >= options.length) {
33+
return options
34+
}
35+
36+
const reordered = [...options]
37+
const [selected] = reordered.splice(index, 1)
38+
reordered.splice(targetIndex, 0, selected)
39+
return reordered
40+
}

0 commit comments

Comments
 (0)