@rokkit/forms 1.0.0-next.122

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (84) hide show
  1. package/dist/src/forms-old/input/types.d.ts +7 -0
  2. package/dist/src/forms-old/lib/form.d.ts +95 -0
  3. package/dist/src/forms-old/lib/index.d.ts +1 -0
  4. package/dist/src/index.d.ts +5 -0
  5. package/dist/src/input/index.d.ts +18 -0
  6. package/dist/src/lib/builder.svelte.d.ts +140 -0
  7. package/dist/src/lib/deprecated/nested.d.ts +48 -0
  8. package/dist/src/lib/deprecated/nested.spec.d.ts +1 -0
  9. package/dist/src/lib/deprecated/validator.d.ts +30 -0
  10. package/dist/src/lib/deprecated/validator.spec.d.ts +1 -0
  11. package/dist/src/lib/fields.d.ts +16 -0
  12. package/dist/src/lib/fields.spec.d.ts +1 -0
  13. package/dist/src/lib/index.d.ts +7 -0
  14. package/dist/src/lib/layout.d.ts +7 -0
  15. package/dist/src/lib/schema.d.ts +7 -0
  16. package/dist/src/lib/validation.d.ts +41 -0
  17. package/dist/src/types.d.ts +5 -0
  18. package/package.json +38 -0
  19. package/src/DataEditor.svelte +30 -0
  20. package/src/FieldLayout.svelte +48 -0
  21. package/src/FormRenderer.svelte +118 -0
  22. package/src/Input.svelte +75 -0
  23. package/src/InputField.svelte +55 -0
  24. package/src/ListEditor.svelte +44 -0
  25. package/src/NestedEditor.svelte +85 -0
  26. package/src/forms-old/CheckBox.svelte +56 -0
  27. package/src/forms-old/DataEditor.svelte +30 -0
  28. package/src/forms-old/FieldLayout.svelte +48 -0
  29. package/src/forms-old/Form.svelte +17 -0
  30. package/src/forms-old/Icon.svelte +76 -0
  31. package/src/forms-old/Item.svelte +25 -0
  32. package/src/forms-old/ListEditor.svelte +44 -0
  33. package/src/forms-old/Tabs.svelte +57 -0
  34. package/src/forms-old/Wrapper.svelte +12 -0
  35. package/src/forms-old/input/Input.svelte +17 -0
  36. package/src/forms-old/input/InputField.svelte +70 -0
  37. package/src/forms-old/input/InputSelect.svelte +23 -0
  38. package/src/forms-old/input/InputSwitch.svelte +19 -0
  39. package/src/forms-old/input/types.js +29 -0
  40. package/src/forms-old/lib/form.js +72 -0
  41. package/src/forms-old/lib/index.js +12 -0
  42. package/src/forms-old/mocks/CustomField.svelte +7 -0
  43. package/src/forms-old/mocks/CustomWrapper.svelte +8 -0
  44. package/src/forms-old/mocks/Register.svelte +25 -0
  45. package/src/index.js +7 -0
  46. package/src/inp/Input.svelte +17 -0
  47. package/src/inp/InputField.svelte +69 -0
  48. package/src/inp/InputSelect.svelte +23 -0
  49. package/src/inp/InputSwitch.svelte +19 -0
  50. package/src/input/InputCheckbox.svelte +74 -0
  51. package/src/input/InputColor.svelte +42 -0
  52. package/src/input/InputDate.svelte +54 -0
  53. package/src/input/InputDateTime.svelte +54 -0
  54. package/src/input/InputEmail.svelte +63 -0
  55. package/src/input/InputFile.svelte +45 -0
  56. package/src/input/InputMonth.svelte +54 -0
  57. package/src/input/InputNumber.svelte +57 -0
  58. package/src/input/InputPassword.svelte +60 -0
  59. package/src/input/InputRadio.svelte +60 -0
  60. package/src/input/InputRange.svelte +51 -0
  61. package/src/input/InputSelect.svelte +71 -0
  62. package/src/input/InputSwitch.svelte +29 -0
  63. package/src/input/InputTel.svelte +60 -0
  64. package/src/input/InputText.svelte +60 -0
  65. package/src/input/InputTextArea.svelte +59 -0
  66. package/src/input/InputTime.svelte +54 -0
  67. package/src/input/InputUrl.svelte +60 -0
  68. package/src/input/InputWeek.svelte +54 -0
  69. package/src/input/index.js +23 -0
  70. package/src/lib/Input.svelte +291 -0
  71. package/src/lib/builder.svelte.js +359 -0
  72. package/src/lib/deprecated/Form.svelte +17 -0
  73. package/src/lib/deprecated/FormRenderer.svelte +121 -0
  74. package/src/lib/deprecated/nested.js +192 -0
  75. package/src/lib/deprecated/nested.spec.js +512 -0
  76. package/src/lib/deprecated/validator.js +137 -0
  77. package/src/lib/deprecated/validator.spec.js +348 -0
  78. package/src/lib/fields.js +119 -0
  79. package/src/lib/fields.spec.js +250 -0
  80. package/src/lib/index.js +7 -0
  81. package/src/lib/layout.js +63 -0
  82. package/src/lib/schema.js +32 -0
  83. package/src/lib/validation.js +273 -0
  84. package/src/types.js +29 -0
@@ -0,0 +1,7 @@
1
+ export namespace types {
2
+ export let string: import("svelte/legacy").LegacyComponentType;
3
+ export let integer: import("svelte/legacy").LegacyComponentType;
4
+ export { CheckBox as boolean };
5
+ export let phone: import("svelte/legacy").LegacyComponentType;
6
+ }
7
+ import CheckBox from '../CheckBox.svelte';
@@ -0,0 +1,95 @@
1
+ export namespace messages {
2
+ let required: string;
3
+ let email: string;
4
+ let url: string;
5
+ let color: string;
6
+ let number: string;
7
+ let min: string;
8
+ let max: string;
9
+ let pattern: string;
10
+ let exclusiveMin: string;
11
+ let exclusiveMax: string;
12
+ let minLength: string;
13
+ let maxLength: string;
14
+ let minItems: string;
15
+ let maxItems: string;
16
+ let uniqueItems: string;
17
+ let contains: string;
18
+ let exclude: string;
19
+ let integer: string;
20
+ }
21
+ export namespace dataTypes {
22
+ export namespace integer_1 {
23
+ let editor: string;
24
+ namespace props {
25
+ let type: string;
26
+ let step: number;
27
+ }
28
+ let availableProps: string[];
29
+ }
30
+ export { integer_1 as integer };
31
+ export namespace number_1 {
32
+ let editor_1: string;
33
+ export { editor_1 as editor };
34
+ export namespace props_1 {
35
+ let type_1: string;
36
+ export { type_1 as type };
37
+ let step_1: number;
38
+ export { step_1 as step };
39
+ }
40
+ export { props_1 as props };
41
+ let availableProps_1: string[];
42
+ export { availableProps_1 as availableProps };
43
+ }
44
+ export { number_1 as number };
45
+ export namespace range {
46
+ let editor_2: string;
47
+ export { editor_2 as editor };
48
+ export namespace props_2 {
49
+ let type_2: string;
50
+ export { type_2 as type };
51
+ }
52
+ export { props_2 as props };
53
+ }
54
+ export let string: {
55
+ default: string;
56
+ text: string;
57
+ password: string;
58
+ email: string;
59
+ url: string;
60
+ tel: string;
61
+ date: string;
62
+ 'datetime-local': string;
63
+ time: string;
64
+ week: string;
65
+ month: string;
66
+ file: string;
67
+ hidden: string;
68
+ color: string;
69
+ colorpicker: string;
70
+ };
71
+ export namespace _enum {
72
+ let _default: string;
73
+ export { _default as default };
74
+ export let select: string;
75
+ export let radio: string;
76
+ }
77
+ export { _enum as enum };
78
+ export namespace boolean {
79
+ let _default_1: string;
80
+ export { _default_1 as default };
81
+ export let checkbox: string;
82
+ let _switch: string;
83
+ export { _switch as switch };
84
+ let radio_1: string;
85
+ export { radio_1 as radio };
86
+ }
87
+ export namespace array {
88
+ let _default_2: string;
89
+ export { _default_2 as default };
90
+ }
91
+ export namespace object {
92
+ let _default_3: string;
93
+ export { _default_3 as default };
94
+ }
95
+ }
@@ -0,0 +1 @@
1
+ export { messages, dataTypes } from "./form";
@@ -0,0 +1,5 @@
1
+ export { FormBuilder } from "./lib/builder.svelte.js";
2
+ export { default as FormRenderer } from "./FormRenderer.svelte";
3
+ export { default as Input } from "./Input.svelte";
4
+ export { default as InputField } from "./InputField.svelte";
5
+ export * from "./input";
@@ -0,0 +1,18 @@
1
+ export { default as InputCheckbox } from "./InputCheckbox.svelte";
2
+ export { default as InputColor } from "./InputColor.svelte";
3
+ export { default as InputDate } from "./InputDate.svelte";
4
+ export { default as InputDateTime } from "./InputDateTime.svelte";
5
+ export { default as InputEmail } from "./InputEmail.svelte";
6
+ export { default as InputFile } from "./InputFile.svelte";
7
+ export { default as InputMonth } from "./InputMonth.svelte";
8
+ export { default as InputNumber } from "./InputNumber.svelte";
9
+ export { default as InputPassword } from "./InputPassword.svelte";
10
+ export { default as InputRadio } from "./InputRadio.svelte";
11
+ export { default as InputRange } from "./InputRange.svelte";
12
+ export { default as InputSelect } from "./InputSelect.svelte";
13
+ export { default as InputTel } from "./InputTel.svelte";
14
+ export { default as InputText } from "./InputText.svelte";
15
+ export { default as InputTextArea } from "./InputTextArea.svelte";
16
+ export { default as InputTime } from "./InputTime.svelte";
17
+ export { default as InputUrl } from "./InputUrl.svelte";
18
+ export { default as InputWeek } from "./InputWeek.svelte";
@@ -0,0 +1,140 @@
1
+ /**
2
+ * @typedef {Object} FormElement
3
+ * @property {string} scope - JSON Pointer path (e.g., '#/email', '#/user/name')
4
+ * @property {string} type - Input type (text, number, range, checkbox, select, etc.)
5
+ * @property {any} value - Current value from data
6
+ * @property {boolean} override - Whether to use custom child snippet (from layout)
7
+ * @property {Object} props - Merged properties from schema + layout + validation
8
+ * @property {string} [props.label] - Display label (from layout)
9
+ * @property {string} [props.description] - Help text (from layout)
10
+ * @property {string} [props.placeholder] - Placeholder text (from layout)
11
+ * @property {boolean} [props.required] - Required flag (from schema)
12
+ * @property {number} [props.min] - Minimum value (from schema)
13
+ * @property {number} [props.max] - Maximum value (from schema)
14
+ * @property {Object} [props.message] - Validation message object
15
+ * @property {string} [props.message.state] - Message state: 'error', 'warning', 'info', 'success'
16
+ * @property {string} [props.message.text] - Message text content
17
+ */
18
+ /**
19
+ * FormBuilder class for dynamically generating forms from data structures
20
+ */
21
+ export class FormBuilder {
22
+ /**
23
+ * Create a new FormBuilder instance
24
+ * @param {Object} [data={}] - Initial data object
25
+ * @param {Object|null} [schema=null] - Optional schema override
26
+ * @param {Object|null} [layout=null] - Optional layout override
27
+ */
28
+ constructor(data?: Object, schema?: Object | null, layout?: Object | null);
29
+ /** @type {FormElement[]} */
30
+ elements: FormElement[];
31
+ combined: any;
32
+ /**
33
+ * Set the data
34
+ * @param {Object} value - New data object
35
+ */
36
+ set data(value: Object);
37
+ /**
38
+ * Get the current data
39
+ * @returns {Object} Current data object
40
+ */
41
+ get data(): Object;
42
+ /**
43
+ * Set the schema
44
+ * @param {Object} value - New schema object
45
+ */
46
+ set schema(value: Object);
47
+ /**
48
+ * Get the current schema
49
+ * @returns {Object} Current schema object
50
+ */
51
+ get schema(): Object;
52
+ /**
53
+ * Set the layout
54
+ * @param {Object} value - New layout object
55
+ */
56
+ set layout(value: Object);
57
+ /**
58
+ * Get the current layout
59
+ * @returns {Object} Current layout object
60
+ */
61
+ get layout(): Object;
62
+ /**
63
+ * Set validation messages for fields
64
+ * @param {Object} value - Validation object with field paths as keys
65
+ */
66
+ set validation(value: Object);
67
+ /**
68
+ * Get the current validation state
69
+ * @returns {Object} Current validation object
70
+ */
71
+ get validation(): Object;
72
+ /**
73
+ * Update a specific field value
74
+ * @param {string} path - Field path (e.g., 'count', 'settings/distance')
75
+ * @param {any} value - New value
76
+ */
77
+ updateField(path: string, value: any): void;
78
+ /**
79
+ * Get a field value by path
80
+ * @param {string} path - Field path
81
+ * @returns {any} Field value
82
+ */
83
+ getValue(path: string): any;
84
+ /**
85
+ * Set validation message for a specific field
86
+ * @param {string} fieldPath - Field path (without '#/' prefix)
87
+ * @param {Object|null} message - Validation message object or null to clear
88
+ * @param {string} message.state - Message state: 'error', 'warning', 'info', 'success'
89
+ * @param {string} message.text - Message text content
90
+ */
91
+ setFieldValidation(fieldPath: string, message: Object | null): void;
92
+ /**
93
+ * Clear all validation messages
94
+ */
95
+ clearValidation(): void;
96
+ /**
97
+ * Reset form to initial state
98
+ */
99
+ reset(): void;
100
+ #private;
101
+ }
102
+ export type FormElement = {
103
+ /**
104
+ * - JSON Pointer path (e.g., '#/email', '#/user/name')
105
+ */
106
+ scope: string;
107
+ /**
108
+ * - Input type (text, number, range, checkbox, select, etc.)
109
+ */
110
+ type: string;
111
+ /**
112
+ * - Current value from data
113
+ */
114
+ value: any;
115
+ /**
116
+ * - Whether to use custom child snippet (from layout)
117
+ */
118
+ override: boolean;
119
+ /**
120
+ * - Merged properties from schema + layout + validation
121
+ */
122
+ props: {
123
+ label?: string | undefined;
124
+ description?: string | undefined;
125
+ placeholder?: string | undefined;
126
+ required?: boolean | undefined;
127
+ min?: number | undefined;
128
+ max?: number | undefined;
129
+ message?: {
130
+ /**
131
+ * - Message state: 'error', 'warning', 'info', 'success'
132
+ */
133
+ state?: string | undefined;
134
+ /**
135
+ * - Message text content
136
+ */
137
+ text?: string | undefined;
138
+ } | undefined;
139
+ };
140
+ };
@@ -0,0 +1,48 @@
1
+ /**
2
+ * Flattens an object into a flat object
3
+ *
4
+ * @param {Object} input - The object to flatten
5
+ * @param {String} scope - The scope of the object
6
+ */
7
+ export function flattenObject(input: Object, scope?: string): any;
8
+ /**
9
+ * Flattens an object into an array of key-value pairs
10
+ *
11
+ * @param {Object} input - The object to flatten
12
+ * @param {String} scope - The scope of the object
13
+ */
14
+ export function flattenAttributes(input: Object, scope?: string): {
15
+ key: string;
16
+ value: any;
17
+ type: string;
18
+ scope: string;
19
+ }[];
20
+ /**
21
+ * Derives a nested schema from an object
22
+ *
23
+ * @param {Object} input - The object to derive the schema from
24
+ * @param {String} scope - The scope of the object
25
+ * @returns {Object} The derived schema
26
+ */
27
+ export function deriveNestedSchema(input: Object, scope?: string): Object;
28
+ /**
29
+ * Flattens an element into a flat object
30
+ *
31
+ * @param {Object} element - The element to flatten
32
+ */
33
+ export function flattenElement(element: Object): any;
34
+ /**
35
+ * Generates an index array referencing the input data
36
+ *
37
+ * @param {Object} data - The flat object to index
38
+ * @param {String} key - The key to use as index
39
+ */
40
+ export function generateIndex(data: Object, key?: string): any;
41
+ /**
42
+ * Generates a tree table from the input data
43
+ *
44
+ * @param {Object} data - The data to generate the tree table from
45
+ * @param {String} key - The key to use as index
46
+ * @param {Boolean} ellipsis - Whether to truncate the value
47
+ */
48
+ export function generateTreeTable(data: Object, key?: string, ellipsis?: boolean): any;
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,30 @@
1
+ /**
2
+ * Get a validator function that takes a regex expression and returns a validation function
3
+ *
4
+ * @param {string|RegExp} pattern
5
+ * @returns {import('./types').ValidationFunction}
6
+ */
7
+ export function getPatternValidator(pattern: string | RegExp): any;
8
+ /**
9
+ * Get a validator function that takes a min and max value and returns a validation function
10
+ *
11
+ * @param {number} min
12
+ * @param {number} max
13
+ * @returns {import('./types').ValidationFunction}
14
+ */
15
+ export function getRangeValidator(min: number, max: number): any;
16
+ /**
17
+ * Get a validator function that takes a type and returns a validation function
18
+ *
19
+ * @param {string} type
20
+ * @returns {import('./types').ValidationFunction}
21
+ * @throws {Error} - If the type is invalid
22
+ */
23
+ export function getTypeValidator(type: string): any;
24
+ /**
25
+ * Create a custom Svelte store with validation rules.
26
+ * @param {*} value - The initial value for the store.
27
+ * @param {Array<import('./types').ValidationRule>} rules - An array of validation rules.
28
+ * @returns {import('svelte').Writable<import('./types').ValidationResult>} - The custom Svelte store.
29
+ */
30
+ export function verifiable(input: any, rules: Array<any>): import("svelte").Writable<any>;
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,16 @@
1
+ /**
2
+ * Find an attribute in a schema by path
3
+ * @param {string} scope
4
+ * @param {import('../types').DataSchema} schema
5
+ * @returns {import('../types').LayoutSchema}
6
+ * @throws {Error} Invalid path
7
+ */
8
+ export function findAttributeByPath(scope: string, schema: import("../types").DataSchema): import("../types").LayoutSchema;
9
+ /**
10
+ * Get combined schema and layout
11
+ * @param {*} data
12
+ * @param {import('../types').DataSchema} schema
13
+ * @param {import('../types').LayoutSchema} layout
14
+ * @returns {import('../types').LayoutSchema}
15
+ */
16
+ export function getSchemaWithLayout(schema: import("../types").DataSchema, layout: import("../types").LayoutSchema): import("../types").LayoutSchema;
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,7 @@
1
+ export { FormBuilder } from "./builder.svelte.js";
2
+ export { default as FormRenderer } from "./FormRenderer.svelte";
3
+ export { default as Input } from "./Input.svelte";
4
+ export * from "./schema.js";
5
+ export * from "./layout.js";
6
+ export * from "./fields.js";
7
+ export { validateField, validateAll, createMessage, patterns } from "./validation.js";
@@ -0,0 +1,7 @@
1
+ /**
2
+ * Derives a layout from a given value.
3
+ * @param {any} value
4
+ * @param {string} scope
5
+ * @returns {import('../types').DataLayout}
6
+ */
7
+ export function deriveLayoutFromValue(value: any, scope?: string): import("../types").DataLayout;
@@ -0,0 +1,7 @@
1
+ /**
2
+ * Derives a schema from a given value.
3
+ *
4
+ * @param {any} data
5
+ * @returns {import('../types').DataSchema}
6
+ */
7
+ export function deriveSchemaFromValue(data: any): import("../types").DataSchema;
@@ -0,0 +1,41 @@
1
+ /**
2
+ * Create validation messages for informational purposes
3
+ * @param {string} fieldPath - Field path
4
+ * @param {string} state - Message state ('info', 'warning', 'success')
5
+ * @param {string} text - Message text
6
+ * @returns {Object} Field path to message mapping
7
+ */
8
+ export function createMessage(fieldPath: string, state: string, text: string): Object;
9
+ /**
10
+ * Validate a single field value against its schema
11
+ * @param {any} value - Field value to validate
12
+ * @param {Object} fieldSchema - Field schema definition
13
+ * @param {string} [fieldLabel] - Field label for error messages
14
+ * @returns {ValidationMessage|null} Validation message or null if valid
15
+ */
16
+ export function validateField(value: any, fieldSchema: Object, fieldLabel?: string): ValidationMessage | null;
17
+ /**
18
+ * Validate all fields in a data object
19
+ * @param {Object} data - Data object to validate
20
+ * @param {Object} schema - Schema object with properties
21
+ * @param {Object} layout - Layout object with element definitions
22
+ * @returns {Object} Validation results keyed by field path
23
+ */
24
+ export function validateAll(data: Object, schema: Object, layout: Object): Object;
25
+ export namespace patterns {
26
+ let email: RegExp;
27
+ let phone: RegExp;
28
+ let url: RegExp;
29
+ let zipCode: RegExp;
30
+ let creditCard: RegExp;
31
+ }
32
+ export type ValidationMessage = {
33
+ /**
34
+ * - Message state: 'error', 'warning', 'info', 'success'
35
+ */
36
+ state: string;
37
+ /**
38
+ * - Message text content
39
+ */
40
+ text: string;
41
+ };
@@ -0,0 +1,5 @@
1
+ export namespace types {
2
+ let string: import("svelte/legacy").LegacyComponentType;
3
+ let integer: import("svelte/legacy").LegacyComponentType;
4
+ let phone: import("svelte/legacy").LegacyComponentType;
5
+ }
package/package.json ADDED
@@ -0,0 +1,38 @@
1
+ {
2
+ "name": "@rokkit/forms",
3
+ "version": "1.0.0-next.122",
4
+ "module": "src/index.js",
5
+ "author": "Jerry Thomas <me@jerrythomas.name>",
6
+ "license": "MIT",
7
+ "type": "module",
8
+ "publishConfig": {
9
+ "access": "public"
10
+ },
11
+ "scripts": {
12
+ "prepublishOnly": "tsc --project tsconfig.build.json",
13
+ "clean": "rm -rf dist",
14
+ "build": "bun clean && bun prepublishOnly"
15
+ },
16
+ "files": [
17
+ "src/**/*.js",
18
+ "src/**/*.svelte",
19
+ "dist/**/*.d.ts"
20
+ ],
21
+ "exports": {
22
+ "./src": "./src",
23
+ "./package.json": "./package.json",
24
+ ".": {
25
+ "types": "./dist/index.d.ts",
26
+ "import": "./src/index.js",
27
+ "svelte": "./src/index.js"
28
+ }
29
+ },
30
+ "dependencies": {
31
+ "@rokkit/core": "latest",
32
+ "@rokkit/states": "latest",
33
+ "@rokkit/ui": "latest",
34
+ "@rokkit/bits-ui": "latest",
35
+ "ramda": "^0.30.1",
36
+ "valibot": "^1.1.0"
37
+ }
38
+ }
@@ -0,0 +1,30 @@
1
+ <script>
2
+ import { setContext } from 'svelte'
3
+ import { types } from './input/types'
4
+ import Wrapper from './wrappers/Wrapper.svelte'
5
+ import Item from './Item.svelte'
6
+ import Tabs from './Tabs.svelte'
7
+ import FieldLayout from './FieldLayout.svelte'
8
+ import { noop } from '@rokkit/core'
9
+
10
+ const registry = $state({})
11
+ setContext('registry', registry)
12
+
13
+ import { deriveSchemaFromValue, deriveLayoutFromValue, getSchemaWithLayout } from './lib'
14
+
15
+ let { value, schema = null, layout = null, using = {}, onchange = noop } = $props()
16
+
17
+ registry.editors = { ...types, ...using?.editors }
18
+ registry.components = { default: Item, ...using?.components }
19
+ registry.wrappers = { default: Wrapper, ...using?.wrappers }
20
+ registry.navigators = { default: Tabs, ...using?.navigators }
21
+
22
+ let schemaWithLayout = $derived.by(() => {
23
+ return getSchemaWithLayout(
24
+ schema ?? deriveSchemaFromValue(value),
25
+ layout ?? deriveLayoutFromValue(value)
26
+ )
27
+ })
28
+ </script>
29
+
30
+ <FieldLayout schema={schemaWithLayout} bind:value {onchange} />
@@ -0,0 +1,48 @@
1
+ <script>
2
+ import { getContext } from 'svelte'
3
+ import { omit } from 'ramda'
4
+ import InputField from './input/InputField.svelte'
5
+ import FieldLayout from './FieldLayout.svelte'
6
+
7
+ // const dispatch = createEventDispatcher()
8
+ const registry = getContext('registry')
9
+
10
+ export let value = {}
11
+ export let schema = {}
12
+ export let path = []
13
+
14
+ function handle() {
15
+ dispatch('change', value)
16
+ }
17
+
18
+ let Wrapper = registry.wrappers[schema.wrapper] ?? registry.wrappers.default
19
+ let wrapperProps = omit(['wrapper', 'elements', 'key'], schema)
20
+ </script>
21
+
22
+ {#if !Array.isArray(schema.elements)}
23
+ <error> Invalid schema. Expected schema to include an 'elements' array. </error>
24
+ {:else}
25
+ <Wrapper {...wrapperProps}>
26
+ {#each schema.elements as item, index (index)}
27
+ {@const elementPath = item.key ? [...path, item.key] : path}
28
+ {@const props = { ...item.props, path: elementPath }}
29
+ {@const nested = Array.isArray(item.elements) && item.elements.length > 0}
30
+ {@const Component = item.component
31
+ ? (registry.components[item.component] ?? registry.components.default)
32
+ : null}
33
+
34
+ {#if nested}
35
+ {#if item.key}
36
+ <FieldLayout {...props} schema={item} bind:value={value[item.key]} on:change={handle} />
37
+ {:else}
38
+ <FieldLayout {...props} schema={item} bind:value on:change={handle} />
39
+ {/if}
40
+ {:else if Component}
41
+ <Component {...item.props} value={item.key ? value[item.key] : null} />
42
+ {:else}
43
+ {@const name = elementPath.join('.')}
44
+ <InputField {name} bind:value={value[item.key]} {...item.props} on:change={handle} />
45
+ {/if}
46
+ {/each}
47
+ </Wrapper>
48
+ {/if}