@kayr/umbika-web-renderer 0.0.0-stage → 0.1.0

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 (77) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +46 -2
  3. package/THIRD_PARTY_LICENSES.txt +20 -0
  4. package/dist/browser/umbika.min.css +23 -0
  5. package/dist/browser/umbika.min.d.mts +437 -0
  6. package/dist/browser/umbika.min.d.ts +437 -0
  7. package/dist/browser/umbika.min.js +60 -0
  8. package/dist/browser/umbika.min.mjs +60 -0
  9. package/dist/casconda.d.ts +2 -0
  10. package/dist/casconda.d.ts.map +1 -0
  11. package/dist/contracts.d.ts +165 -0
  12. package/dist/contracts.d.ts.map +1 -0
  13. package/dist/dom-identity.d.ts +17 -0
  14. package/dist/dom-identity.d.ts.map +1 -0
  15. package/dist/field-presentation.d.ts +26 -0
  16. package/dist/field-presentation.d.ts.map +1 -0
  17. package/dist/index.d.ts +16 -0
  18. package/dist/index.d.ts.map +1 -0
  19. package/dist/layout.d.ts +25 -0
  20. package/dist/layout.d.ts.map +1 -0
  21. package/dist/mount.d.ts +14 -0
  22. package/dist/mount.d.ts.map +1 -0
  23. package/dist/render-nodes.d.ts +18 -0
  24. package/dist/render-nodes.d.ts.map +1 -0
  25. package/dist/renderers/multi-select.d.ts +18 -0
  26. package/dist/renderers/multi-select.d.ts.map +1 -0
  27. package/dist/renderers/object.d.ts +6 -0
  28. package/dist/renderers/object.d.ts.map +1 -0
  29. package/dist/renderers/output.d.ts +6 -0
  30. package/dist/renderers/output.d.ts.map +1 -0
  31. package/dist/renderers/select.d.ts +6 -0
  32. package/dist/renderers/select.d.ts.map +1 -0
  33. package/dist/renderers/table.d.ts +6 -0
  34. package/dist/renderers/table.d.ts.map +1 -0
  35. package/dist/renderers/text.d.ts +8 -0
  36. package/dist/renderers/text.d.ts.map +1 -0
  37. package/dist/row-details.d.ts +89 -0
  38. package/dist/row-details.d.ts.map +1 -0
  39. package/dist/standalone/contracts.d.ts +77 -0
  40. package/dist/standalone/contracts.d.ts.map +1 -0
  41. package/dist/standalone/index.d.ts +7 -0
  42. package/dist/standalone/index.d.ts.map +1 -0
  43. package/dist/standalone/mount.d.ts +9 -0
  44. package/dist/standalone/mount.d.ts.map +1 -0
  45. package/dist/summary.d.ts +29 -0
  46. package/dist/summary.d.ts.map +1 -0
  47. package/dist/tags.d.ts +32 -0
  48. package/dist/tags.d.ts.map +1 -0
  49. package/package.json +78 -4
  50. package/src/casconda.js +10 -0
  51. package/src/contracts.js +79 -0
  52. package/src/dom-identity.js +31 -0
  53. package/src/field-presentation.js +71 -0
  54. package/src/index.js +9 -0
  55. package/src/layout.js +176 -0
  56. package/src/mount.js +569 -0
  57. package/src/render-nodes.js +77 -0
  58. package/src/renderers/multi-select.js +211 -0
  59. package/src/renderers/object.js +43 -0
  60. package/src/renderers/output.js +87 -0
  61. package/src/renderers/select.js +104 -0
  62. package/src/renderers/table.js +344 -0
  63. package/src/renderers/text.js +82 -0
  64. package/src/row-details.js +305 -0
  65. package/src/standalone/contracts.js +22 -0
  66. package/src/standalone/index.js +5 -0
  67. package/src/standalone/mount.js +128 -0
  68. package/src/summary.js +135 -0
  69. package/src/tags.js +69 -0
  70. package/styles/base.css +432 -0
  71. package/styles/standalone.css +2 -0
  72. package/styles/themes/default.css +99 -0
  73. package/vendor/LICENSE +21 -0
  74. package/vendor/README.md +33 -0
  75. package/vendor/casconda.min.d.mts +340 -0
  76. package/vendor/casconda.min.d.ts +340 -0
  77. package/vendor/casconda.min.mjs +1 -0
@@ -0,0 +1,211 @@
1
+ import { computed, rx } from "../casconda.js";
2
+ import { _fieldDomId } from "../dom-identity.js";
3
+ import {
4
+ _fieldDescription,
5
+ _fieldPresentation,
6
+ } from "../field-presentation.js";
7
+ import { button, div, fieldset, foreach, input, label } from "../tags.js";
8
+
9
+ /**
10
+ * @typedef {Readonly<{
11
+ * value: string;
12
+ * label: string;
13
+ * unavailable: boolean;
14
+ * }>} MultiSelectChoice
15
+ */
16
+
17
+ /**
18
+ * @param {import("../contracts.js").KeyedItemResource<import("@kayr/umbika-core").ActiveFieldNode>} resource
19
+ * @param {import("../contracts.js").NodeRendererContext} context
20
+ */
21
+ export function _renderMultiSelectField(resource, context) {
22
+ const initial = resource.get();
23
+ if (!initial || initial.type !== "multi-select") return "";
24
+
25
+ const presentation = _fieldPresentation(
26
+ context.instanceId,
27
+ initial,
28
+ resource,
29
+ );
30
+ const choices = computed(() => _multiSelectChoices(resource.get()), resource);
31
+
32
+ return fieldset(
33
+ {
34
+ id: presentation.control,
35
+ class: "umbika-field umbika-multi-select",
36
+ "data-umbika-kind": "multi-select",
37
+ "data-umbika-required": rx.attr(
38
+ resource,
39
+ (
40
+ /** @type {import("@kayr/umbika-core").ActiveFieldNode | undefined} */ node,
41
+ ) => String(node?.type === "multi-select" && node.required),
42
+ ),
43
+ "data-umbika-invalid": rx.attr(
44
+ resource,
45
+ (
46
+ /** @type {import("@kayr/umbika-core").ActiveFieldNode | undefined} */ node,
47
+ ) => String(node?.type === "multi-select" && node.errors.length > 0),
48
+ ),
49
+ "aria-invalid": rx.attr(
50
+ resource,
51
+ (
52
+ /** @type {import("@kayr/umbika-core").ActiveFieldNode | undefined} */ node,
53
+ ) => String(node?.type === "multi-select" && node.errors.length > 0),
54
+ ),
55
+ "aria-describedby": presentation.describedBy,
56
+ },
57
+ presentation.legend,
58
+ _fieldDescription(
59
+ presentation.descriptionId,
60
+ "Choose any options that apply",
61
+ ),
62
+ div(
63
+ { class: "umbika-multi-select__choices" },
64
+ foreach(
65
+ { id: "value", resource: true },
66
+ choices,
67
+ (
68
+ /** @type {MultiSelectChoice} */ initialChoice,
69
+ /** @type {number} */ _index,
70
+ /** @type {import("../contracts.js").KeyedItemResource<MultiSelectChoice>} */ choiceResource,
71
+ ) =>
72
+ _renderChoice(
73
+ initial.path,
74
+ initialChoice,
75
+ choiceResource,
76
+ resource,
77
+ context,
78
+ ),
79
+ ),
80
+ ),
81
+ button(
82
+ {
83
+ type: "button",
84
+ class: "umbika-action umbika-multi-select__clear",
85
+ hidden: rx.prop(
86
+ resource,
87
+ (
88
+ /** @type {import("@kayr/umbika-core").ActiveFieldNode | undefined} */ node,
89
+ ) => node?.type !== "multi-select" || node.required,
90
+ ),
91
+ disabled: rx.prop(
92
+ resource,
93
+ (
94
+ /** @type {import("@kayr/umbika-core").ActiveFieldNode | undefined} */ node,
95
+ ) =>
96
+ node?.type !== "multi-select" ||
97
+ !Array.isArray(node.value) ||
98
+ node.value.length === 0,
99
+ ),
100
+ onclick: () => {
101
+ const current = resource.get();
102
+ if (!current || current.type !== "multi-select" || current.required)
103
+ return;
104
+ context.commitMultiSelect?.(current.path, []);
105
+ },
106
+ },
107
+ "Clear selection",
108
+ ),
109
+ presentation.errors,
110
+ );
111
+ }
112
+
113
+ /**
114
+ * @param {import("@kayr/umbika-core").FieldPath} initialPath
115
+ * @param {MultiSelectChoice} initialChoice
116
+ * @param {import("../contracts.js").KeyedItemResource<MultiSelectChoice>} choiceResource
117
+ * @param {import("../contracts.js").KeyedItemResource<import("@kayr/umbika-core").ActiveFieldNode>} fieldResource
118
+ * @param {import("../contracts.js").NodeRendererContext} context
119
+ */
120
+ function _renderChoice(
121
+ initialPath,
122
+ initialChoice,
123
+ choiceResource,
124
+ fieldResource,
125
+ context,
126
+ ) {
127
+ const controlId = _fieldDomId(context.instanceId, "choice", [
128
+ ...initialPath,
129
+ initialChoice.value,
130
+ ]);
131
+ return label(
132
+ {
133
+ class: rx.attr(choiceResource, (choice) =>
134
+ choice?.unavailable
135
+ ? "umbika-multi-select__choice umbika-multi-select__choice--unavailable"
136
+ : "umbika-multi-select__choice",
137
+ ),
138
+ "data-umbika-choice": initialChoice.value,
139
+ "data-umbika-unavailable": rx.attr(choiceResource, (choice) =>
140
+ String(choice?.unavailable === true),
141
+ ),
142
+ for: controlId,
143
+ },
144
+ input({
145
+ id: controlId,
146
+ class: "umbika-field__control umbika-multi-select__control",
147
+ type: "checkbox",
148
+ value: initialChoice.value,
149
+ checked: rx.prop(fieldResource, (node) =>
150
+ node?.type === "multi-select" && Array.isArray(node.value)
151
+ ? node.value.includes(initialChoice.value)
152
+ : false,
153
+ ),
154
+ onchange: (/** @type {Event} */ event) => {
155
+ const current = fieldResource.get();
156
+ const target = event.currentTarget;
157
+ if (
158
+ !current ||
159
+ current.type !== "multi-select" ||
160
+ !(target instanceof HTMLInputElement)
161
+ )
162
+ return;
163
+ const value = initialChoice.value;
164
+ const next = _nextSelection(current.value, value, target.checked);
165
+ context.commitMultiSelect?.(current.path, next);
166
+ },
167
+ }),
168
+ rx.text(choiceResource, (choice) =>
169
+ choice
170
+ ? `${choice.label}${choice.unavailable ? " — unavailable" : ""}`
171
+ : "",
172
+ ),
173
+ );
174
+ }
175
+
176
+ /**
177
+ * @param {unknown} currentValue
178
+ * @param {string} value
179
+ * @param {boolean} checked
180
+ */
181
+ function _nextSelection(currentValue, value, checked) {
182
+ const selected = Array.isArray(currentValue)
183
+ ? currentValue.filter(
184
+ (entry, index, values) =>
185
+ typeof entry === "string" && values.indexOf(entry) === index,
186
+ )
187
+ : [];
188
+ if (!checked) return selected.filter((entry) => entry !== value);
189
+ return selected.includes(value) ? selected : [...selected, value];
190
+ }
191
+
192
+ /**
193
+ * @param {import("@kayr/umbika-core").ActiveFieldNode | undefined} node
194
+ * @returns {readonly MultiSelectChoice[]}
195
+ */
196
+ function _multiSelectChoices(node) {
197
+ if (!node || node.type !== "multi-select") return [];
198
+ const available = new Set(node.options.map((option) => option.value));
199
+ /** @type {MultiSelectChoice[]} */
200
+ const choices = node.options.map((option) =>
201
+ Object.freeze({ ...option, unavailable: false }),
202
+ );
203
+ if (!Array.isArray(node.value)) return choices;
204
+ const represented = new Set(available);
205
+ for (const value of node.value) {
206
+ if (typeof value !== "string" || represented.has(value)) continue;
207
+ represented.add(value);
208
+ choices.push(Object.freeze({ value, label: value, unavailable: true }));
209
+ }
210
+ return choices;
211
+ }
@@ -0,0 +1,43 @@
1
+ import { computed, rx } from "../casconda.js";
2
+ import { _structuralDomId } from "../dom-identity.js";
3
+ import { _renderErrors } from "../field-presentation.js";
4
+ import { fieldset, legend } from "../tags.js";
5
+
6
+ /**
7
+ * @param {import("../contracts.js").KeyedItemResource<import("@kayr/umbika-core").ActiveFieldNode>} resource
8
+ * @param {import("../contracts.js").NodeRendererContext} context
9
+ */
10
+ export function _renderObjectField(resource, context) {
11
+ const initial = resource.get();
12
+ if (!initial || initial.type !== "object") return "";
13
+
14
+ const errorsId = _structuralDomId(context.instanceId, "errors", initial.path);
15
+ const children = computed(() => {
16
+ const current = resource.get();
17
+ return current?.type === "object" ? current.children : [];
18
+ }, resource);
19
+
20
+ return fieldset(
21
+ {
22
+ id: _structuralDomId(context.instanceId, "object", initial.path),
23
+ class: "umbika-object",
24
+ "data-umbika-kind": "object",
25
+ "aria-describedby": errorsId,
26
+ tabindex: "-1",
27
+ "data-umbika-invalid": rx.attr(resource, (node) =>
28
+ node?.type === "object" && node.errors.length > 0 ? "true" : "false",
29
+ ),
30
+ },
31
+ legend(
32
+ { class: "umbika-object__heading" },
33
+ rx.text(
34
+ resource,
35
+ (
36
+ /** @type {import("@kayr/umbika-core").ActiveFieldNode | undefined} */ node,
37
+ ) => (node?.type === "object" ? (node.label ?? node.id) : ""),
38
+ ),
39
+ ),
40
+ _renderErrors(errorsId, resource),
41
+ context.renderNodes(children, context.definitionPath),
42
+ );
43
+ }
@@ -0,0 +1,87 @@
1
+ import { rx } from "../casconda.js";
2
+ import {
3
+ _displayValue,
4
+ _fieldDescription,
5
+ _fieldPresentation,
6
+ } from "../field-presentation.js";
7
+ import { div, output } from "../tags.js";
8
+
9
+ /**
10
+ * @param {import("../contracts.js").KeyedItemResource<import("@kayr/umbika-core").ActiveFieldNode>} resource
11
+ * @param {import("../contracts.js").NodeRendererContext} context
12
+ */
13
+ export function _renderFieldOutput(resource, context) {
14
+ const initial = resource.get();
15
+ if (!initial || !("value" in initial)) return "";
16
+
17
+ const calculated = initial.type === "calculated";
18
+ const presentation = _fieldPresentation(
19
+ context.instanceId,
20
+ initial,
21
+ resource,
22
+ );
23
+ return div(
24
+ {
25
+ class: calculated
26
+ ? "umbika-field umbika-field--calculated"
27
+ : "umbika-field",
28
+ "data-umbika-kind": initial.type,
29
+ "data-umbika-readonly": "true",
30
+ },
31
+ presentation.label,
32
+ _fieldDescription(
33
+ presentation.descriptionId,
34
+ calculated ? "Calculated automatically" : "Read only",
35
+ ),
36
+ output(
37
+ {
38
+ id: presentation.control,
39
+ class: calculated ? "umbika-calculated" : "umbika-field__output",
40
+ tabindex: "-1",
41
+ "aria-live": "polite",
42
+ "aria-invalid": rx.attr(
43
+ resource,
44
+ (
45
+ /** @type {import("@kayr/umbika-core").ActiveFieldNode | undefined} */ node,
46
+ ) => String(node && "value" in node && node.errors.length > 0),
47
+ ),
48
+ "aria-describedby": presentation.describedBy,
49
+ },
50
+ rx.text(
51
+ resource,
52
+ (
53
+ /** @type {import("@kayr/umbika-core").ActiveFieldNode | undefined} */ node,
54
+ ) => (node && "value" in node ? _outputValue(node) : ""),
55
+ ),
56
+ ),
57
+ presentation.errors,
58
+ );
59
+ }
60
+
61
+ /** @param {import("@kayr/umbika-core").ActiveField} node */
62
+ function _outputValue(node) {
63
+ if (
64
+ node.type === "select" &&
65
+ typeof node.value === "string" &&
66
+ node.value !== ""
67
+ ) {
68
+ return _choiceLabel(node.options, node.value);
69
+ }
70
+ if (node.type === "multi-select" && Array.isArray(node.value)) {
71
+ return _displayValue(
72
+ node.value.map((value) => _choiceLabel(node.options, value)).join(", "),
73
+ );
74
+ }
75
+ return _displayValue(node.value);
76
+ }
77
+
78
+ /**
79
+ * @param {readonly import("@kayr/umbika-core").SelectOption[]} options
80
+ * @param {unknown} value
81
+ */
82
+ function _choiceLabel(options, value) {
83
+ return (
84
+ options.find((option) => option.value === value)?.label ??
85
+ `${String(value)} (unavailable)`
86
+ );
87
+ }
@@ -0,0 +1,104 @@
1
+ import { computed, ref, rx } from "../casconda.js";
2
+ import {
3
+ _fieldDescription,
4
+ _fieldPresentation,
5
+ } from "../field-presentation.js";
6
+ import { div, foreach, option, select } from "../tags.js";
7
+
8
+ /**
9
+ * @param {import("../contracts.js").KeyedItemResource<import("@kayr/umbika-core").ActiveFieldNode>} resource
10
+ * @param {import("../contracts.js").NodeRendererContext} context
11
+ */
12
+ export function _renderSelectField(resource, context) {
13
+ const initial = resource.get();
14
+ if (!initial || initial.type !== "select") return "";
15
+
16
+ const presentation = _fieldPresentation(
17
+ context.instanceId,
18
+ initial,
19
+ resource,
20
+ );
21
+ const draft = ref(_selectDisplayValue(initial.value));
22
+ const options = computed(() => {
23
+ const current = resource.get();
24
+ return current?.type === "select" ? current.options : [];
25
+ }, resource);
26
+
27
+ draft.subscribe((/** @type {unknown} */ value) => {
28
+ const current = resource.get();
29
+ if (!current || current.type !== "select") return;
30
+ context.commitSelect?.(
31
+ current.path,
32
+ String(value) === ""
33
+ ? undefined
34
+ : current.options.find((option) => option.value === String(value)),
35
+ );
36
+ });
37
+
38
+ return div(
39
+ {
40
+ class: "umbika-field",
41
+ "data-umbika-kind": "select",
42
+ },
43
+ presentation.label,
44
+ _fieldDescription(presentation.descriptionId, "Choose one option"),
45
+ select(
46
+ {
47
+ id: presentation.control,
48
+ class: "umbika-field__control",
49
+ bind: {
50
+ ref: draft,
51
+ source: resource,
52
+ read: (
53
+ /** @type {import("@kayr/umbika-core").ActiveFieldNode | undefined} */ node,
54
+ ) => (node?.type === "select" ? _selectDisplayValue(node.value) : ""),
55
+ event: "change",
56
+ },
57
+ required: rx.prop(
58
+ resource,
59
+ (
60
+ /** @type {import("@kayr/umbika-core").ActiveFieldNode | undefined} */ node,
61
+ ) => (node?.type === "select" ? node.required : false),
62
+ ),
63
+ "aria-required": rx.attr(
64
+ resource,
65
+ (
66
+ /** @type {import("@kayr/umbika-core").ActiveFieldNode | undefined} */ node,
67
+ ) => String(node?.type === "select" && node.required),
68
+ ),
69
+ "aria-invalid": rx.attr(
70
+ resource,
71
+ (
72
+ /** @type {import("@kayr/umbika-core").ActiveFieldNode | undefined} */ node,
73
+ ) => String(node?.type === "select" && node.errors.length > 0),
74
+ ),
75
+ "aria-describedby": presentation.describedBy,
76
+ },
77
+ option({ value: "" }, "Select…"),
78
+ foreach(
79
+ { id: "value", resource: true },
80
+ options,
81
+ (
82
+ /** @type {import("@kayr/umbika-core").SelectOption} */ initialOption,
83
+ /** @type {number} */ _index,
84
+ /** @type {import("../contracts.js").KeyedItemResource<import("@kayr/umbika-core").SelectOption>} */ optionResource,
85
+ ) =>
86
+ option(
87
+ { value: initialOption.value },
88
+ rx.text(
89
+ optionResource,
90
+ (
91
+ /** @type {import("@kayr/umbika-core").SelectOption | undefined} */ entry,
92
+ ) => entry?.label ?? "",
93
+ ),
94
+ ),
95
+ ),
96
+ ),
97
+ presentation.errors,
98
+ );
99
+ }
100
+
101
+ /** @param {unknown} value */
102
+ function _selectDisplayValue(value) {
103
+ return value === undefined || value === null ? "" : String(value);
104
+ }