Skip to content

Commit fbd46f4

Browse files
committed
[form] Use keyed array in array fields
1 parent e96946a commit fbd46f4

File tree

4 files changed

+21
-41
lines changed

4 files changed

+21
-41
lines changed

packages/form/src/form/fields/array/array-field.svelte

Lines changed: 15 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<script lang="ts">
2+
import { createKeyedArray } from "@/lib/keyed-array.svelte.js";
23
import { isFixedItems, type Schema } from "@/core/index.js";
34
45
import {
@@ -17,10 +18,7 @@
1718
1819
import type { FieldProps } from "../model.js";
1920
20-
import {
21-
setArrayContext,
22-
type ArrayContext,
23-
} from "./context.js";
21+
import { setArrayContext, type ArrayContext } from "./context.js";
2422
2523
let { value = $bindable(), config }: FieldProps<"array"> = $props();
2624
@@ -51,6 +49,8 @@
5149
validateField(ctx, config, value);
5250
}
5351
52+
const keyedArray = createKeyedArray(() => value ?? []);
53+
5454
const arrayCtx: ArrayContext = {
5555
get errors() {
5656
return errors;
@@ -74,45 +74,29 @@
7474
return copyable;
7575
},
7676
validate,
77+
key(index) {
78+
return keyedArray.key(index);
79+
},
7780
pushItem(itemSchema: Schema) {
78-
if (value === undefined) {
79-
return;
80-
}
81-
value.push(getDefaultFieldState(ctx, itemSchema, undefined));
81+
keyedArray.push(getDefaultFieldState(ctx, itemSchema, undefined));
8282
validate();
8383
},
8484
moveItemUp(index) {
85-
if (value === undefined || index < 1) {
86-
return;
87-
}
88-
const tmp = value[index];
89-
value[index] = value[index - 1];
90-
value[index - 1] = tmp;
85+
keyedArray.swap(index, index - 1);
9186
validate();
9287
},
9388
moveItemDown(index) {
94-
if (value === undefined || index > value.length - 2) {
95-
return;
96-
}
97-
const tmp = value[index];
98-
value[index] = value[index + 1];
99-
value[index + 1] = tmp;
89+
keyedArray.swap(index, index + 1);
10090
validate();
10191
},
10292
copyItem(index) {
103-
if (value === undefined) {
104-
return
105-
}
106-
value.splice(index, 0, $state.snapshot(value[index]))
107-
validate()
93+
keyedArray.insert(index, $state.snapshot(value![index]));
94+
validate();
10895
},
10996
removeItem(index) {
110-
if (value === undefined) {
111-
return
112-
}
113-
value.splice(index, 1)
114-
validate()
115-
}
97+
keyedArray.remove(index);
98+
validate();
99+
},
116100
};
117101
setArrayContext(arrayCtx);
118102

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

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,14 @@
11
import { getContext, setContext } from "svelte";
22

3-
import {
4-
isFilesArray2 as isFilesArrayInternal,
5-
type SchemaArrayValue,
6-
type Schema,
7-
type SchemaValue,
8-
} from "@/core/index.js";
3+
import type { SchemaArrayValue, Schema, SchemaValue } from "@/core/index.js";
94

10-
import type { ValidationError } from "../../validator.js";
115
import {
126
type FormContext,
137
makeArrayItemId,
148
makeIdSchema,
159
} from "../../context/index.js";
16-
import { type IdSchema } from "../../id-schema.js";
10+
import type { ValidationError } from "../../validator.js";
11+
import type { IdSchema } from "../../id-schema.js";
1712

1813
export interface ArrayContext {
1914
disabled: boolean;
@@ -25,6 +20,7 @@ export interface ArrayContext {
2520
errors: ValidationError<unknown>[];
2621
/** @deprecated */
2722
validate: () => void;
23+
key(index: number): number;
2824
pushItem(itemSchema: Schema): void;
2925
moveItemUp(index: number): void;
3026
moveItemDown(index: number): void;

packages/form/src/form/fields/array/fixed-array-field.svelte

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@
7878
addButton={arrayCtx.canAdd && schemaAdditionalItems ? addButton : undefined}
7979
>
8080
{#if value}
81-
{#each value as item, index}
81+
{#each value as item, index (arrayCtx.key(index))}
8282
{@const isAdditional = index >= schemaItems.length}
8383
{@const itemSchema = isAdditional && schemaAdditionalItems ? retrieveSchema(ctx, schemaAdditionalItems, item) : schemaItems[index]!}
8484
{@const uiSchema = config.uiSchema}

packages/form/src/form/fields/array/normal-array-field.svelte

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@
6363
addButton={arrayCtx.canAdd ? addButton : undefined}
6464
>
6565
{#if value}
66-
{#each value as item, index}
66+
{#each value as item, index (arrayCtx.key(index))}
6767
{@const itemSchema = retrieveSchema(ctx, schemaItems, item)}
6868
{@const itemIdSchema = makeIdSchema(
6969
ctx,

0 commit comments

Comments
 (0)