@sjsf/form
Advanced tools
| <script lang="ts"> | ||
| import { multipleOptions, type WidgetProps } from "../../form/index.js"; | ||
| import { multipleOptions, indexMapper, type WidgetProps } from "../../form/index.js"; | ||
@@ -11,3 +11,3 @@ let { | ||
| const guarder = multipleOptions({ | ||
| options: () => options, | ||
| mapper: () => indexMapper(options), | ||
| value: () => value, | ||
@@ -14,0 +14,0 @@ update: (v) => (value = v), |
| <script lang="ts"> | ||
| import { singleOption, type WidgetProps } from "../../form/index.js"; | ||
| import { singleOption, indexMapper, type WidgetProps } from "../../form/index.js"; | ||
@@ -11,3 +11,3 @@ let { | ||
| const guarder = singleOption({ | ||
| options: () => options, | ||
| mapper: () => indexMapper(options), | ||
| value: () => value, | ||
@@ -14,0 +14,0 @@ update: (v) => (value = v), |
| <script lang="ts"> | ||
| import { multipleOptions, singleOption, type WidgetProps } from "../../form/index.js"; | ||
| import { indexMapper, multipleOptions, singleOption, type WidgetProps } from "../../form/index.js"; | ||
@@ -16,3 +16,3 @@ let { | ||
| (multiple ? multipleOptions : singleOption)({ | ||
| options: () => options, | ||
| mapper: () => indexMapper(options), | ||
| // @ts-expect-error | ||
@@ -19,0 +19,0 @@ value: () => value, |
| import type { EnumOption, SchemaArrayValue, SchemaValue } from '../core/index.js'; | ||
| export declare function singleOption({ options, value, update, readonly, }: { | ||
| options: () => EnumOption<SchemaValue>[]; | ||
| export interface OptionsMapper<V> { | ||
| fromValue: (value: SchemaValue | undefined) => V; | ||
| toValue: (value: V) => SchemaValue | undefined; | ||
| } | ||
| export declare function indexMapper(options: EnumOption<SchemaValue>[]): OptionsMapper<number>; | ||
| export declare function stringIndexMapper(options: EnumOption<SchemaValue>[]): OptionsMapper<string>; | ||
| export declare function singleOption<V>({ mapper, value, update, readonly, }: { | ||
| mapper: () => OptionsMapper<V>; | ||
| value: () => SchemaValue | undefined; | ||
@@ -8,6 +14,6 @@ update: (value: SchemaValue | undefined) => void; | ||
| }): { | ||
| value: number; | ||
| value: V; | ||
| }; | ||
| export declare function multipleOptions({ options, value, update, readonly, }: { | ||
| options: () => EnumOption<SchemaValue>[]; | ||
| export declare function multipleOptions<V>({ mapper, value, update, readonly, }: { | ||
| mapper: () => OptionsMapper<V>; | ||
| value: () => SchemaArrayValue | undefined; | ||
@@ -17,3 +23,3 @@ update: (value: SchemaArrayValue) => void; | ||
| }): { | ||
| value: number[]; | ||
| value: V[]; | ||
| }; |
| import { deepEqual } from "../lib/deep-equal.js"; | ||
| import { isObject } from '../lib/object.js'; | ||
| function makeOptionsMapper(options) { | ||
| export function indexMapper(options) { | ||
| const map = new Map(options.map((option, index) => [option.value, index])); | ||
| return { | ||
| valueToIndex(value) { | ||
| fromValue(value) { | ||
| if (value === undefined) { | ||
@@ -19,3 +19,3 @@ return -1; | ||
| }, | ||
| indexToValue(index) { | ||
| toValue(index) { | ||
| return options[index]?.value; | ||
@@ -25,7 +25,18 @@ }, | ||
| } | ||
| export function singleOption({ options, value, update, readonly, }) { | ||
| const { indexToValue, valueToIndex } = $derived(makeOptionsMapper(options())); | ||
| export function stringIndexMapper(options) { | ||
| const { fromValue, toValue } = indexMapper(options); | ||
| return { | ||
| fromValue(value) { | ||
| return String(fromValue(value)); | ||
| }, | ||
| toValue(value) { | ||
| return toValue(Number(value)); | ||
| }, | ||
| }; | ||
| } | ||
| export function singleOption({ mapper, value, update, readonly, }) { | ||
| const { fromValue, toValue } = $derived(mapper()); | ||
| return { | ||
| get value() { | ||
| return valueToIndex(value()); | ||
| return fromValue(value()); | ||
| }, | ||
@@ -36,11 +47,11 @@ set value(v) { | ||
| } | ||
| update(indexToValue(v)); | ||
| update(toValue(v)); | ||
| } | ||
| }; | ||
| } | ||
| export function multipleOptions({ options, value, update, readonly, }) { | ||
| const { indexToValue, valueToIndex } = $derived(makeOptionsMapper(options())); | ||
| export function multipleOptions({ mapper, value, update, readonly, }) { | ||
| const { fromValue, toValue } = $derived(mapper()); | ||
| return { | ||
| get value() { | ||
| return value()?.map(valueToIndex) ?? []; | ||
| return value()?.map(fromValue) ?? []; | ||
| }, | ||
@@ -51,5 +62,5 @@ set value(v) { | ||
| } | ||
| update(v.map(indexToValue)); | ||
| update(v.map(toValue)); | ||
| } | ||
| }; | ||
| } |
+1
-1
| { | ||
| "name": "@sjsf/form", | ||
| "version": "0.0.5", | ||
| "version": "0.0.6", | ||
| "description": "Svelte 5 library for creating forms based on JSON schema.", | ||
@@ -5,0 +5,0 @@ "license": "(MIT AND Apache-2.0)", |
264699
0.23%4596
0.37%