Skip to content

Commit 66ca1a5

Browse files
committed
[fields] Fix population of missing tuple items
1 parent b8c9942 commit 66ca1a5

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

.changeset/odd-pumas-kiss.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@sjsf/form": patch
3+
---
4+
5+
Fix population of missing tuple items

packages/form/src/fields/array/context.svelte.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ export interface ArrayContext<V extends Validator> {
5656
moveItemDown(index: number): void;
5757
copyItem(index: number): void;
5858
removeItem(index: number): void;
59+
ensureArrayLength(length: number): void;
5960
}
6061

6162
const ARRAY_CONTEXT = Symbol("array-context");
@@ -146,6 +147,15 @@ function createItemsAPI<V extends Validator>(
146147
keyedArray.remove(index);
147148
validate();
148149
},
150+
ensureArrayLength(length) {
151+
let l = value()?.length;
152+
if (l === undefined) {
153+
return;
154+
}
155+
for (; l < length; l++) {
156+
keyedArray.push(undefined);
157+
}
158+
},
149159
} satisfies Partial<ArrayContext<V>>;
150160
}
151161

@@ -249,9 +259,7 @@ export function createTupleContext<V extends Validator>(
249259
setValue(new Array(itemsSchema.length));
250260
return;
251261
}
252-
if (val.length < itemsSchema.length) {
253-
val.push(...new Array(itemsSchema.length - value.length));
254-
}
262+
api.ensureArrayLength(itemsSchema.length);
255263
});
256264

257265
const schemaAdditionalItems = $derived.by(() => {

0 commit comments

Comments
 (0)