-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVueJs.vue
72 lines (59 loc) · 1.58 KB
/
VueJs.vue
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
<script setup lang="ts">
// NOTE: Type checking / inference is not working declaratively,
// there might be a way though,
// like extending Vue Components namespace with HTMLTagNameMap.
import { reactive } from 'vue';
import '@jsfe/shoelace';
import type { FromSchema, JSONSchema7, UiSchema, Jsf } from '@jsfe/shoelace';
// -----------------------------------------------------------------------------
const mySchema = {
type: 'object',
properties: {
foo: {
type: 'string',
},
bar: {
type: 'boolean',
},
},
} as const satisfies JSONSchema7;
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;
}
const uiSchema: UiSchema = {
bar: {
'ui:widget': 'switch',
},
};
let dataInVue = reactive<MyData>({
value: {
foo: 'hello',
},
});
const handleDataChange: Jsf['dataChangeCallback'] = (newData) => {
console.log({ 'Data from Vue': newData });
if (assertValidData(newData)) dataInVue.value = newData;
else console.error('Invalid data!');
};
const handleFormSubmit: Jsf['submitCallback'] = (newData, valid) => {
console.log({ 'Submitted from Vue!': newData, valid });
if (assertValidData(newData)) {
// Do stuff...
}
};
</script>
<template>
<article id="vue">
<jsf-shoelace
.schema="mySchema"
.data="dataInVue.value"
.dataChangeCallback="handleDataChange"
.submitCallback="handleFormSubmit"
.uiSchema="uiSchema"
></jsf-shoelace>
<pre>{{ JSON.stringify(dataInVue, null, 2) }}</pre>
</article>
</template>