-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLitJs.ts
62 lines (53 loc) · 1.49 KB
/
LitJs.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
import { LitElement, html } from 'lit';
import { customElement, state } from 'lit/decorators.js';
import '@jsfe/shoelace';
import type { FromSchema, JSONSchema7 } from '@jsfe/shoelace';
// -----------------------------------------------------------------------------
export const mySchema = {
type: 'object',
properties: {
foo: {
type: 'string',
},
bar: {
type: 'boolean',
},
},
} as const satisfies JSONSchema7;
export type MyData = FromSchema<typeof mySchema>;
function assertValidData(data: unknown): data is MyData {
// Use your AJV or other schema checker here, if you need thorough validation
return true;
}
@customElement('lit-js')
export default class LitJs extends LitElement {
@state() private _dataInLit: MyData = {
foo: 'hello',
};
override render() {
return html`
<jsf-shoelace
.schema=${mySchema /* Type-casted as JSONSchema7 */}
.uiSchema=${{
/* Type-casted as UiSchema */
bar: {
'ui:widget': 'switch',
},
}}
.data=${this._dataInLit}
.dataChangeCallback=${(newData: unknown) => {
console.log({ 'Data from Lit': newData });
if (assertValidData(newData)) this._dataInLit = newData;
else console.error('Invalid data!');
}}
.submitCallback=${(newData: unknown, valid: boolean) => {
console.log({ 'Submitted from Lit!': newData, valid });
if (assertValidData(newData)) {
// Do stuff...
}
}}
></jsf-shoelace>
<pre>${JSON.stringify({ data: this._dataInLit }, null, 2)}</pre>
`;
}
}