vanjs-jsf 0.5.1 → 0.6.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.
- package/README.md +136 -15
- package/dist/VanJsfField.d.ts +40 -57
- package/dist/VanJsfField.js +124 -566
- package/dist/VanJsfField.js.map +1 -1
- package/dist/VanJsfForm.d.ts +82 -1
- package/dist/VanJsfForm.js +213 -119
- package/dist/VanJsfForm.js.map +1 -1
- package/dist/index.d.ts +5 -1
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -1
- package/dist/jsf-defaults.css +80 -0
- package/dist/schema.d.ts +13 -0
- package/dist/schema.js +29 -0
- package/dist/schema.js.map +1 -0
- package/dist/theme.d.ts +20 -2
- package/dist/theme.js +12 -1
- package/dist/theme.js.map +1 -1
- package/dist/types.d.ts +83 -0
- package/dist/types.js +2 -0
- package/dist/types.js.map +1 -0
- package/dist/util.d.ts +10 -0
- package/dist/util.js +17 -0
- package/dist/util.js.map +1 -0
- package/dist/values.d.ts +11 -0
- package/dist/values.js +51 -0
- package/dist/values.js.map +1 -0
- package/dist/widgets/array.d.ts +16 -0
- package/dist/widgets/array.js +108 -0
- package/dist/widgets/array.js.map +1 -0
- package/dist/widgets/basic.d.ts +2 -0
- package/dist/widgets/basic.js +97 -0
- package/dist/widgets/basic.js.map +1 -0
- package/dist/widgets/choice.d.ts +2 -0
- package/dist/widgets/choice.js +127 -0
- package/dist/widgets/choice.js.map +1 -0
- package/dist/widgets/code.d.ts +2 -0
- package/dist/widgets/code.js +146 -0
- package/dist/widgets/code.js.map +1 -0
- package/dist/widgets/date.d.ts +3 -0
- package/dist/widgets/date.js +43 -0
- package/dist/widgets/date.js.map +1 -0
- package/dist/widgets/file.d.ts +6 -0
- package/dist/widgets/file.js +130 -0
- package/dist/widgets/file.js.map +1 -0
- package/dist/widgets/index.d.ts +3 -0
- package/dist/widgets/index.js +22 -0
- package/dist/widgets/index.js.map +1 -0
- package/package.json +19 -17
- package/dist/JsfUtils.d.ts +0 -4
- package/dist/JsfUtils.js +0 -11
- package/dist/JsfUtils.js.map +0 -1
- package/dist/VanJSComponent.d.ts +0 -3
- package/dist/VanJSComponent.js +0 -3
- package/dist/VanJSComponent.js.map +0 -1
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import van from "vanjs-core";
|
|
2
|
+
import { resolve } from "../theme";
|
|
3
|
+
import { schemaOf } from "../util";
|
|
4
|
+
const { button, div, fieldset, input, span } = van.tags;
|
|
5
|
+
export const arrayBounds = (field) => {
|
|
6
|
+
const schema = schemaOf(field);
|
|
7
|
+
const pick = (k) => typeof field[k] === "number" ? field[k] : typeof schema[k] === "number" ? schema[k] : undefined;
|
|
8
|
+
return { min: pick("minItems") ?? 0, max: pick("maxItems") ?? Infinity };
|
|
9
|
+
};
|
|
10
|
+
export const itemTypeOf = (field) => {
|
|
11
|
+
const items = (field.items ?? schemaOf(field).items ?? {});
|
|
12
|
+
return items.type ?? "string";
|
|
13
|
+
};
|
|
14
|
+
/** Value of a new scalar item. */
|
|
15
|
+
export const blankItem = (itemType) => itemType === "number" || itemType === "integer" ? 0 : itemType === "boolean" ? false : "";
|
|
16
|
+
/** Templates of a group-array item, fresh objects on every call. */
|
|
17
|
+
export const itemTemplates = (field) => typeof field.fields === "function" ? field.fields() : field.fields ?? [];
|
|
18
|
+
/** A new group-array item, with the defaults its sub-fields declare. */
|
|
19
|
+
export const blankGroupItem = (field) => Object.fromEntries(itemTemplates(field).filter((t) => t.default !== undefined).map((t) => [t.name, t.default]));
|
|
20
|
+
const groupTitle = (ctx) => div({ id: `${ctx.id}-label`, class: resolve(ctx.field.titleClass, ctx.theme.legend || ctx.theme.label) }, ctx.field.label ?? "", ctx.field.required ? span({ class: ctx.theme.requiredIndicator || "" }, " *") : null);
|
|
21
|
+
const currentItems = (ctx) => {
|
|
22
|
+
const v = ctx.getValue();
|
|
23
|
+
return Array.isArray(v) ? v : [];
|
|
24
|
+
};
|
|
25
|
+
const addLabel = (ctx) => typeof ctx.field.addFieldText === "string" ? ctx.field.addFieldText : ctx.texts.add;
|
|
26
|
+
/**
|
|
27
|
+
* Shared shell of both list widgets. Rows re-render only when an item is added or removed,
|
|
28
|
+
* never while typing, so an input keeps its focus. `rows` receives `bump` to re-render after
|
|
29
|
+
* a removal.
|
|
30
|
+
*/
|
|
31
|
+
function listWidget(ctx, rows, blank) {
|
|
32
|
+
const { max } = arrayBounds(ctx.field);
|
|
33
|
+
const version = van.state(0);
|
|
34
|
+
const bump = () => { version.val++; };
|
|
35
|
+
const add = () => {
|
|
36
|
+
const items = currentItems(ctx);
|
|
37
|
+
if (items.length >= max)
|
|
38
|
+
return;
|
|
39
|
+
ctx.setValue([...items, blank()]);
|
|
40
|
+
bump();
|
|
41
|
+
};
|
|
42
|
+
return [
|
|
43
|
+
groupTitle(ctx),
|
|
44
|
+
ctx.description(),
|
|
45
|
+
() => {
|
|
46
|
+
void version.val;
|
|
47
|
+
return div({ class: ctx.theme.arrayItems || "jsf-array-items", role: "group", "aria-labelledby": `${ctx.id}-label` }, rows(bump));
|
|
48
|
+
},
|
|
49
|
+
button({
|
|
50
|
+
type: "button",
|
|
51
|
+
class: ctx.theme.arrayAddButton || "jsf-array-add",
|
|
52
|
+
disabled: () => { void version.val; return isLocked(ctx) || currentItems(ctx).length >= max; },
|
|
53
|
+
onclick: add,
|
|
54
|
+
}, addLabel(ctx)),
|
|
55
|
+
ctx.errorMessage(),
|
|
56
|
+
];
|
|
57
|
+
}
|
|
58
|
+
const isLocked = (ctx) => Boolean(ctx.field.readOnly || ctx.field.disabled);
|
|
59
|
+
const removeButton = (ctx, index, bump) => button({
|
|
60
|
+
type: "button",
|
|
61
|
+
class: ctx.theme.arrayRemoveButton || "jsf-array-remove",
|
|
62
|
+
disabled: isLocked(ctx) || currentItems(ctx).length <= arrayBounds(ctx.field).min,
|
|
63
|
+
"aria-label": `${ctx.texts.remove} ${index + 1}`,
|
|
64
|
+
onclick: () => {
|
|
65
|
+
const next = currentItems(ctx).slice();
|
|
66
|
+
next.splice(index, 1);
|
|
67
|
+
ctx.setValue(next);
|
|
68
|
+
bump();
|
|
69
|
+
},
|
|
70
|
+
}, ctx.texts.remove);
|
|
71
|
+
/** Repeatable list of scalars (string, number, integer, boolean); the value is a real array. */
|
|
72
|
+
export const arrayWidget = (ctx) => {
|
|
73
|
+
const itemType = itemTypeOf(ctx.field);
|
|
74
|
+
const isNumeric = itemType === "number" || itemType === "integer";
|
|
75
|
+
const isBoolean = itemType === "boolean";
|
|
76
|
+
const itemClass = resolve(ctx.field.class, ctx.theme.input);
|
|
77
|
+
const update = (i, v) => {
|
|
78
|
+
const next = currentItems(ctx).slice();
|
|
79
|
+
next[i] = v;
|
|
80
|
+
ctx.setValue(next);
|
|
81
|
+
};
|
|
82
|
+
const rowInput = (i, v) => isBoolean
|
|
83
|
+
? input({
|
|
84
|
+
type: "checkbox",
|
|
85
|
+
id: `${ctx.id}-${i}`,
|
|
86
|
+
name: `${ctx.path.join(".")}.${i}`,
|
|
87
|
+
class: itemClass,
|
|
88
|
+
checked: Boolean(v),
|
|
89
|
+
disabled: isLocked(ctx),
|
|
90
|
+
onchange: (e) => update(i, e.target.checked),
|
|
91
|
+
})
|
|
92
|
+
: input({
|
|
93
|
+
type: isNumeric ? "number" : "text",
|
|
94
|
+
id: `${ctx.id}-${i}`,
|
|
95
|
+
name: `${ctx.path.join(".")}.${i}`,
|
|
96
|
+
class: itemClass,
|
|
97
|
+
value: v === undefined || v === null ? "" : String(v),
|
|
98
|
+
readOnly: isLocked(ctx),
|
|
99
|
+
oninput: (e) => {
|
|
100
|
+
const raw = e.target.value;
|
|
101
|
+
update(i, isNumeric ? (raw === "" ? "" : Number(raw)) : raw);
|
|
102
|
+
},
|
|
103
|
+
});
|
|
104
|
+
return listWidget(ctx, (bump) => currentItems(ctx).map((v, i) => div({ class: ctx.theme.arrayRow || "jsf-array-row" }, rowInput(i, v), removeButton(ctx, i, bump))), () => blankItem(itemType));
|
|
105
|
+
};
|
|
106
|
+
/** Repeatable list of objects: each item renders its sub-fields as a fieldset. */
|
|
107
|
+
export const groupArrayWidget = (ctx) => listWidget(ctx, (bump) => ctx.renderItems().map((nodes, i) => fieldset({ class: ctx.theme.arrayGroupItem || "jsf-array-group-item" }, ...nodes, removeButton(ctx, i, bump))), () => blankGroupItem(ctx.field));
|
|
108
|
+
//# sourceMappingURL=array.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"array.js","sourceRoot":"","sources":["../../lib/widgets/array.ts"],"names":[],"mappings":"AAAA,OAAO,GAAiB,MAAM,YAAY,CAAC;AAE3C,OAAO,EAAE,OAAO,EAAE,MAAM,UAAU,CAAC;AACnC,OAAO,EAAE,QAAQ,EAAE,MAAM,SAAS,CAAC;AAEnC,MAAM,EAAE,MAAM,EAAE,GAAG,EAAE,QAAQ,EAAE,KAAK,EAAE,IAAI,EAAE,GAAG,GAAG,CAAC,IAAI,CAAC;AAIxD,MAAM,CAAC,MAAM,WAAW,GAAG,CAAC,KAAe,EAAgC,EAAE;IAC3E,MAAM,MAAM,GAAG,QAAQ,CAAC,KAAK,CAAC,CAAC;IAC/B,MAAM,IAAI,GAAG,CAAC,CAA0B,EAAE,EAAE,CAC1C,OAAO,KAAK,CAAC,CAAC,CAAC,KAAK,QAAQ,CAAC,CAAC,CAAE,KAAK,CAAC,CAAC,CAAY,CAAC,CAAC,CAAC,OAAO,MAAM,CAAC,CAAC,CAAC,KAAK,QAAQ,CAAC,CAAC,CAAE,MAAM,CAAC,CAAC,CAAY,CAAC,CAAC,CAAC,SAAS,CAAC;IAC1H,OAAO,EAAE,GAAG,EAAE,IAAI,CAAC,UAAU,CAAC,IAAI,CAAC,EAAE,GAAG,EAAE,IAAI,CAAC,UAAU,CAAC,IAAI,QAAQ,EAAE,CAAC;AAC3E,CAAC,CAAC;AAEF,MAAM,CAAC,MAAM,UAAU,GAAG,CAAC,KAAe,EAAU,EAAE;IACpD,MAAM,KAAK,GAAG,CAAC,KAAK,CAAC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AAQ,IAAI,eAAe,EAAE,EAAE,QAAQ,CAAC,CAAC,EAAE,CAAC,CAAC,EAAE,YAAY,CAAC,GAAG,EAAE,CAAC,EAAE,IAAI,CAAC,CAAC,CAAC,EACpG,GAAG,EAAE,CAAC,SAAS,CAAC,QAAQ,CAAC,CAC1B,CAAC;AACJ,CAAC,CAAC;AAEF,kFAAkF;AAClF,MAAM,CAAC,MAAM,gBAAgB,GAAW,CAAC,GAAG,EAAE,EAAE,CAAC,UAAU,CACzD,GAAG,EACH,CAAC,IAAI,EAAE,EAAE,CAAC,GAAG,CAAC,WAAW,EAAE,CAAC,GAAG,CAAC,CAAC,KAAK,EAAE,CAAC,EAAE,EAAE,CAC3C,QAAQ,CAAC,EAAE,KAAK,EAAE,GAAG,CAAC,KAAK,CAAC,cAAc,IAAI,sBAAsB,EAAE,EAAE,GAAG,KAAK,EAAE,YAAY,CAAC,GAAG,EAAE,CAAC,EAAE,IAAI,CAAC,CAAC,CAAC,EAChH,GAAG,EAAE,CAAC,cAAc,CAAC,GAAG,CAAC,KAAK,CAAC,CAChC,CAAC","sourcesContent":["import van, { ChildDom } from \"vanjs-core\";\nimport type { JsfField, Widget, WidgetContext } from \"../types\";\nimport { resolve } from \"../theme\";\nimport { schemaOf } from \"../util\";\n\nconst { button, div, fieldset, input, span } = van.tags;\n\ntype ItemSchema = { type?: string };\n\nexport const arrayBounds = (field: JsfField): { min: number; max: number } => {\n const schema = schemaOf(field);\n const pick = (k: \"minItems\" | \"maxItems\") =>\n typeof field[k] === \"number\" ? (field[k] as number) : typeof schema[k] === \"number\" ? (schema[k] as number) : undefined;\n return { min: pick(\"minItems\") ?? 0, max: pick(\"maxItems\") ?? Infinity };\n};\n\nexport const itemTypeOf = (field: JsfField): string => {\n const items = (field.items ?? schemaOf(field).items ?? {}) as ItemSchema;\n return items.type ?? \"string\";\n};\n\n/** Value of a new scalar item. */\nexport const blankItem = (itemType: string): unknown =>\n itemType === \"number\" || itemType === \"integer\" ? 0 : itemType === \"boolean\" ? false : \"\";\n\n/** Templates of a group-array item, fresh objects on every call. */\nexport const itemTemplates = (field: JsfField): JsfField[] =>\n typeof field.fields === \"function\" ? field.fields() : field.fields ?? [];\n\n/** A new group-array item, with the defaults its sub-fields declare. */\nexport const blankGroupItem = (field: JsfField): Record<string, unknown> =>\n Object.fromEntries(itemTemplates(field).filter((t) => t.default !== undefined).map((t) => [t.name, t.default]));\n\nconst groupTitle = (ctx: WidgetContext) =>\n div(\n { id: `${ctx.id}-label`, class: resolve(ctx.field.titleClass, ctx.theme.legend || ctx.theme.label) },\n ctx.field.label ?? \"\",\n ctx.field.required ? span({ class: ctx.theme.requiredIndicator || \"\" }, \" *\") : null,\n );\n\nconst currentItems = (ctx: WidgetContext): unknown[] => {\n const v = ctx.getValue();\n return Array.isArray(v) ? v : [];\n};\n\nconst addLabel = (ctx: WidgetContext) =>\n typeof ctx.field.addFieldText === \"string\" ? ctx.field.addFieldText : ctx.texts.add;\n\n/**\n * Shared shell of both list widgets. Rows re-render only when an item is added or removed,\n * never while typing, so an input keeps its focus. `rows` receives `bump` to re-render after\n * a removal.\n */\nfunction listWidget(ctx: WidgetContext, rows: (bump: () => void) => Element[], blank: () => unknown): ChildDom[] {\n const { max } = arrayBounds(ctx.field);\n const version = van.state(0);\n const bump = () => { version.val++; };\n const add = () => {\n const items = currentItems(ctx);\n if (items.length >= max) return;\n ctx.setValue([...items, blank()]);\n bump();\n };\n return [\n groupTitle(ctx),\n ctx.description(),\n () => {\n void version.val;\n return div(\n { class: ctx.theme.arrayItems || \"jsf-array-items\", role: \"group\", \"aria-labelledby\": `${ctx.id}-label` },\n rows(bump),\n );\n },\n button({\n type: \"button\",\n class: ctx.theme.arrayAddButton || \"jsf-array-add\",\n disabled: () => { void version.val; return isLocked(ctx) || currentItems(ctx).length >= max; },\n onclick: add,\n }, addLabel(ctx)),\n ctx.errorMessage(),\n ];\n}\n\nconst isLocked = (ctx: WidgetContext) => Boolean(ctx.field.readOnly || ctx.field.disabled);\n\nconst removeButton = (ctx: WidgetContext, index: number, bump: () => void) => button({\n type: \"button\",\n class: ctx.theme.arrayRemoveButton || \"jsf-array-remove\",\n disabled: isLocked(ctx) || currentItems(ctx).length <= arrayBounds(ctx.field).min,\n \"aria-label\": `${ctx.texts.remove} ${index + 1}`,\n onclick: () => {\n const next = currentItems(ctx).slice();\n next.splice(index, 1);\n ctx.setValue(next);\n bump();\n },\n}, ctx.texts.remove);\n\n/** Repeatable list of scalars (string, number, integer, boolean); the value is a real array. */\nexport const arrayWidget: Widget = (ctx) => {\n const itemType = itemTypeOf(ctx.field);\n const isNumeric = itemType === \"number\" || itemType === \"integer\";\n const isBoolean = itemType === \"boolean\";\n const itemClass = resolve(ctx.field.class, ctx.theme.input);\n\n const update = (i: number, v: unknown) => {\n const next = currentItems(ctx).slice();\n next[i] = v;\n ctx.setValue(next);\n };\n\n const rowInput = (i: number, v: unknown): Element => isBoolean\n ? input({\n type: \"checkbox\",\n id: `${ctx.id}-${i}`,\n name: `${ctx.path.join(\".\")}.${i}`,\n class: itemClass,\n checked: Boolean(v),\n disabled: isLocked(ctx),\n onchange: (e: Event) => update(i, (e.target as HTMLInputElement).checked),\n })\n : input({\n type: isNumeric ? \"number\" : \"text\",\n id: `${ctx.id}-${i}`,\n name: `${ctx.path.join(\".\")}.${i}`,\n class: itemClass,\n value: v === undefined || v === null ? \"\" : String(v),\n readOnly: isLocked(ctx),\n oninput: (e: Event) => {\n const raw = (e.target as HTMLInputElement).value;\n update(i, isNumeric ? (raw === \"\" ? \"\" : Number(raw)) : raw);\n },\n });\n\n return listWidget(\n ctx,\n (bump) => currentItems(ctx).map((v, i) =>\n div({ class: ctx.theme.arrayRow || \"jsf-array-row\" }, rowInput(i, v), removeButton(ctx, i, bump))),\n () => blankItem(itemType),\n );\n};\n\n/** Repeatable list of objects: each item renders its sub-fields as a fieldset. */\nexport const groupArrayWidget: Widget = (ctx) => listWidget(\n ctx,\n (bump) => ctx.renderItems().map((nodes, i) =>\n fieldset({ class: ctx.theme.arrayGroupItem || \"jsf-array-group-item\" }, ...nodes, removeButton(ctx, i, bump))),\n () => blankGroupItem(ctx.field),\n);\n"]}
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import van from "vanjs-core";
|
|
2
|
+
import { resolve } from "../theme";
|
|
3
|
+
import { emptyToUndefined } from "../values";
|
|
4
|
+
import { compact, num, toText } from "../util";
|
|
5
|
+
const { input, textarea, output, div } = van.tags;
|
|
6
|
+
const target = (e) => e.target;
|
|
7
|
+
const textInput = (type, extra = () => ({})) => (ctx) => [
|
|
8
|
+
ctx.label(),
|
|
9
|
+
ctx.description(),
|
|
10
|
+
input(compact({
|
|
11
|
+
...ctx.attrs(),
|
|
12
|
+
type,
|
|
13
|
+
class: resolve(ctx.field.class, ctx.theme.input),
|
|
14
|
+
value: toText(ctx.getValue()),
|
|
15
|
+
maxLength: num(ctx.field.maxLength),
|
|
16
|
+
...extra(ctx),
|
|
17
|
+
oninput: (e) => ctx.setValue(emptyToUndefined(target(e).value)),
|
|
18
|
+
})),
|
|
19
|
+
ctx.errorMessage(),
|
|
20
|
+
];
|
|
21
|
+
const numberInput = (ctx) => {
|
|
22
|
+
const f = ctx.field;
|
|
23
|
+
return [
|
|
24
|
+
ctx.label(),
|
|
25
|
+
ctx.description(),
|
|
26
|
+
input(compact({
|
|
27
|
+
...ctx.attrs(),
|
|
28
|
+
type: "number",
|
|
29
|
+
class: resolve(f.class, ctx.theme.input),
|
|
30
|
+
value: toText(ctx.getValue()),
|
|
31
|
+
min: num(f.minimum),
|
|
32
|
+
max: num(f.maximum),
|
|
33
|
+
step: f.step ?? (f.jsonType === "integer" ? 1 : "any"),
|
|
34
|
+
oninput: (e) => {
|
|
35
|
+
const raw = target(e).value;
|
|
36
|
+
ctx.setValue(raw === "" ? undefined : Number(raw));
|
|
37
|
+
},
|
|
38
|
+
})),
|
|
39
|
+
ctx.errorMessage(),
|
|
40
|
+
];
|
|
41
|
+
};
|
|
42
|
+
/** A slider always shows a position, so the form starts with a value (see initialValue). */
|
|
43
|
+
const rangeInput = (ctx) => {
|
|
44
|
+
const f = ctx.field;
|
|
45
|
+
const current = van.state(toText(ctx.getValue()));
|
|
46
|
+
return [
|
|
47
|
+
ctx.label(),
|
|
48
|
+
ctx.description(),
|
|
49
|
+
div({ class: "jsf-range" }, input(compact({
|
|
50
|
+
...ctx.attrs(),
|
|
51
|
+
type: "range",
|
|
52
|
+
class: resolve(f.class, ctx.theme.input),
|
|
53
|
+
value: current.rawVal,
|
|
54
|
+
min: num(f.minimum),
|
|
55
|
+
max: num(f.maximum),
|
|
56
|
+
step: f.step ?? (f.jsonType === "integer" ? 1 : "any"),
|
|
57
|
+
oninput: (e) => {
|
|
58
|
+
current.val = target(e).value;
|
|
59
|
+
ctx.setValue(Number(current.rawVal));
|
|
60
|
+
},
|
|
61
|
+
})), output({ for: ctx.id, class: ctx.theme.rangeOutput || "jsf-range-output" }, current)),
|
|
62
|
+
ctx.errorMessage(),
|
|
63
|
+
];
|
|
64
|
+
};
|
|
65
|
+
const textareaInput = (ctx) => [
|
|
66
|
+
ctx.label(),
|
|
67
|
+
ctx.description(),
|
|
68
|
+
textarea(compact({
|
|
69
|
+
...ctx.attrs(),
|
|
70
|
+
class: resolve(ctx.field.class, ctx.theme.textarea),
|
|
71
|
+
rows: num(ctx.field.rows),
|
|
72
|
+
cols: num(ctx.field.columns),
|
|
73
|
+
maxLength: num(ctx.field.maxLength),
|
|
74
|
+
value: toText(ctx.getValue()),
|
|
75
|
+
oninput: (e) => ctx.setValue(emptyToUndefined(e.target.value)),
|
|
76
|
+
})),
|
|
77
|
+
ctx.errorMessage(),
|
|
78
|
+
];
|
|
79
|
+
const hiddenInput = (ctx) => input({ type: "hidden", id: ctx.id, name: ctx.path.join("."), value: toText(ctx.getValue()) });
|
|
80
|
+
export const basicWidgets = {
|
|
81
|
+
text: textInput("text"),
|
|
82
|
+
password: textInput("password", (ctx) => ({ autocomplete: ctx.field.autocomplete ?? "new-password" })),
|
|
83
|
+
email: textInput("email", (ctx) => ({ autocomplete: ctx.field.autocomplete ?? "email" })),
|
|
84
|
+
url: textInput("url"),
|
|
85
|
+
tel: textInput("tel"),
|
|
86
|
+
search: textInput("search"),
|
|
87
|
+
color: textInput("color"),
|
|
88
|
+
time: textInput("time"),
|
|
89
|
+
"datetime-local": textInput("datetime-local"),
|
|
90
|
+
month: textInput("month"),
|
|
91
|
+
week: textInput("week"),
|
|
92
|
+
number: numberInput,
|
|
93
|
+
range: rangeInput,
|
|
94
|
+
textarea: textareaInput,
|
|
95
|
+
hidden: hiddenInput,
|
|
96
|
+
};
|
|
97
|
+
//# sourceMappingURL=basic.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"basic.js","sourceRoot":"","sources":["../../lib/widgets/basic.ts"],"names":[],"mappings":"AAAA,OAAO,GAAG,MAAM,YAAY,CAAC;AAE7B,OAAO,EAAE,OAAO,EAAE,MAAM,UAAU,CAAC;AACnC,OAAO,EAAE,gBAAgB,EAAE,MAAM,WAAW,CAAC;AAC7C,OAAO,EAAE,OAAO,EAAE,GAAG,EAAE,MAAM,EAAE,MAAM,SAAS,CAAC;AAE/C,MAAM,EAAE,KAAK,EAAE,QAAQ,EAAE,MAAM,EAAE,GAAG,EAAE,GAAG,GAAG,CAAC,IAAI,CAAC;AAElD,MAAM,MAAM,GAAG,CAAC,CAAQ,EAAE,EAAE,CAAC,CAAC,CAAC,MAA0B,CAAC;AAE1D,MAAM,SAAS,GAAG,CAAC,IAAY,EAAE,QAAyD,GAAG,EAAE,CAAC,CAAC,EAAE,CAAC,EAAU,EAAE,CAC9G,CAAC,GAAG,EAAE,EAAE,CAAC;IACP,GAAG,CAAC,KAAK,EAAE;IACX,GAAG,CAAC,WAAW,EAAE;IACjB,KAAK,CAAC,OAAO,CAAC;QACZ,GAAG,GAAG,CAAC,KAAK,EAAE;QACd,IAAI;QACJ,KAAK,EAAE,OAAO,CAAC,GAAG,CAAC,KAAK,CAAC,KAAK,E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,QAAQ,EAAE,SAAS,CAAC,UAAU,EAAE,CAAC,GAAG,EAAE,EAAE,CAAC,CAAC,EAAE,YAAY,EAAE,GAAG,CAAC,KAAK,CAAC,YAAY,IAAI,cAAc,EAAE,CAAC,CAAC;IACtG,KAAK,EAAE,SAAS,CAAC,OAAO,EAAE,CAAC,GAAG,EAAE,EAAE,CAAC,CAAC,EAAE,YAAY,EAAE,GAAG,CAAC,KAAK,CAAC,YAAY,IAAI,OAAO,EAAE,CAAC,CAAC;IACzF,GAAG,EAAE,SAAS,CAAC,KAAK,CAAC;IACrB,GAAG,EAAE,SAAS,CAAC,KAAK,CAAC;IACrB,MAAM,EAAE,SAAS,CAAC,QAAQ,CAAC;IAC3B,KAAK,EAAE,SAAS,CAAC,OAAO,CAAC;IACzB,IAAI,EAAE,SAAS,CAAC,MAAM,CAAC;IACvB,gBAAgB,EAAE,SAAS,CAAC,gBAAgB,CAAC;IAC7C,KAAK,EAAE,SAAS,CAAC,OAAO,CAAC;IACzB,IAAI,EAAE,SAAS,CAAC,MAAM,CAAC;IACvB,MAAM,EAAE,WAAW;IACnB,KAAK,EAAE,UAAU;IACjB,QAAQ,EAAE,aAAa;IACvB,MAAM,EAAE,WAAW;CACpB,CAAC","sourcesContent":["import van from \"vanjs-core\";\nimport type { Widget, WidgetContext } from \"../types\";\nimport { resolve } from \"../theme\";\nimport { emptyToUndefined } from \"../values\";\nimport { compact, num, toText } from \"../util\";\n\nconst { input, textarea, output, div } = van.tags;\n\nconst target = (e: Event) => e.target as HTMLInputElement;\n\nconst textInput = (type: string, extra: (ctx: WidgetContext) => Record<string, unknown> = () => ({})): Widget =>\n (ctx) => [\n ctx.label(),\n ctx.description(),\n input(compact({\n ...ctx.attrs(),\n type,\n class: resolve(ctx.field.class, ctx.theme.input),\n value: toText(ctx.getValue()),\n maxLength: num(ctx.field.maxLength),\n ...extra(ctx),\n oninput: (e: Event) => ctx.setValue(emptyToUndefined(target(e).value)),\n })),\n ctx.errorMessage(),\n ];\n\nconst numberInput: Widget = (ctx) => {\n const f = ctx.field;\n return [\n ctx.label(),\n ctx.description(),\n input(compact({\n ...ctx.attrs(),\n type: \"number\",\n class: resolve(f.class, ctx.theme.input),\n value: toText(ctx.getValue()),\n min: num(f.minimum),\n max: num(f.maximum),\n step: f.step ?? (f.jsonType === \"integer\" ? 1 : \"any\"),\n oninput: (e: Event) => {\n const raw = target(e).value;\n ctx.setValue(raw === \"\" ? undefined : Number(raw));\n },\n })),\n ctx.errorMessage(),\n ];\n};\n\n/** A slider always shows a position, so the form starts with a value (see initialValue). */\nconst rangeInput: Widget = (ctx) => {\n const f = ctx.field;\n const current = van.state(toText(ctx.getValue()));\n return [\n ctx.label(),\n ctx.description(),\n div({ class: \"jsf-range\" },\n input(compact({\n ...ctx.attrs(),\n type: \"range\",\n class: resolve(f.class, ctx.theme.input),\n value: current.rawVal,\n min: num(f.minimum),\n max: num(f.maximum),\n step: f.step ?? (f.jsonType === \"integer\" ? 1 : \"any\"),\n oninput: (e: Event) => {\n current.val = target(e).value;\n ctx.setValue(Number(current.rawVal));\n },\n })),\n output({ for: ctx.id, class: ctx.theme.rangeOutput || \"jsf-range-output\" }, current),\n ),\n ctx.errorMessage(),\n ];\n};\n\nconst textareaInput: Widget = (ctx) => [\n ctx.label(),\n ctx.description(),\n textarea(compact({\n ...ctx.attrs(),\n class: resolve(ctx.field.class, ctx.theme.textarea),\n rows: num(ctx.field.rows),\n cols: num(ctx.field.columns),\n maxLength: num(ctx.field.maxLength),\n value: toText(ctx.getValue()),\n oninput: (e: Event) => ctx.setValue(emptyToUndefined((e.target as HTMLTextAreaElement).value)),\n })),\n ctx.errorMessage(),\n];\n\nconst hiddenInput: Widget = (ctx) =>\n input({ type: \"hidden\", id: ctx.id, name: ctx.path.join(\".\"), value: toText(ctx.getValue()) });\n\nexport const basicWidgets: Record<string, Widget> = {\n text: textInput(\"text\"),\n password: textInput(\"password\", (ctx) => ({ autocomplete: ctx.field.autocomplete ?? \"new-password\" })),\n email: textInput(\"email\", (ctx) => ({ autocomplete: ctx.field.autocomplete ?? \"email\" })),\n url: textInput(\"url\"),\n tel: textInput(\"tel\"),\n search: textInput(\"search\"),\n color: textInput(\"color\"),\n time: textInput(\"time\"),\n \"datetime-local\": textInput(\"datetime-local\"),\n month: textInput(\"month\"),\n week: textInput(\"week\"),\n number: numberInput,\n range: rangeInput,\n textarea: textareaInput,\n hidden: hiddenInput,\n};\n"]}
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
import van from "vanjs-core";
|
|
2
|
+
import { resolve } from "../theme";
|
|
3
|
+
import { compact, optionLookup } from "../util";
|
|
4
|
+
const { div, input, label, option, select, small, span } = van.tags;
|
|
5
|
+
const optionsOf = (ctx) => ctx.field.options ?? [];
|
|
6
|
+
/** readOnly has no effect on selects, radios or checkboxes: they are disabled instead. */
|
|
7
|
+
const isLocked = (ctx) => Boolean(ctx.field.readOnly || ctx.field.disabled);
|
|
8
|
+
const requiredMark = (ctx) => ctx.field.required ? span({ class: ctx.theme.requiredIndicator || "" }, " *") : null;
|
|
9
|
+
/** Title of a group of controls (radios, checkboxes): a label element would point at only one of them. */
|
|
10
|
+
const groupTitle = (ctx) => div({ id: `${ctx.id}-label`, class: resolve(ctx.field.titleClass, ctx.theme.legend || ctx.theme.label) }, ctx.field.label ?? "", requiredMark(ctx));
|
|
11
|
+
const groupAria = (ctx, role) => compact({
|
|
12
|
+
role,
|
|
13
|
+
"aria-labelledby": `${ctx.id}-label`,
|
|
14
|
+
"aria-describedby": ctx.attrs()["aria-describedby"],
|
|
15
|
+
"aria-required": ctx.field.required ? "true" : undefined,
|
|
16
|
+
});
|
|
17
|
+
const optionDescription = (o) => o.description ? small({ class: "jsf-option-description" }, ` ${o.description}`) : null;
|
|
18
|
+
const selectWidget = (ctx) => {
|
|
19
|
+
const opts = optionsOf(ctx);
|
|
20
|
+
const lookup = optionLookup(opts);
|
|
21
|
+
const current = ctx.getValue();
|
|
22
|
+
const placeholder = typeof ctx.field.placeholder === "string" ? ctx.field.placeholder : ctx.texts.selectPlaceholder;
|
|
23
|
+
const el = select(compact({
|
|
24
|
+
...ctx.attrs(),
|
|
25
|
+
placeholder: undefined,
|
|
26
|
+
readOnly: undefined,
|
|
27
|
+
disabled: isLocked(ctx) || undefined,
|
|
28
|
+
class: resolve(ctx.field.class, ctx.theme.select),
|
|
29
|
+
onchange: (e) => ctx.setValue(lookup(e.target.value)),
|
|
30
|
+
}),
|
|
31
|
+
// Without an empty option the browser shows the first one as chosen while the value is unset.
|
|
32
|
+
option({ class: ctx.theme.option || "", value: "", selected: current === undefined }, placeholder), opts.map((o) => option(compact({
|
|
33
|
+
class: ctx.theme.option || "",
|
|
34
|
+
value: String(o.value),
|
|
35
|
+
selected: current !== undefined && String(o.value) === String(current),
|
|
36
|
+
disabled: o.disabled || undefined,
|
|
37
|
+
title: o.description,
|
|
38
|
+
}), o.label)));
|
|
39
|
+
// `selected` on a detached option is not honoured everywhere; set the value once mounted in the select.
|
|
40
|
+
el.value = current === undefined ? "" : String(current);
|
|
41
|
+
return [ctx.label(), ctx.description(), el, ctx.errorMessage()];
|
|
42
|
+
};
|
|
43
|
+
const radioWidget = (ctx) => {
|
|
44
|
+
const current = ctx.getValue();
|
|
45
|
+
return [
|
|
46
|
+
groupTitle(ctx),
|
|
47
|
+
ctx.description(),
|
|
48
|
+
div({ ...groupAria(ctx, "radiogroup"), class: ctx.theme.radioGroup || "" }, optionsOf(ctx).map((o, i) => label({ class: ctx.theme.radioLabel || "" }, input(compact({
|
|
49
|
+
type: "radio",
|
|
50
|
+
id: i === 0 ? ctx.id : `${ctx.id}-${i}`,
|
|
51
|
+
name: ctx.path.join("."),
|
|
52
|
+
class: resolve(ctx.field.class, ctx.theme.radioInput),
|
|
53
|
+
value: String(o.value),
|
|
54
|
+
checked: current !== undefined && String(o.value) === String(current),
|
|
55
|
+
disabled: isLocked(ctx) || o.disabled || undefined,
|
|
56
|
+
"aria-invalid": () => (ctx.error.val ? "true" : "false"),
|
|
57
|
+
onchange: () => ctx.setValue(o.value),
|
|
58
|
+
})), o.label, optionDescription(o)))),
|
|
59
|
+
ctx.errorMessage(),
|
|
60
|
+
];
|
|
61
|
+
};
|
|
62
|
+
/**
|
|
63
|
+
* A single checkbox. A boolean field stores true/false; a checkbox over a `const` stores that
|
|
64
|
+
* value when checked and nothing when not. `variant: "switch"` renders it as a toggle.
|
|
65
|
+
*/
|
|
66
|
+
const checkboxWidget = (ctx) => {
|
|
67
|
+
const f = ctx.field;
|
|
68
|
+
const isBoolean = f.jsonType === "boolean" || f.checkboxValue === true || f.checkboxValue === undefined;
|
|
69
|
+
const onValue = isBoolean ? true : f.checkboxValue;
|
|
70
|
+
const isSwitch = f.variant === "switch";
|
|
71
|
+
const inputClass = [resolve(f.class, ctx.theme.checkboxInput), isSwitch ? "jsf-switch" : ""]
|
|
72
|
+
.filter(Boolean).join(" ");
|
|
73
|
+
return [
|
|
74
|
+
label({ class: resolve(f.titleClass, ctx.theme.checkboxLabel || "jsf-checkbox-label") }, input(compact({
|
|
75
|
+
...ctx.attrs(),
|
|
76
|
+
placeholder: undefined,
|
|
77
|
+
readOnly: undefined,
|
|
78
|
+
disabled: isLocked(ctx) || undefined,
|
|
79
|
+
type: "checkbox",
|
|
80
|
+
role: isSwitch ? "switch" : undefined,
|
|
81
|
+
class: inputClass,
|
|
82
|
+
checked: ctx.getValue() === onValue,
|
|
83
|
+
onchange: (e) => {
|
|
84
|
+
const checked = e.target.checked;
|
|
85
|
+
ctx.setValue(checked ? onValue : isBoolean ? false : undefined);
|
|
86
|
+
},
|
|
87
|
+
})), span(f.label ?? ""), requiredMark(ctx)),
|
|
88
|
+
ctx.description(),
|
|
89
|
+
ctx.errorMessage(),
|
|
90
|
+
];
|
|
91
|
+
};
|
|
92
|
+
/** An array whose items come from a fixed list: one checkbox per option, the value is the chosen values in option order. */
|
|
93
|
+
const multiselectWidget = (ctx) => {
|
|
94
|
+
const opts = optionsOf(ctx);
|
|
95
|
+
const current = ctx.getValue();
|
|
96
|
+
const chosen = new Set((Array.isArray(current) ? current : []).map(String));
|
|
97
|
+
const emit = () => ctx.setValue(opts.filter((o) => chosen.has(String(o.value))).map((o) => o.value));
|
|
98
|
+
return [
|
|
99
|
+
groupTitle(ctx),
|
|
100
|
+
ctx.description(),
|
|
101
|
+
div({ ...groupAria(ctx, "group"), class: ctx.theme.checkboxGroup || "jsf-checkbox-group" }, opts.map((o, i) => label({ class: ctx.theme.checkboxLabel || "jsf-checkbox-label" }, input(compact({
|
|
102
|
+
type: "checkbox",
|
|
103
|
+
id: i === 0 ? ctx.id : `${ctx.id}-${i}`,
|
|
104
|
+
name: ctx.path.join("."),
|
|
105
|
+
class: resolve(ctx.field.class, ctx.theme.checkboxInput),
|
|
106
|
+
value: String(o.value),
|
|
107
|
+
checked: chosen.has(String(o.value)),
|
|
108
|
+
disabled: isLocked(ctx) || o.disabled || undefined,
|
|
109
|
+
onchange: (e) => {
|
|
110
|
+
const key = String(o.value);
|
|
111
|
+
if (e.target.checked)
|
|
112
|
+
chosen.add(key);
|
|
113
|
+
else
|
|
114
|
+
chosen.delete(key);
|
|
115
|
+
emit();
|
|
116
|
+
},
|
|
117
|
+
})), span(o.label), optionDescription(o)))),
|
|
118
|
+
ctx.errorMessage(),
|
|
119
|
+
];
|
|
120
|
+
};
|
|
121
|
+
export const choiceWidgets = {
|
|
122
|
+
select: selectWidget,
|
|
123
|
+
radio: radioWidget,
|
|
124
|
+
checkbox: checkboxWidget,
|
|
125
|
+
multiselect: multiselectWidget,
|
|
126
|
+
};
|
|
127
|
+
//# sourceMappingURL=choice.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"choice.js","sourceRoot":"","sources":["../../lib/widgets/choice.ts"],"names":[],"mappings":"AAAA,OAAO,GAAG,MAAM,YAAY,CAAC;AAE7B,OAAO,EAAE,OAAO,EAAE,MAAM,UAAU,CAAC;AACnC,OAAO,EAAE,OAAO,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;A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AAC,GAAG,CAAC,EAAE,CAAC,CAAC,CAAC,GAAG,GAAG,CAAC,EAAE,IAAI,CAAC,EAAE;YACvC,IAAI,EAAE,GAAG,CAAC,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC;YACxB,KAAK,EAAE,OAAO,CAAC,GAAG,CAAC,KAAK,CAAC,KAAK,EAAE,GAAG,CAAC,KAAK,CAAC,aAAa,CAAC;YACxD,KAAK,EAAE,MAAM,CAAC,CAAC,CAAC,KAAK,CAAC;YACtB,OAAO,EAAE,MAAM,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC;YACpC,QAAQ,EAAE,QAAQ,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC,QAAQ,IAAI,SAAS;YAClD,QAAQ,EAAE,CAAC,CAAQ,EAAE,EAAE;gBACrB,MAAM,GAAG,GAAG,MAAM,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC;gBAC5B,IAAK,CAAC,CAAC,MAA2B,CAAC,OAAO;oBAAE,MAAM,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;;oBACvD,MAAM,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;gBACxB,IAAI,EAAE,CAAC;YACT,CAAC;SACF,CAAC,CAAC,EACH,IAAI,CAAC,CAAC,CAAC,KAAK,CAAC,EACb,iBAAiB,CAAC,CAAC,CAAC,CACrB,CACF,CACF;QACD,GAAG,CAAC,YAAY,EAAE;KACnB,CAAC;AACJ,CAAC,CAAC;AAEF,MAAM,CAAC,MAAM,aAAa,GAA2B;IACnD,MAAM,EAAE,YAAY;IACpB,KAAK,EAAE,WAAW;IAClB,QAAQ,EAAE,cAAc;IACxB,WAAW,EAAE,iBAAiB;CAC/B,CAAC","sourcesContent":["import van from \"vanjs-core\";\nimport type { Option, Widget, WidgetContext } from \"../types\";\nimport { resolve } from \"../theme\";\nimport { compact, optionLookup } from \"../util\";\n\nconst { div, input, label, option, select, small, span } = van.tags;\n\nconst optionsOf = (ctx: WidgetContext): Option[] => ctx.field.options ?? [];\n\n/** readOnly has no effect on selects, radios or checkboxes: they are disabled instead. */\nconst isLocked = (ctx: WidgetContext) => Boolean(ctx.field.readOnly || ctx.field.disabled);\n\nconst requiredMark = (ctx: WidgetContext) =>\n ctx.field.required ? span({ class: ctx.theme.requiredIndicator || \"\" }, \" *\") : null;\n\n/** Title of a group of controls (radios, checkboxes): a label element would point at only one of them. */\nconst groupTitle = (ctx: WidgetContext) =>\n div(\n { id: `${ctx.id}-label`, class: resolve(ctx.field.titleClass, ctx.theme.legend || ctx.theme.label) },\n ctx.field.label ?? \"\",\n requiredMark(ctx),\n );\n\nconst groupAria = (ctx: WidgetContext, role: string) => compact({\n role,\n \"aria-labelledby\": `${ctx.id}-label`,\n \"aria-describedby\": ctx.attrs()[\"aria-describedby\"],\n \"aria-required\": ctx.field.required ? \"true\" : undefined,\n});\n\nconst optionDescription = (o: Option) =>\n o.description ? small({ class: \"jsf-option-description\" }, ` ${o.description}`) : null;\n\nconst selectWidget: Widget = (ctx) => {\n const opts = optionsOf(ctx);\n const lookup = optionLookup(opts);\n const current = ctx.getValue();\n const placeholder = typeof ctx.field.placeholder === \"string\" ? ctx.field.placeholder : ctx.texts.selectPlaceholder;\n const el = select(\n compact({\n ...ctx.attrs(),\n placeholder: undefined,\n readOnly: undefined,\n disabled: isLocked(ctx) || undefined,\n class: resolve(ctx.field.class, ctx.theme.select),\n onchange: (e: Event) => ctx.setValue(lookup((e.target as HTMLSelectElement).value)),\n }),\n // Without an empty option the browser shows the first one as chosen while the value is unset.\n option({ class: ctx.theme.option || \"\", value: \"\", selected: current === undefined }, placeholder),\n opts.map((o) =>\n option(compact({\n class: ctx.theme.option || \"\",\n value: String(o.value),\n selected: current !== undefined && String(o.value) === String(current),\n disabled: o.disabled || undefined,\n title: o.description,\n }), o.label),\n ),\n ) as HTMLSelectElement;\n // `selected` on a detached option is not honoured everywhere; set the value once mounted in the select.\n el.value = current === undefined ? \"\" : String(current);\n return [ctx.label(), ctx.description(), el, ctx.errorMessage()];\n};\n\nconst radioWidget: Widget = (ctx) => {\n const current = ctx.getValue();\n return [\n groupTitle(ctx),\n ctx.description(),\n div(\n { ...groupAria(ctx, \"radiogroup\"), class: ctx.theme.radioGroup || \"\" },\n optionsOf(ctx).map((o, i) =>\n label(\n { class: ctx.theme.radioLabel || \"\" },\n input(compact({\n type: \"radio\",\n id: i === 0 ? ctx.id : `${ctx.id}-${i}`,\n name: ctx.path.join(\".\"),\n class: resolve(ctx.field.class, ctx.theme.radioInput),\n value: String(o.value),\n checked: current !== undefined && String(o.value) === String(current),\n disabled: isLocked(ctx) || o.disabled || undefined,\n \"aria-invalid\": () => (ctx.error.val ? \"true\" : \"false\"),\n onchange: () => ctx.setValue(o.value),\n })),\n o.label,\n optionDescription(o),\n ),\n ),\n ),\n ctx.errorMessage(),\n ];\n};\n\n/**\n * A single checkbox. A boolean field stores true/false; a checkbox over a `const` stores that\n * value when checked and nothing when not. `variant: \"switch\"` renders it as a toggle.\n */\nconst checkboxWidget: Widget = (ctx) => {\n const f = ctx.field;\n const isBoolean = f.jsonType === \"boolean\" || f.checkboxValue === true || f.checkboxValue === undefined;\n const onValue = isBoolean ? true : f.checkboxValue;\n const isSwitch = f.variant === \"switch\";\n const inputClass = [resolve(f.class, ctx.theme.checkboxInput), isSwitch ? \"jsf-switch\" : \"\"]\n .filter(Boolean).join(\" \");\n return [\n label(\n { class: resolve(f.titleClass, ctx.theme.checkboxLabel || \"jsf-checkbox-label\") },\n input(compact({\n ...ctx.attrs(),\n placeholder: undefined,\n readOnly: undefined,\n disabled: isLocked(ctx) || undefined,\n type: \"checkbox\",\n role: isSwitch ? \"switch\" : undefined,\n class: inputClass,\n checked: ctx.getValue() === onValue,\n onchange: (e: Event) => {\n const checked = (e.target as HTMLInputElement).checked;\n ctx.setValue(checked ? onValue : isBoolean ? false : undefined);\n },\n })),\n span(f.label ?? \"\"),\n requiredMark(ctx),\n ),\n ctx.description(),\n ctx.errorMessage(),\n ];\n};\n\n/** An array whose items come from a fixed list: one checkbox per option, the value is the chosen values in option order. */\nconst multiselectWidget: Widget = (ctx) => {\n const opts = optionsOf(ctx);\n const current = ctx.getValue();\n const chosen = new Set((Array.isArray(current) ? current : []).map(String));\n const emit = () => ctx.setValue(opts.filter((o) => chosen.has(String(o.value))).map((o) => o.value));\n return [\n groupTitle(ctx),\n ctx.description(),\n div(\n { ...groupAria(ctx, \"group\"), class: ctx.theme.checkboxGroup || \"jsf-checkbox-group\" },\n opts.map((o, i) =>\n label(\n { class: ctx.theme.checkboxLabel || \"jsf-checkbox-label\" },\n input(compact({\n type: \"checkbox\",\n id: i === 0 ? ctx.id : `${ctx.id}-${i}`,\n name: ctx.path.join(\".\"),\n class: resolve(ctx.field.class, ctx.theme.checkboxInput),\n value: String(o.value),\n checked: chosen.has(String(o.value)),\n disabled: isLocked(ctx) || o.disabled || undefined,\n onchange: (e: Event) => {\n const key = String(o.value);\n if ((e.target as HTMLInputElement).checked) chosen.add(key);\n else chosen.delete(key);\n emit();\n },\n })),\n span(o.label),\n optionDescription(o),\n ),\n ),\n ),\n ctx.errorMessage(),\n ];\n};\n\nexport const choiceWidgets: Record<string, Widget> = {\n select: selectWidget,\n radio: radioWidget,\n checkbox: checkboxWidget,\n multiselect: multiselectWidget,\n};\n"]}
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
import van from "vanjs-core";
|
|
2
|
+
const { div } = van.tags;
|
|
3
|
+
/**
|
|
4
|
+
* Rules the `code` field lints with. These are ESLint's own `eslint:recommended` set, spelled
|
|
5
|
+
* out because `eslint-linter-browserify` ships no config resolver — a bare `Linter` with no
|
|
6
|
+
* `rules` reports parse errors and nothing else, which looks like a working editor while
|
|
7
|
+
* catching none of the mistakes people actually make.
|
|
8
|
+
*
|
|
9
|
+
* Stylistic rules are deliberately absent. A host embedding this editor has no say in the
|
|
10
|
+
* code style of whoever types into it, and flagging every semicolon as an error is worse than
|
|
11
|
+
* flagging nothing: the real diagnostics drown.
|
|
12
|
+
*/
|
|
13
|
+
const ESLINT_RULES = {
|
|
14
|
+
"constructor-super": "error",
|
|
15
|
+
"for-direction": "error",
|
|
16
|
+
"getter-return": "error",
|
|
17
|
+
"no-async-promise-executor": "error",
|
|
18
|
+
"no-case-declarations": "error",
|
|
19
|
+
"no-class-assign": "error",
|
|
20
|
+
"no-compare-neg-zero": "error",
|
|
21
|
+
"no-cond-assign": "error",
|
|
22
|
+
"no-const-assign": "error",
|
|
23
|
+
"no-constant-binary-expression": "error",
|
|
24
|
+
"no-constant-condition": "error",
|
|
25
|
+
"no-control-regex": "error",
|
|
26
|
+
"no-debugger": "error",
|
|
27
|
+
"no-delete-var": "error",
|
|
28
|
+
"no-dupe-args": "error",
|
|
29
|
+
"no-dupe-class-members": "error",
|
|
30
|
+
"no-dupe-else-if": "error",
|
|
31
|
+
"no-dupe-keys": "error",
|
|
32
|
+
"no-duplicate-case": "error",
|
|
33
|
+
"no-empty": "error",
|
|
34
|
+
"no-empty-character-class": "error",
|
|
35
|
+
"no-empty-pattern": "error",
|
|
36
|
+
"no-empty-static-block": "error",
|
|
37
|
+
"no-ex-assign": "error",
|
|
38
|
+
"no-extra-boolean-cast": "error",
|
|
39
|
+
"no-fallthrough": "error",
|
|
40
|
+
"no-func-assign": "error",
|
|
41
|
+
"no-global-assign": "error",
|
|
42
|
+
"no-import-assign": "error",
|
|
43
|
+
"no-invalid-regexp": "error",
|
|
44
|
+
"no-irregular-whitespace": "error",
|
|
45
|
+
"no-loss-of-precision": "error",
|
|
46
|
+
"no-misleading-character-class": "error",
|
|
47
|
+
"no-new-native-nonconstructor": "error",
|
|
48
|
+
"no-nonoctal-decimal-escape": "error",
|
|
49
|
+
"no-obj-calls": "error",
|
|
50
|
+
"no-octal": "error",
|
|
51
|
+
"no-prototype-builtins": "error",
|
|
52
|
+
"no-redeclare": "error",
|
|
53
|
+
"no-regex-spaces": "error",
|
|
54
|
+
"no-self-assign": "error",
|
|
55
|
+
"no-setter-return": "error",
|
|
56
|
+
"no-shadow-restricted-names": "error",
|
|
57
|
+
"no-sparse-arrays": "error",
|
|
58
|
+
"no-this-before-super": "error",
|
|
59
|
+
"no-undef": "error",
|
|
60
|
+
"no-unexpected-multiline": "error",
|
|
61
|
+
"no-unreachable": "error",
|
|
62
|
+
"no-unsafe-finally": "error",
|
|
63
|
+
"no-unsafe-negation": "error",
|
|
64
|
+
"no-unsafe-optional-chaining": "error",
|
|
65
|
+
"no-unused-labels": "error",
|
|
66
|
+
"no-unused-private-class-members": "error",
|
|
67
|
+
"no-unused-vars": "error",
|
|
68
|
+
"no-useless-backreference": "error",
|
|
69
|
+
"no-useless-catch": "error",
|
|
70
|
+
"no-useless-escape": "error",
|
|
71
|
+
"no-with": "error",
|
|
72
|
+
"require-yield": "error",
|
|
73
|
+
"use-isnan": "error",
|
|
74
|
+
"valid-typeof": "error",
|
|
75
|
+
};
|
|
76
|
+
/**
|
|
77
|
+
* Loads CodeMirror + ESLint on demand and mounts the editor into `parent`. These are the
|
|
78
|
+
* heaviest dependencies in the library; importing them here keeps them out of the bundle of
|
|
79
|
+
* forms that have no `code` field.
|
|
80
|
+
*/
|
|
81
|
+
async function mountCodeEditor(ctx, parent) {
|
|
82
|
+
const [{ basicSetup, EditorView }, { javascript, esLint }, { json, jsonParseLinter }, { lintGutter, linter, forEachDiagnostic }, eslint, globalsModule, { dracula },] = await Promise.all([
|
|
83
|
+
import("codemirror"),
|
|
84
|
+
import("@codemirror/lang-javascript"),
|
|
85
|
+
import("@codemirror/lang-json"),
|
|
86
|
+
import("@codemirror/lint"),
|
|
87
|
+
import("eslint-linter-browserify"),
|
|
88
|
+
import("globals"),
|
|
89
|
+
import("thememirror"),
|
|
90
|
+
]);
|
|
91
|
+
const f = ctx.field;
|
|
92
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
93
|
+
const globals = globalsModule.default ?? globalsModule;
|
|
94
|
+
// A `code` field may declare the globals its host injects at runtime. Those names are in
|
|
95
|
+
// no environment preset, so without this every single one of them reads as `no-undef`.
|
|
96
|
+
const fieldGlobals = f.globals && typeof f.globals === "object" ? f.globals : {};
|
|
97
|
+
const eslintConfig = {
|
|
98
|
+
languageOptions: {
|
|
99
|
+
globals: { ...fieldGlobals, ...globals.node },
|
|
100
|
+
parserOptions: { ecmaVersion: 2022, sourceType: "module" },
|
|
101
|
+
},
|
|
102
|
+
rules: ESLINT_RULES,
|
|
103
|
+
};
|
|
104
|
+
const cmTheme = EditorView.theme({
|
|
105
|
+
".cm-content, .cm-gutter": { "min-height": "150px" },
|
|
106
|
+
".cm-gutters": { margin: "1px" },
|
|
107
|
+
".cm-scroller": { overflow: "auto" },
|
|
108
|
+
});
|
|
109
|
+
const lang = f.codemirrorType === "json"
|
|
110
|
+
? [json(), linter(jsonParseLinter())]
|
|
111
|
+
: [
|
|
112
|
+
javascript({ typescript: f.codemirrorType === "typescript" }),
|
|
113
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
114
|
+
linter(esLint(new eslint.Linter(), eslintConfig)),
|
|
115
|
+
];
|
|
116
|
+
new EditorView({
|
|
117
|
+
doc: typeof ctx.getValue() === "string" ? ctx.getValue() : "",
|
|
118
|
+
parent,
|
|
119
|
+
extensions: [
|
|
120
|
+
dracula,
|
|
121
|
+
cmTheme,
|
|
122
|
+
basicSetup,
|
|
123
|
+
lintGutter(),
|
|
124
|
+
...lang,
|
|
125
|
+
EditorView.editable.of(!(f.readOnly || f.disabled)),
|
|
126
|
+
// Diagnostics arrive in their own transaction after the linter runs, not with the edit,
|
|
127
|
+
// so every update re-reads them and only edits emit a new value.
|
|
128
|
+
EditorView.updateListener.of((u) => {
|
|
129
|
+
let lintError = "";
|
|
130
|
+
forEachDiagnostic(u.state, (d) => {
|
|
131
|
+
if (!lintError && d.severity === "error")
|
|
132
|
+
lintError = d.message;
|
|
133
|
+
});
|
|
134
|
+
if (u.docChanged)
|
|
135
|
+
ctx.setValue(u.state.doc.toString() || undefined);
|
|
136
|
+
ctx.setWidgetError(lintError);
|
|
137
|
+
}),
|
|
138
|
+
],
|
|
139
|
+
});
|
|
140
|
+
}
|
|
141
|
+
export const codeWidget = (ctx) => {
|
|
142
|
+
const editorHost = div({ id: ctx.id, class: "jsf-code" });
|
|
143
|
+
void mountCodeEditor(ctx, editorHost);
|
|
144
|
+
return [ctx.label(), ctx.description(), editorHost, ctx.errorMessage()];
|
|
145
|
+
};
|
|
146
|
+
//# sourceMappingURL=code.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"code.js","sourceRoot":"","sources":["../../lib/widgets/code.ts"],"names":[],"mappings":"AAAA,OAAO,GAAG,MAAM,YAAY,CAAC;AAG7B,MAAM,EAAE,GAAG,EAAE,GAAG,GAAG,CAAC,IAAI,CAAC;AAEzB;;;;;;;;;GASG;AACH,MAAM,YAAY,GAA4B;IAC5C,mBAAmB,EAAE,OAAO;IAC5B,eAAe,EAAE,OAAO;IACxB,eAAe,EAAE,OAAO;IACxB,2BAA2B,EAAE,OAAO;IACpC,sBAAsB,EAAE,OAAO;IAC/B,iBAAiB,EAAE,OAAO;IAC1B,qBAAqB,EAAE,OAAO;IAC9B,gBAAgB,EAAE,OAAO;IACzB,iBAAiB,EAAE,OAAO;IAC1B,+BAA+B,EAAE,OAAO;IACxC,uBAAuB,EAAE,OAAO;IAChC,kBAAkB,EAAE,OAAO;IAC3B,aAAa,EAAE,OAAO;IACtB,eAAe,EAAE,OAAO;IACxB,cAAc,EAAE,OAAO;IACvB,uBAAuB,EAAE,OAAO;IAChC,iBAAiB,EAAE,OAAO;IAC1B,cAAc,EAAE,OAAO;IACvB,mBAAmB,EAAE,OAAO;IAC5B,UAAU,EAAE,OAAO;IACnB,0BAA0B,EAAE,OAAO;IACnC,kBAAkB,EAAE,OAAO;IAC3B,uBAAuB,EAAE,OAAO;IAChC,cAAc,EAAE,OAAO;IACvB,uBAAuB,EAAE,OAAO;IAChC,gBAAgB,EAAE,OAAO;IACzB,gBAAgB,EAAE,OAAO;I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,EAAE,UAAU,CAAC,CAAC;IACtC,OAAO,CAAC,GAAG,CAAC,KAAK,EAAE,EAAE,GAAG,CAAC,WAAW,EAAE,EAAE,UAAU,EAAE,GAAG,CAAC,YAAY,EAAE,CAAC,CAAC;AAC1E,CAAC,CAAC","sourcesContent":["import van from \"vanjs-core\";\nimport type { Widget, WidgetContext } from \"../types\";\n\nconst { div } = van.tags;\n\n/**\n * Rules the `code` field lints with. These are ESLint's own `eslint:recommended` set, spelled\n * out because `eslint-linter-browserify` ships no config resolver — a bare `Linter` with no\n * `rules` reports parse errors and nothing else, which looks like a working editor while\n * catching none of the mistakes people actually make.\n *\n * Stylistic rules are deliberately absent. A host embedding this editor has no say in the\n * code style of whoever types into it, and flagging every semicolon as an error is worse than\n * flagging nothing: the real diagnostics drown.\n */\nconst ESLINT_RULES: Record<string, \"error\"> = {\n \"constructor-super\": \"error\",\n \"for-direction\": \"error\",\n \"getter-return\": \"error\",\n \"no-async-promise-executor\": \"error\",\n \"no-case-declarations\": \"error\",\n \"no-class-assign\": \"error\",\n \"no-compare-neg-zero\": \"error\",\n \"no-cond-assign\": \"error\",\n \"no-const-assign\": \"error\",\n \"no-constant-binary-expression\": \"error\",\n \"no-constant-condition\": \"error\",\n \"no-control-regex\": \"error\",\n \"no-debugger\": \"error\",\n \"no-delete-var\": \"error\",\n \"no-dupe-args\": \"error\",\n \"no-dupe-class-members\": \"error\",\n \"no-dupe-else-if\": \"error\",\n \"no-dupe-keys\": \"error\",\n \"no-duplicate-case\": \"error\",\n \"no-empty\": \"error\",\n \"no-empty-character-class\": \"error\",\n \"no-empty-pattern\": \"error\",\n \"no-empty-static-block\": \"error\",\n \"no-ex-assign\": \"error\",\n \"no-extra-boolean-cast\": \"error\",\n \"no-fallthrough\": \"error\",\n \"no-func-assign\": \"error\",\n \"no-global-assign\": \"error\",\n \"no-import-assign\": \"error\",\n \"no-invalid-regexp\": \"error\",\n \"no-irregular-whitespace\": \"error\",\n \"no-loss-of-precision\": \"error\",\n \"no-misleading-character-class\": \"error\",\n \"no-new-native-nonconstructor\": \"error\",\n \"no-nonoctal-decimal-escape\": \"error\",\n \"no-obj-calls\": \"error\",\n \"no-octal\": \"error\",\n \"no-prototype-builtins\": \"error\",\n \"no-redeclare\": \"error\",\n \"no-regex-spaces\": \"error\",\n \"no-self-assign\": \"error\",\n \"no-setter-return\": \"error\",\n \"no-shadow-restricted-names\": \"error\",\n \"no-sparse-arrays\": \"error\",\n \"no-this-before-super\": \"error\",\n \"no-undef\": \"error\",\n \"no-unexpected-multiline\": \"error\",\n \"no-unreachable\": \"error\",\n \"no-unsafe-finally\": \"error\",\n \"no-unsafe-negation\": \"error\",\n \"no-unsafe-optional-chaining\": \"error\",\n \"no-unused-labels\": \"error\",\n \"no-unused-private-class-members\": \"error\",\n \"no-unused-vars\": \"error\",\n \"no-useless-backreference\": \"error\",\n \"no-useless-catch\": \"error\",\n \"no-useless-escape\": \"error\",\n \"no-with\": \"error\",\n \"require-yield\": \"error\",\n \"use-isnan\": \"error\",\n \"valid-typeof\": \"error\",\n};\n\n/**\n * Loads CodeMirror + ESLint on demand and mounts the editor into `parent`. These are the\n * heaviest dependencies in the library; importing them here keeps them out of the bundle of\n * forms that have no `code` field.\n */\nasync function mountCodeEditor(ctx: WidgetContext, parent: HTMLElement): Promise<void> {\n const [\n { basicSetup, EditorView },\n { javascript, esLint },\n { json, jsonParseLinter },\n { lintGutter, linter, forEachDiagnostic },\n eslint,\n globalsModule,\n { dracula },\n ] = await Promise.all([\n import(\"codemirror\"),\n import(\"@codemirror/lang-javascript\"),\n import(\"@codemirror/lang-json\"),\n import(\"@codemirror/lint\"),\n import(\"eslint-linter-browserify\"),\n import(\"globals\"),\n import(\"thememirror\"),\n ]);\n const f = ctx.field;\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n const globals: any = (globalsModule as any).default ?? globalsModule;\n // A `code` field may declare the globals its host injects at runtime. Those names are in\n // no environment preset, so without this every single one of them reads as `no-undef`.\n const fieldGlobals = f.globals && typeof f.globals === \"object\" ? f.globals : {};\n const eslintConfig = {\n languageOptions: {\n globals: { ...fieldGlobals, ...globals.node },\n parserOptions: { ecmaVersion: 2022, sourceType: \"module\" },\n },\n rules: ESLINT_RULES,\n };\n const cmTheme = EditorView.theme({\n \".cm-content, .cm-gutter\": { \"min-height\": \"150px\" },\n \".cm-gutters\": { margin: \"1px\" },\n \".cm-scroller\": { overflow: \"auto\" },\n });\n const lang = f.codemirrorType === \"json\"\n ? [json(), linter(jsonParseLinter())]\n : [\n javascript({ typescript: f.codemirrorType === \"typescript\" }),\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n linter(esLint(new eslint.Linter(), eslintConfig as any)),\n ];\n new EditorView({\n doc: typeof ctx.getValue() === \"string\" ? (ctx.getValue() as string) : \"\",\n parent,\n extensions: [\n dracula,\n cmTheme,\n basicSetup,\n lintGutter(),\n ...lang,\n EditorView.editable.of(!(f.readOnly || f.disabled)),\n // Diagnostics arrive in their own transaction after the linter runs, not with the edit,\n // so every update re-reads them and only edits emit a new value.\n EditorView.updateListener.of((u) => {\n let lintError = \"\";\n forEachDiagnostic(u.state, (d) => {\n if (!lintError && d.severity === \"error\") lintError = d.message;\n });\n if (u.docChanged) ctx.setValue(u.state.doc.toString() || undefined);\n ctx.setWidgetError(lintError);\n }),\n ],\n });\n}\n\nexport const codeWidget: Widget = (ctx) => {\n const editorHost = div({ id: ctx.id, class: \"jsf-code\" });\n void mountCodeEditor(ctx, editorHost);\n return [ctx.label(), ctx.description(), editorHost, ctx.errorMessage()];\n};\n"]}
|