-
-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy patharray-primitive.ts
82 lines (65 loc) · 1.85 KB
/
array-primitive.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import { html } from 'lit';
import type { Jsf } from '../json-schema-form.js';
import type { Widgets, Path, UiSchema, JSONSchema7 } from '@jsfe/types';
export const fieldArrayPrimitive = (
schema: JSONSchema7,
dataLevel: unknown,
path: Path,
uiState: unknown,
uiOptions: UiSchema,
required: boolean,
handleChange: Jsf<unknown>['_handleChange'],
// dig: Jsf['_dig'],
schemaPath: Path,
widgets: Widgets,
level = 0,
) => {
// if (!Array.isArray(dataLevel)) return html``;
const id = path.join('.');
function missing(widgetName: string) {
const options = { id, message: `Missing ${widgetName} widget.` };
return widgets?.callout?.(options) ?? html`<p>${options.message}</p>`;
}
const helpText =
schema.description ?? (uiOptions?.['ui:help'] as string) ?? '';
// NOTE: Unused for now. Too noisy?
// let itemsLabel: string | undefined;
// if (
// typeof schema.items === 'object' &&
// !Array.isArray(schema.items) &&
// schema.items.title
// ) {
// itemsLabel = schema.items.title;
// }
// options.value ||= [];
// if (!Array.isArray(options.value)) return;
const items = schema.items;
if (typeof items !== 'object' || Array.isArray(items)) return;
const valueChangedCallback = (enumValue: (string | number)[]) => {
const schemaPathAugmented = [
...schemaPath,
'items',
'enum',
// FIXME:
// enumValue.at(-1),
];
handleChange(path, enumValue, schemaPathAugmented);
};
const disabled = uiOptions?.['ui:disabled'] || false;
const options = {
label: schema.title,
helpText,
value: dataLevel ?? schema?.default,
enum: items.enum,
disabled,
// itemsLabel,
level,
id,
// required,
valueChangedCallback,
};
if (uiOptions?.['ui:widget'] === 'select') {
return widgets?.selectMultiple?.(options) || missing('multi select');
}
return widgets?.checkboxGroup?.(options) || missing('array primitive');
};