-
-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathobject.ts
96 lines (87 loc) · 2.15 KB
/
object.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import { html } from 'lit';
import type { JSONSchema7 } from '@jsfe/types';
import type { Widgets, Jsf, Path, UiSchema } from '../index.js';
export const fieldObject = (
schema: JSONSchema7,
data: unknown,
path: Path,
uiState: unknown,
uiSchema: UiSchema,
dig: Jsf<unknown>['_dig'],
schemaPath: Path,
widgets: Widgets,
level = 0,
) => {
const error = 'Wrong object field';
if (typeof schema.properties !== 'object')
return widgets.callout?.({ id: '', message: error }) ?? html`${error}`;
const children = Object.entries(schema.properties).map(
([propName, propValue]) => {
if (Array.isArray(propValue) || typeof propValue === 'boolean')
return html``;
const value = data?.[propName] as unknown;
const subPath = [...path, propName];
const required = schema.required?.includes(propName);
const schemaPathAugmented = [...schemaPath];
// const propName = path.at(-1);
// schemaPathAugmented.push(propName);
// if (!Number.isNaN(propName)) {
// schemaPathAugmented.push(Number(propName));
// } else {
// }
schemaPathAugmented.push(propName);
// ${debuggerInline({ schemaPath, path })}
return dig(
propValue,
value,
subPath,
uiState?.[propName],
uiSchema?.[propName],
schemaPathAugmented,
required,
level + 1,
);
},
);
let label: string | undefined;
const key = path.at(-1);
if (schema.title) {
label = schema.title;
} else if (typeof key !== 'number') {
label = key;
}
if (typeof uiSchema?.['ui:title'] === 'string') label = uiSchema['ui:title'];
const options = {
id: path.join('.'),
label,
helpText: schema.description,
children,
level,
};
return (
widgets?.object?.(options) ??
widgets?.callout?.({ id: '', message: error }) ??
html`${error}`
);
};
// ${
// /* NOTE: Experimental */ schema?.additionalProperties
// ? html`<div>
// ${schema?.additionalProperties
// ? dig(
// {
// type: 'array',
// items: {
// type: 'string',
// },
// },
// data,
// [...path],
// uiState,
// uiSchema,
// schemaPath,
// )
// : nothing}
// </div>`
// : nothing
// }