Skip to content

Commit

Permalink
Merge pull request #40 from x0k/fix-array-inputs-validation
Browse files Browse the repository at this point in the history
Fix array inputs validation
  • Loading branch information
x0k authored Nov 30, 2024
2 parents 9f3fe03 + 1505190 commit 9fc04f5
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 42 deletions.
5 changes: 5 additions & 0 deletions .changeset/eight-feet-sin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@sjsf/form": patch
---

Fix `array` based inputs validation
4 changes: 2 additions & 2 deletions apps/docs/src/content/docs/api-reference/fields.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@ interface FieldsAndProps<V extends SchemaValue> {
};
null: FieldCommonProps<V>;
enum: FieldCommonProps<V> & {
multiple?: boolean;
multiple?: Schema;
};
file: FieldCommonProps<V> & {
multiple?: boolean;
multiple?: Schema;
};
hidden: FieldCommonProps<V>;
}
Expand Down
31 changes: 16 additions & 15 deletions packages/form/src/basic-theme/widgets/select-widget.svelte
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
<script lang="ts">
import { indexMapper, multipleOptions, singleOption, type WidgetProps } from "@/form/index.js";
import {
indexMapper,
multipleOptions,
singleOption,
type WidgetProps,
} from "@/form/index.js";
let {
attributes,
Expand All @@ -8,23 +13,23 @@
multiple,
config,
}: WidgetProps<"select"> = $props();
const mapped = $derived(
(multiple ? multipleOptions : singleOption)({
mapper: () => indexMapper(options),
const mapped = $derived(
(multiple ? multipleOptions : singleOption)({
mapper: () => indexMapper(options),
// @ts-expect-error
value: () => value,
update: (v) => (value = v),
})
);
value: () => value,
update: (v) => (value = v),
})
);
</script>

{#snippet children()}
{#if !multiple && config.schema.default === undefined}
<option value={-1}>{attributes.placeholder}</option>
{/if}
{#each options as option, index (option.id)}
<option value={index} disabled={option.disabled} >
<option value={index} disabled={option.disabled}>
{option.label}
</option>
{/each}
Expand All @@ -39,11 +44,7 @@
{@render children()}
</select>
{:else}
<select
bind:value={mapped.value}
style="flex-grow: 1"
{...attributes}
>
<select bind:value={mapped.value} style="flex-grow: 1" {...attributes}>
{@render children()}
</select>
{/if}
10 changes: 5 additions & 5 deletions packages/form/src/form/context/event-handlers.svelte.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
ON_BLUR,
} from "../validation.js";

import type { FormContext } from './context.js';
import type { FormContext } from "./context.js";

export function makeEventHandlers(ctx: FormContext, validate: () => void) {
let changed = $state(false);
Expand All @@ -22,7 +22,7 @@ export function makeEventHandlers(ctx: FormContext, validate: () => void) {
touched = false;
});

const makeHandler = (event: number, validate: () => void) => {
const makeHandler = (event: number) => {
const m = ctx.inputsValidationMode;
if (
!(m & event) ||
Expand All @@ -34,9 +34,9 @@ export function makeEventHandlers(ctx: FormContext, validate: () => void) {
}
return validate;
};
const onInput = $derived(makeHandler(ON_INPUT, validate));
const onChange = $derived(makeHandler(ON_CHANGE, validate));
const onBlur = $derived(makeHandler(ON_BLUR, validate));
const onInput = $derived(makeHandler(ON_INPUT));
const onChange = $derived(makeHandler(ON_CHANGE));
const onBlur = $derived(makeHandler(ON_BLUR));

return {
oninput() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,7 @@
</script>

<Field
multiple
multiple={schemaItems}
bind:value
config={{
...config,
schema: schemaItems,
}}
config={config}
/>
26 changes: 15 additions & 11 deletions packages/form/src/form/fields/enum-field.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -13,29 +13,33 @@
import type { FieldProps } from "./model.js";
let {
config,
value = $bindable(),
multiple = false,
}: FieldProps<"enum"> = $props();
let { config, value = $bindable(), multiple }: FieldProps<"enum"> = $props();
const ctx = getFormContext();
const Template = $derived(getTemplate(ctx, "field", config));
const Widget = $derived(getWidget(ctx, "select", config));
const handlers = makeEventHandlers(ctx, () =>
validateField(ctx, config, value)
);
const handlers = makeEventHandlers(ctx, () => validateField(ctx, config, value));
const attributes = $derived(selectAttributes(ctx, config, handlers));
const options = $derived(
createOptions2(config.schema, config.idSchema, config.uiOptions, (i) =>
makePseudoId(ctx, config.idSchema.$id, i)
createOptions2(
multiple ?? config.schema,
config.idSchema,
config.uiOptions,
(i) => makePseudoId(ctx, config.idSchema.$id, i)
) ?? []
);
const errors = $derived(getErrors(ctx, config.idSchema));
</script>

<Template showTitle {value} {config} {errors}>
<Widget {attributes} {config} {errors} bind:value {options} {multiple} />
<Widget
{attributes}
{config}
{errors}
bind:value
{options}
multiple={multiple !== undefined}
/>
</Template>
4 changes: 2 additions & 2 deletions packages/form/src/form/fields/file-field.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
let {
config,
value = $bindable(),
multiple = false,
multiple
}: FieldProps<"file"> = $props();
const ctx = getFormContext();
Expand Down Expand Up @@ -96,6 +96,6 @@
{attributes}
{errors}
{config}
{multiple}
multiple={multiple !== undefined}
/>
</Template>
5 changes: 3 additions & 2 deletions packages/form/src/form/fields/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type {
ANY_OF_KEY,
SchemaObjectValue,
SchemaArrayValue,
Schema,
} from "@/core/index.js";

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

null: FieldCommonProps<V>;
enum: FieldCommonProps<V> & {
multiple?: boolean;
multiple?: Schema;
};
file: FieldCommonProps<V> & {
multiple?: boolean;
multiple?: Schema;
};
hidden: FieldCommonProps<V>;
}
Expand Down

0 comments on commit 9fc04f5

Please sign in to comment.