Skip to content

Commit 9fc04f5

Browse files
authored
Merge pull request #40 from x0k/fix-array-inputs-validation
Fix array inputs validation
2 parents 9f3fe03 + 1505190 commit 9fc04f5

File tree

8 files changed

+50
-42
lines changed

8 files changed

+50
-42
lines changed

.changeset/eight-feet-sin.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 `array` based inputs validation

apps/docs/src/content/docs/api-reference/fields.mdx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,10 @@ interface FieldsAndProps<V extends SchemaValue> {
5353
};
5454
null: FieldCommonProps<V>;
5555
enum: FieldCommonProps<V> & {
56-
multiple?: boolean;
56+
multiple?: Schema;
5757
};
5858
file: FieldCommonProps<V> & {
59-
multiple?: boolean;
59+
multiple?: Schema;
6060
};
6161
hidden: FieldCommonProps<V>;
6262
}
Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
<script lang="ts">
2-
import { indexMapper, multipleOptions, singleOption, type WidgetProps } from "@/form/index.js";
2+
import {
3+
indexMapper,
4+
multipleOptions,
5+
singleOption,
6+
type WidgetProps,
7+
} from "@/form/index.js";
38
49
let {
510
attributes,
@@ -8,23 +13,23 @@
813
multiple,
914
config,
1015
}: WidgetProps<"select"> = $props();
11-
12-
const mapped = $derived(
13-
(multiple ? multipleOptions : singleOption)({
14-
mapper: () => indexMapper(options),
16+
17+
const mapped = $derived(
18+
(multiple ? multipleOptions : singleOption)({
19+
mapper: () => indexMapper(options),
1520
// @ts-expect-error
16-
value: () => value,
17-
update: (v) => (value = v),
18-
})
19-
);
21+
value: () => value,
22+
update: (v) => (value = v),
23+
})
24+
);
2025
</script>
2126

2227
{#snippet children()}
2328
{#if !multiple && config.schema.default === undefined}
2429
<option value={-1}>{attributes.placeholder}</option>
2530
{/if}
2631
{#each options as option, index (option.id)}
27-
<option value={index} disabled={option.disabled} >
32+
<option value={index} disabled={option.disabled}>
2833
{option.label}
2934
</option>
3035
{/each}
@@ -39,11 +44,7 @@
3944
{@render children()}
4045
</select>
4146
{:else}
42-
<select
43-
bind:value={mapped.value}
44-
style="flex-grow: 1"
45-
{...attributes}
46-
>
47+
<select bind:value={mapped.value} style="flex-grow: 1" {...attributes}>
4748
{@render children()}
4849
</select>
4950
{/if}

packages/form/src/form/context/event-handlers.svelte.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
ON_BLUR,
88
} from "../validation.js";
99

10-
import type { FormContext } from './context.js';
10+
import type { FormContext } from "./context.js";
1111

1212
export function makeEventHandlers(ctx: FormContext, validate: () => void) {
1313
let changed = $state(false);
@@ -22,7 +22,7 @@ export function makeEventHandlers(ctx: FormContext, validate: () => void) {
2222
touched = false;
2323
});
2424

25-
const makeHandler = (event: number, validate: () => void) => {
25+
const makeHandler = (event: number) => {
2626
const m = ctx.inputsValidationMode;
2727
if (
2828
!(m & event) ||
@@ -34,9 +34,9 @@ export function makeEventHandlers(ctx: FormContext, validate: () => void) {
3434
}
3535
return validate;
3636
};
37-
const onInput = $derived(makeHandler(ON_INPUT, validate));
38-
const onChange = $derived(makeHandler(ON_CHANGE, validate));
39-
const onBlur = $derived(makeHandler(ON_BLUR, validate));
37+
const onInput = $derived(makeHandler(ON_INPUT));
38+
const onChange = $derived(makeHandler(ON_CHANGE));
39+
const onBlur = $derived(makeHandler(ON_BLUR));
4040

4141
return {
4242
oninput() {

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

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,7 @@
2121
</script>
2222

2323
<Field
24-
multiple
24+
multiple={schemaItems}
2525
bind:value
26-
config={{
27-
...config,
28-
schema: schemaItems,
29-
}}
26+
config={config}
3027
/>

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

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,29 +13,33 @@
1313
1414
import type { FieldProps } from "./model.js";
1515
16-
let {
17-
config,
18-
value = $bindable(),
19-
multiple = false,
20-
}: FieldProps<"enum"> = $props();
16+
let { config, value = $bindable(), multiple }: FieldProps<"enum"> = $props();
2117
2218
const ctx = getFormContext();
2319
2420
const Template = $derived(getTemplate(ctx, "field", config));
2521
const Widget = $derived(getWidget(ctx, "select", config));
2622
27-
const handlers = makeEventHandlers(ctx, () =>
28-
validateField(ctx, config, value)
29-
);
23+
const handlers = makeEventHandlers(ctx, () => validateField(ctx, config, value));
3024
const attributes = $derived(selectAttributes(ctx, config, handlers));
3125
const options = $derived(
32-
createOptions2(config.schema, config.idSchema, config.uiOptions, (i) =>
33-
makePseudoId(ctx, config.idSchema.$id, i)
26+
createOptions2(
27+
multiple ?? config.schema,
28+
config.idSchema,
29+
config.uiOptions,
30+
(i) => makePseudoId(ctx, config.idSchema.$id, i)
3431
) ?? []
3532
);
3633
const errors = $derived(getErrors(ctx, config.idSchema));
3734
</script>
3835

3936
<Template showTitle {value} {config} {errors}>
40-
<Widget {attributes} {config} {errors} bind:value {options} {multiple} />
37+
<Widget
38+
{attributes}
39+
{config}
40+
{errors}
41+
bind:value
42+
{options}
43+
multiple={multiple !== undefined}
44+
/>
4145
</Template>

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
let {
1919
config,
2020
value = $bindable(),
21-
multiple = false,
21+
multiple
2222
}: FieldProps<"file"> = $props();
2323
2424
const ctx = getFormContext();
@@ -96,6 +96,6 @@
9696
{attributes}
9797
{errors}
9898
{config}
99-
{multiple}
99+
multiple={multiple !== undefined}
100100
/>
101101
</Template>

packages/form/src/form/fields/model.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import type {
66
ANY_OF_KEY,
77
SchemaObjectValue,
88
SchemaArrayValue,
9+
Schema,
910
} from "@/core/index.js";
1011

1112
import type { Config } from "../config.js";
@@ -49,10 +50,10 @@ export interface FieldsAndProps<V extends SchemaValue> {
4950

5051
null: FieldCommonProps<V>;
5152
enum: FieldCommonProps<V> & {
52-
multiple?: boolean;
53+
multiple?: Schema;
5354
};
5455
file: FieldCommonProps<V> & {
55-
multiple?: boolean;
56+
multiple?: Schema;
5657
};
5758
hidden: FieldCommonProps<V>;
5859
}

0 commit comments

Comments
 (0)