@tak-ps/vue-tabler 5.4.0 → 5.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/.github/workflows/test.yml +9 -1
- package/CHANGELOG.md +14 -0
- package/README.md +3 -0
- package/components/Schema.vue +399 -12
- package/components/input/Input.vue +3 -0
- package/package.json +4 -2
- package/test/Schema.spec.ts +518 -0
- package/vitest.config.mjs +5 -0
|
@@ -36,4 +36,12 @@ jobs:
|
|
|
36
36
|
run: npm run lint
|
|
37
37
|
|
|
38
38
|
- name: Test
|
|
39
|
-
run: npm test
|
|
39
|
+
run: npm run test:coverage
|
|
40
|
+
|
|
41
|
+
- name: Upload coverage
|
|
42
|
+
if: matrix.node-version == 24
|
|
43
|
+
uses: codecov/codecov-action@v5
|
|
44
|
+
with:
|
|
45
|
+
token: ${{ secrets.CODECOV_TOKEN }}
|
|
46
|
+
files: coverage/lcov.info
|
|
47
|
+
fail_ci_if_error: false
|
package/CHANGELOG.md
CHANGED
|
@@ -12,6 +12,20 @@
|
|
|
12
12
|
|
|
13
13
|
### Pending
|
|
14
14
|
|
|
15
|
+
### v5.6.0 - 2026-09-15
|
|
16
|
+
|
|
17
|
+
- :white_check_mark: Add `TablerSchema` tests for editing every primitive input type and for default, const and nested default seeding across model and schema changes
|
|
18
|
+
- :white_check_mark: Collect v8 test coverage with `npm run test:coverage` and upload it to Codecov from the Test workflow
|
|
19
|
+
- :tada: Render a `type: object` property in `TablerSchema` as a nested form instead of a raw textarea
|
|
20
|
+
- :tada: Render an `anyOf`/`oneOf` of object shapes in `TablerSchema` with a variant picker, seeding `const` discriminators and resetting the value when the variant changes
|
|
21
|
+
|
|
22
|
+
### v5.5.0
|
|
23
|
+
|
|
24
|
+
- :tada: Render an array of objects in `TablerSchema` as a table with a row editor modal, CSV import and clear, matching the CloudTAK Layer Environment form
|
|
25
|
+
- :rocket: Seed missing `TablerSchema` values from the JSON Schema `default` and leave numbers without a default unset instead of an empty string
|
|
26
|
+
- :rocket: Add a `step` prop to `TablerInput`, set to `1` for integer and `any` for number fields by `TablerSchema`
|
|
27
|
+
- :bug: Fix `TablerSchema` self reference for nested arrays and crash when adding to an array without `items`
|
|
28
|
+
|
|
15
29
|
### v5.4.0
|
|
16
30
|
|
|
17
31
|
- :tada: Add a Multiple Choice field to `TablerSchemaBuilder`, built as an array of enum strings and rendered by `TablerSchema` as a checkbox group via the new `TablerMultiEnum` input
|
package/README.md
CHANGED
|
@@ -10,6 +10,9 @@
|
|
|
10
10
|
<a href="https://github.com/tak-ps/vue-tabler/blob/main/LICENSE">
|
|
11
11
|
<img src="https://img.shields.io/npm/l/@tak-ps/vue-tabler.svg" alt="License">
|
|
12
12
|
</a>
|
|
13
|
+
<a href="https://codecov.io/gh/dfpc-coe/vue-tabler">
|
|
14
|
+
<img src="https://codecov.io/gh/dfpc-coe/vue-tabler/graph/badge.svg" alt="Coverage">
|
|
15
|
+
</a>
|
|
13
16
|
</p>
|
|
14
17
|
|
|
15
18
|
<p align="center">
|
package/components/Schema.vue
CHANGED
|
@@ -3,11 +3,11 @@
|
|
|
3
3
|
<TablerLoading v-if='loading' />
|
|
4
4
|
<template v-else>
|
|
5
5
|
<div
|
|
6
|
-
v-for='key in
|
|
6
|
+
v-for='key in visibleKeys'
|
|
7
7
|
:key='key'
|
|
8
8
|
class='py-2 floating-input'
|
|
9
9
|
>
|
|
10
|
-
<template v-if='s.properties[key]
|
|
10
|
+
<template v-if='isMultiEnum(s.properties[key])'>
|
|
11
11
|
<TablerMultiEnum
|
|
12
12
|
v-model='data[key]'
|
|
13
13
|
:label='key'
|
|
@@ -42,6 +42,7 @@
|
|
|
42
42
|
<TablerInput
|
|
43
43
|
v-model='data[key]'
|
|
44
44
|
:type='s.properties[key].type'
|
|
45
|
+
:step='s.properties[key].type === "integer" ? 1 : "any"'
|
|
45
46
|
:label='key'
|
|
46
47
|
:disabled='disabled'
|
|
47
48
|
:required='s.properties[key].required || false'
|
|
@@ -57,6 +58,126 @@
|
|
|
57
58
|
:description='s.properties[key].description || ""'
|
|
58
59
|
/>
|
|
59
60
|
</template>
|
|
61
|
+
<template v-else-if='isObjectTable(s.properties[key])'>
|
|
62
|
+
<div class='d-flex align-items-center'>
|
|
63
|
+
<label
|
|
64
|
+
class='form-label mb-0'
|
|
65
|
+
v-text='key'
|
|
66
|
+
/>
|
|
67
|
+
<span
|
|
68
|
+
v-if='s.properties[key].required'
|
|
69
|
+
class='text-red mx-1'
|
|
70
|
+
>*</span>
|
|
71
|
+
<div
|
|
72
|
+
v-if='!disabled'
|
|
73
|
+
class='ms-auto d-flex'
|
|
74
|
+
>
|
|
75
|
+
<TablerIconButton
|
|
76
|
+
title='Clear Table'
|
|
77
|
+
@click='data[key].splice(0, data[key].length)'
|
|
78
|
+
>
|
|
79
|
+
<IconTrash
|
|
80
|
+
:size='32'
|
|
81
|
+
stroke='1'
|
|
82
|
+
/>
|
|
83
|
+
</TablerIconButton>
|
|
84
|
+
<TablerIconButton
|
|
85
|
+
title='Import CSV'
|
|
86
|
+
@click='openImport(key)'
|
|
87
|
+
>
|
|
88
|
+
<IconDatabaseImport
|
|
89
|
+
:size='32'
|
|
90
|
+
stroke='1'
|
|
91
|
+
/>
|
|
92
|
+
</TablerIconButton>
|
|
93
|
+
<TablerIconButton
|
|
94
|
+
title='Add Row'
|
|
95
|
+
@click='openRow(key)'
|
|
96
|
+
>
|
|
97
|
+
<IconPlus
|
|
98
|
+
:size='32'
|
|
99
|
+
stroke='1'
|
|
100
|
+
/>
|
|
101
|
+
</TablerIconButton>
|
|
102
|
+
</div>
|
|
103
|
+
</div>
|
|
104
|
+
|
|
105
|
+
<div
|
|
106
|
+
v-if='data[key] && data[key].length'
|
|
107
|
+
class='table-responsive'
|
|
108
|
+
>
|
|
109
|
+
<table class='table table-hover card-table table-vcenter border rounded cursor-pointer'>
|
|
110
|
+
<thead>
|
|
111
|
+
<tr>
|
|
112
|
+
<th
|
|
113
|
+
v-for='col in columns(key)'
|
|
114
|
+
:key='col'
|
|
115
|
+
v-text='col'
|
|
116
|
+
/>
|
|
117
|
+
</tr>
|
|
118
|
+
</thead>
|
|
119
|
+
<tbody>
|
|
120
|
+
<tr
|
|
121
|
+
v-for='(row, i) in data[key]'
|
|
122
|
+
:key='i'
|
|
123
|
+
@click='openRow(key, Number(i))'
|
|
124
|
+
>
|
|
125
|
+
<td
|
|
126
|
+
v-for='col in columns(key)'
|
|
127
|
+
:key='col'
|
|
128
|
+
v-text='row[col]'
|
|
129
|
+
/>
|
|
130
|
+
</tr>
|
|
131
|
+
</tbody>
|
|
132
|
+
</table>
|
|
133
|
+
</div>
|
|
134
|
+
<TablerNone
|
|
135
|
+
v-else
|
|
136
|
+
:label='key'
|
|
137
|
+
:create='!disabled'
|
|
138
|
+
@create='openRow(key)'
|
|
139
|
+
/>
|
|
140
|
+
</template>
|
|
141
|
+
<template v-else-if='isUnion(s.properties[key])'>
|
|
142
|
+
<TablerEnum
|
|
143
|
+
:model-value='variantLabels(key)[variant[key] || 0]'
|
|
144
|
+
:label='key'
|
|
145
|
+
:disabled='disabled'
|
|
146
|
+
:required='s.properties[key].required || false'
|
|
147
|
+
:description='s.properties[key].description || ""'
|
|
148
|
+
:options='variantLabels(key)'
|
|
149
|
+
@update:model-value='selectVariant(key, $event)'
|
|
150
|
+
/>
|
|
151
|
+
|
|
152
|
+
<div class='border rounded my-2 py-2 mx-2 px-2'>
|
|
153
|
+
<TablerSchema
|
|
154
|
+
:key='variant[key] || 0'
|
|
155
|
+
v-model='data[key]'
|
|
156
|
+
:schema='unionVariants(s.properties[key])[variant[key] || 0]'
|
|
157
|
+
:disabled='disabled'
|
|
158
|
+
/>
|
|
159
|
+
</div>
|
|
160
|
+
</template>
|
|
161
|
+
<template v-else-if='isObject(s.properties[key])'>
|
|
162
|
+
<div class='d-flex align-items-center'>
|
|
163
|
+
<label
|
|
164
|
+
class='form-label mb-0'
|
|
165
|
+
v-text='key'
|
|
166
|
+
/>
|
|
167
|
+
<span
|
|
168
|
+
v-if='s.properties[key].required'
|
|
169
|
+
class='text-red mx-1'
|
|
170
|
+
>*</span>
|
|
171
|
+
</div>
|
|
172
|
+
|
|
173
|
+
<div class='border rounded my-2 py-2 mx-2 px-2'>
|
|
174
|
+
<TablerSchema
|
|
175
|
+
v-model='data[key]'
|
|
176
|
+
:schema='s.properties[key]'
|
|
177
|
+
:disabled='disabled'
|
|
178
|
+
/>
|
|
179
|
+
</div>
|
|
180
|
+
</template>
|
|
60
181
|
<template v-else-if='s.properties[key].type === "array"'>
|
|
61
182
|
<div class='d-flex'>
|
|
62
183
|
<label
|
|
@@ -113,21 +234,92 @@
|
|
|
113
234
|
</template>
|
|
114
235
|
</div>
|
|
115
236
|
</template>
|
|
237
|
+
|
|
238
|
+
<TablerModal v-if='edit.shown'>
|
|
239
|
+
<button
|
|
240
|
+
type='button'
|
|
241
|
+
class='btn-close'
|
|
242
|
+
aria-label='Close'
|
|
243
|
+
@click='edit.shown = false'
|
|
244
|
+
/>
|
|
245
|
+
<div class='modal-status bg-yellow' />
|
|
246
|
+
<div class='modal-header'>
|
|
247
|
+
<span class='modal-title'>Row Editor</span>
|
|
248
|
+
<div class='ms-auto'>
|
|
249
|
+
<TablerDelete
|
|
250
|
+
v-if='!disabled && edit.index !== null'
|
|
251
|
+
displaytype='icon'
|
|
252
|
+
@delete='removeRow'
|
|
253
|
+
/>
|
|
254
|
+
</div>
|
|
255
|
+
</div>
|
|
256
|
+
<div class='modal-body py-4'>
|
|
257
|
+
<TablerSchema
|
|
258
|
+
v-model='edit.row'
|
|
259
|
+
:schema='s.properties[edit.key].items'
|
|
260
|
+
:disabled='disabled'
|
|
261
|
+
/>
|
|
262
|
+
|
|
263
|
+
<button
|
|
264
|
+
v-if='!disabled'
|
|
265
|
+
class='btn btn-primary w-100 mt-4'
|
|
266
|
+
@click='saveRow'
|
|
267
|
+
>
|
|
268
|
+
Done
|
|
269
|
+
</button>
|
|
270
|
+
</div>
|
|
271
|
+
</TablerModal>
|
|
272
|
+
|
|
273
|
+
<TablerModal v-if='csv.shown'>
|
|
274
|
+
<button
|
|
275
|
+
type='button'
|
|
276
|
+
class='btn-close'
|
|
277
|
+
aria-label='Close'
|
|
278
|
+
@click='csv.shown = false'
|
|
279
|
+
/>
|
|
280
|
+
<div class='modal-status bg-yellow' />
|
|
281
|
+
<div class='modal-header'>
|
|
282
|
+
<span class='modal-title'>Import CSV</span>
|
|
283
|
+
</div>
|
|
284
|
+
<div class='modal-body py-4'>
|
|
285
|
+
<TablerInput
|
|
286
|
+
v-model='csv.text'
|
|
287
|
+
label='CSV'
|
|
288
|
+
:rows='10'
|
|
289
|
+
:description='"Rows of " + columns(csv.key).join(", ") + " separated by commas or tabs"'
|
|
290
|
+
/>
|
|
291
|
+
|
|
292
|
+
<button
|
|
293
|
+
class='btn btn-primary w-100 mt-4'
|
|
294
|
+
@click='importCSV'
|
|
295
|
+
>
|
|
296
|
+
Import
|
|
297
|
+
</button>
|
|
298
|
+
</div>
|
|
299
|
+
</TablerModal>
|
|
116
300
|
</div>
|
|
117
301
|
</template>
|
|
118
302
|
|
|
119
303
|
<script setup lang="ts">
|
|
120
|
-
import { ref, watch, onMounted } from 'vue'
|
|
304
|
+
import { ref, reactive, computed, watch, onMounted } from 'vue'
|
|
121
305
|
import TablerInput from './input/Input.vue';
|
|
122
306
|
import TablerToggle from './input/Toggle.vue';
|
|
123
307
|
import TablerEnum from './input/Enum.vue';
|
|
124
308
|
import TablerMultiEnum from './input/MultiEnum.vue';
|
|
125
309
|
import TablerLoading from './Loading.vue';
|
|
310
|
+
import TablerModal from './Modal.vue';
|
|
311
|
+
import TablerDelete from './Delete.vue';
|
|
312
|
+
import TablerNone from './None.vue';
|
|
313
|
+
import TablerIconButton from './IconButton.vue';
|
|
314
|
+
import { isMultiEnum } from './schema-types';
|
|
126
315
|
import {
|
|
127
316
|
IconPlus,
|
|
128
317
|
IconTrash,
|
|
318
|
+
IconDatabaseImport,
|
|
129
319
|
} from '@tabler/icons-vue';
|
|
130
320
|
|
|
321
|
+
defineOptions({ name: 'TablerSchema' });
|
|
322
|
+
|
|
131
323
|
export interface SchemaProps {
|
|
132
324
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
133
325
|
modelValue: any;
|
|
@@ -151,19 +343,202 @@ const s = ref<any>({});
|
|
|
151
343
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
152
344
|
const data = ref<any>({});
|
|
153
345
|
|
|
346
|
+
const edit = reactive<{
|
|
347
|
+
shown: boolean;
|
|
348
|
+
key: string;
|
|
349
|
+
index: number | null;
|
|
350
|
+
row: Record<string, unknown>;
|
|
351
|
+
}>({
|
|
352
|
+
shown: false,
|
|
353
|
+
key: '',
|
|
354
|
+
index: null,
|
|
355
|
+
row: {}
|
|
356
|
+
});
|
|
357
|
+
|
|
358
|
+
const csv = reactive({
|
|
359
|
+
shown: false,
|
|
360
|
+
key: '',
|
|
361
|
+
text: ''
|
|
362
|
+
});
|
|
363
|
+
|
|
364
|
+
/** Selected union variant index per property key */
|
|
365
|
+
const variant = reactive<Record<string, number>>({});
|
|
366
|
+
|
|
367
|
+
/** Properties with a fixed const value are seeded but never rendered */
|
|
368
|
+
const visibleKeys = computed<string[]>(() => {
|
|
369
|
+
if (!s.value.properties) return [];
|
|
370
|
+
return Object.keys(s.value.properties).filter((key) => s.value.properties[key].const === undefined);
|
|
371
|
+
});
|
|
372
|
+
|
|
373
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
374
|
+
const isObject = (prop: any): boolean => {
|
|
375
|
+
return prop.type === 'object' && !!prop.properties;
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
/** The object members of an anyOf/oneOf, ignoring a null member used to mark a nullable value */
|
|
379
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
380
|
+
const unionVariants = (prop: any): any[] => {
|
|
381
|
+
const members = prop.oneOf || prop.anyOf;
|
|
382
|
+
if (!Array.isArray(members)) return [];
|
|
383
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
384
|
+
return members.filter((m: any) => m && m.type !== 'null');
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
/** A choice between two or more object shapes, rendered with a variant picker */
|
|
388
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
389
|
+
const isUnion = (prop: any): boolean => {
|
|
390
|
+
const variants = unionVariants(prop);
|
|
391
|
+
return variants.length >= 2 && variants.every((v) => isObject(v));
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
const variantLabels = (key: string): string[] => {
|
|
395
|
+
const labels: string[] = [];
|
|
396
|
+
|
|
397
|
+
unionVariants(s.value.properties[key]).forEach((v, i) => {
|
|
398
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
399
|
+
const discriminator = Object.values(v.properties as Record<string, any>).find((p) => p.const !== undefined);
|
|
400
|
+
let label = v.title || (discriminator ? String(discriminator.const) : `Option ${i + 1}`);
|
|
401
|
+
if (labels.includes(label)) label = `${label} (${i + 1})`;
|
|
402
|
+
labels.push(label);
|
|
403
|
+
});
|
|
404
|
+
|
|
405
|
+
return labels;
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
/** Pick the variant a stored value belongs to by discriminator, then by required keys */
|
|
409
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
410
|
+
const detectVariant = (prop: any, value: any): number => {
|
|
411
|
+
const variants = unionVariants(prop);
|
|
412
|
+
if (!value || typeof value !== 'object') return 0;
|
|
413
|
+
|
|
414
|
+
for (let i = 0; i < variants.length; i++) {
|
|
415
|
+
for (const [k, p] of Object.entries(variants[i].properties)) {
|
|
416
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
417
|
+
if ((p as any).const !== undefined && value[k] === (p as any).const) return i;
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
for (let i = 0; i < variants.length; i++) {
|
|
422
|
+
const required: string[] = variants[i].required || [];
|
|
423
|
+
if (required.length && required.every((k) => value[k] !== undefined)) return i;
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
return 0;
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
/** A fresh value for a variant holding only its const properties, the nested form seeds the rest */
|
|
430
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
431
|
+
const seedVariant = (prop: any, index: number): Record<string, unknown> => {
|
|
432
|
+
const seed: Record<string, unknown> = {};
|
|
433
|
+
const v = unionVariants(prop)[index];
|
|
434
|
+
if (!v) return seed;
|
|
435
|
+
|
|
436
|
+
for (const [k, p] of Object.entries(v.properties)) {
|
|
437
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
438
|
+
if ((p as any).const !== undefined) seed[k] = (p as any).const;
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
return seed;
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
const selectVariant = (key: string, label: string) => {
|
|
445
|
+
const index = variantLabels(key).indexOf(label);
|
|
446
|
+
if (index === -1 || index === variant[key]) return;
|
|
447
|
+
|
|
448
|
+
variant[key] = index;
|
|
449
|
+
data.value[key] = seedVariant(s.value.properties[key], index);
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
const resolveVariants = () => {
|
|
453
|
+
if (!s.value.properties) return;
|
|
454
|
+
|
|
455
|
+
for (const key in s.value.properties) {
|
|
456
|
+
const prop = s.value.properties[key];
|
|
457
|
+
if (isUnion(prop)) variant[key] = detectVariant(prop, data.value[key]);
|
|
458
|
+
}
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
/** An array of objects with known properties is rendered as a table with a row editor */
|
|
462
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
463
|
+
const isObjectTable = (prop: any): boolean => {
|
|
464
|
+
return prop.type === 'array'
|
|
465
|
+
&& prop.items
|
|
466
|
+
&& prop.items.type === 'object'
|
|
467
|
+
&& !!prop.items.properties;
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
const columns = (key: string): string[] => {
|
|
471
|
+
const prop = s.value.properties[key];
|
|
472
|
+
if (!prop || !isObjectTable(prop)) return [];
|
|
473
|
+
return Object.keys(prop.items.properties);
|
|
474
|
+
}
|
|
475
|
+
|
|
154
476
|
const push = (key: string) => {
|
|
155
|
-
|
|
156
|
-
|
|
477
|
+
const items = props.schema.properties[key].items;
|
|
478
|
+
|
|
479
|
+
if (!items || items.type === 'string') {
|
|
480
|
+
data.value[key].push('');
|
|
481
|
+
} else if (items.type === 'object') {
|
|
157
482
|
data.value[key].push({});
|
|
158
|
-
} else if (
|
|
483
|
+
} else if (items.type === 'array') {
|
|
159
484
|
data.value[key].push([]);
|
|
160
|
-
} else if (
|
|
485
|
+
} else if (items.type === 'boolean') {
|
|
161
486
|
data.value[key].push(false);
|
|
162
487
|
} else {
|
|
163
488
|
data.value[key].push('');
|
|
164
489
|
}
|
|
165
490
|
}
|
|
166
491
|
|
|
492
|
+
const openRow = (key: string, index?: number) => {
|
|
493
|
+
edit.key = key;
|
|
494
|
+
edit.index = index ?? null;
|
|
495
|
+
edit.row = index === undefined ? {} : JSON.parse(JSON.stringify(data.value[key][index]));
|
|
496
|
+
edit.shown = true;
|
|
497
|
+
}
|
|
498
|
+
|
|
499
|
+
const saveRow = () => {
|
|
500
|
+
const row = JSON.parse(JSON.stringify(edit.row));
|
|
501
|
+
|
|
502
|
+
if (edit.index === null) {
|
|
503
|
+
data.value[edit.key].push(row);
|
|
504
|
+
} else {
|
|
505
|
+
data.value[edit.key][edit.index] = row;
|
|
506
|
+
}
|
|
507
|
+
|
|
508
|
+
edit.shown = false;
|
|
509
|
+
}
|
|
510
|
+
|
|
511
|
+
const removeRow = () => {
|
|
512
|
+
if (edit.index !== null) {
|
|
513
|
+
data.value[edit.key].splice(edit.index, 1);
|
|
514
|
+
}
|
|
515
|
+
|
|
516
|
+
edit.shown = false;
|
|
517
|
+
}
|
|
518
|
+
|
|
519
|
+
const openImport = (key: string) => {
|
|
520
|
+
csv.key = key;
|
|
521
|
+
csv.text = '';
|
|
522
|
+
csv.shown = true;
|
|
523
|
+
}
|
|
524
|
+
|
|
525
|
+
const importCSV = () => {
|
|
526
|
+
const headers = columns(csv.key);
|
|
527
|
+
const lines = csv.text.split('\n').filter((line) => line.trim().length);
|
|
528
|
+
const delimiter = lines.length && lines[0].includes(',') ? ',' : '\t';
|
|
529
|
+
|
|
530
|
+
for (const line of lines) {
|
|
531
|
+
const cells = line.split(delimiter);
|
|
532
|
+
const row: Record<string, string> = {};
|
|
533
|
+
for (let i = 0; i < headers.length; i++) {
|
|
534
|
+
row[headers[i]] = (cells[i] ?? '').trim();
|
|
535
|
+
}
|
|
536
|
+
data.value[csv.key].push(row);
|
|
537
|
+
}
|
|
538
|
+
|
|
539
|
+
csv.shown = false;
|
|
540
|
+
}
|
|
541
|
+
|
|
167
542
|
// Watch for data changes
|
|
168
543
|
watch(data, () => {
|
|
169
544
|
emit('update:modelValue', data.value);
|
|
@@ -173,24 +548,33 @@ const updateSchema = () => {
|
|
|
173
548
|
s.value = JSON.parse(JSON.stringify(props.schema));
|
|
174
549
|
if (s.value.type === 'object' && s.value.properties) {
|
|
175
550
|
for (const req of (s.value.required || [])) {
|
|
176
|
-
s.value.properties[req].required = true;
|
|
551
|
+
if (s.value.properties[req]) s.value.properties[req].required = true;
|
|
177
552
|
}
|
|
178
553
|
}
|
|
179
554
|
}
|
|
180
555
|
|
|
556
|
+
/** Seed missing keys from the schema default, falling back to an empty value of the right shape */
|
|
181
557
|
const updateDataDefaults = () => {
|
|
182
558
|
if (s.value.type === 'object' && s.value.properties) {
|
|
183
559
|
for (const key in s.value.properties) {
|
|
184
560
|
if (data.value[key] !== undefined) continue;
|
|
185
561
|
|
|
186
|
-
const
|
|
562
|
+
const prop = s.value.properties[key];
|
|
187
563
|
|
|
188
|
-
if (
|
|
564
|
+
if (prop.const !== undefined) {
|
|
565
|
+
data.value[key] = JSON.parse(JSON.stringify(prop.const));
|
|
566
|
+
} else if (prop.default !== undefined) {
|
|
567
|
+
data.value[key] = JSON.parse(JSON.stringify(prop.default));
|
|
568
|
+
} else if (isUnion(prop)) {
|
|
569
|
+
data.value[key] = seedVariant(prop, 0);
|
|
570
|
+
} else if (prop.type === 'array') {
|
|
189
571
|
data.value[key] = [];
|
|
190
|
-
} else if (type === 'boolean') {
|
|
572
|
+
} else if (prop.type === 'boolean') {
|
|
191
573
|
data.value[key] = false;
|
|
192
|
-
} else if (type === 'object') {
|
|
574
|
+
} else if (prop.type === 'object') {
|
|
193
575
|
data.value[key] = {};
|
|
576
|
+
} else if (prop.type === 'number' || prop.type === 'integer') {
|
|
577
|
+
continue;
|
|
194
578
|
} else {
|
|
195
579
|
data.value[key] = '';
|
|
196
580
|
}
|
|
@@ -201,12 +585,14 @@ const updateDataDefaults = () => {
|
|
|
201
585
|
watch(() => props.schema, () => {
|
|
202
586
|
updateSchema();
|
|
203
587
|
updateDataDefaults();
|
|
588
|
+
resolveVariants();
|
|
204
589
|
}, { deep: true });
|
|
205
590
|
|
|
206
591
|
watch(() => props.modelValue, () => {
|
|
207
592
|
if (JSON.stringify(props.modelValue) !== JSON.stringify(data.value)) {
|
|
208
593
|
data.value = JSON.parse(JSON.stringify(props.modelValue));
|
|
209
594
|
updateDataDefaults();
|
|
595
|
+
resolveVariants();
|
|
210
596
|
}
|
|
211
597
|
}, { deep: true });
|
|
212
598
|
|
|
@@ -216,6 +602,7 @@ onMounted(async () => {
|
|
|
216
602
|
data.value = JSON.parse(JSON.stringify(props.modelValue));
|
|
217
603
|
updateSchema();
|
|
218
604
|
updateDataDefaults();
|
|
605
|
+
resolveVariants();
|
|
219
606
|
|
|
220
607
|
loading.value = false;
|
|
221
608
|
})
|
|
@@ -47,6 +47,7 @@
|
|
|
47
47
|
:autocomplete='autocomplete'
|
|
48
48
|
:autofocus='autofocus'
|
|
49
49
|
:type='computed_type'
|
|
50
|
+
:step='step'
|
|
50
51
|
:class='{
|
|
51
52
|
"is-invalid": errorstr,
|
|
52
53
|
"tabler-input-with-end": hasEndAdornment
|
|
@@ -211,6 +212,7 @@ export interface InputProps {
|
|
|
211
212
|
rows?: number;
|
|
212
213
|
wrap?: string;
|
|
213
214
|
type?: string;
|
|
215
|
+
step?: number | string;
|
|
214
216
|
label?: string;
|
|
215
217
|
placeholder?: string;
|
|
216
218
|
error?: string;
|
|
@@ -227,6 +229,7 @@ const props = withDefaults(defineProps<InputProps>(), {
|
|
|
227
229
|
rows: 1,
|
|
228
230
|
wrap: 'soft',
|
|
229
231
|
type: 'text',
|
|
232
|
+
step: undefined,
|
|
230
233
|
label: '',
|
|
231
234
|
placeholder: '',
|
|
232
235
|
error: ''
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tak-ps/vue-tabler",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "5.
|
|
4
|
+
"version": "5.6.0",
|
|
5
5
|
"lib": "lib.ts",
|
|
6
6
|
"main": "lib.ts",
|
|
7
7
|
"module": "lib.ts",
|
|
@@ -12,6 +12,7 @@
|
|
|
12
12
|
"scripts": {
|
|
13
13
|
"test": "vitest run",
|
|
14
14
|
"test:watch": "vitest",
|
|
15
|
+
"test:coverage": "vitest run --coverage",
|
|
15
16
|
"check": "vue-tsc --noEmit",
|
|
16
17
|
"lint": "eslint lib.ts components/"
|
|
17
18
|
},
|
|
@@ -38,11 +39,12 @@
|
|
|
38
39
|
"@eslint/js": "^10.0.0",
|
|
39
40
|
"@typescript-eslint/parser": "^8.8.1",
|
|
40
41
|
"@vitejs/plugin-vue": "^6.0.5",
|
|
42
|
+
"@vitest/coverage-v8": "^5.0.1",
|
|
41
43
|
"@vue/test-utils": "^2.4.6",
|
|
42
44
|
"eslint": "^10.0.0",
|
|
43
45
|
"eslint-plugin-vue": "^10.0.0",
|
|
44
46
|
"globals": "^17.0.0",
|
|
45
|
-
"jsdom": "^
|
|
47
|
+
"jsdom": "^30.0.0",
|
|
46
48
|
"typescript": "^6.0.0",
|
|
47
49
|
"typescript-eslint": "^8.36.0",
|
|
48
50
|
"vite": "^8.0.0",
|
|
@@ -0,0 +1,518 @@
|
|
|
1
|
+
import { describe, expect, it, afterEach } from 'vitest'
|
|
2
|
+
import { mount, flushPromises } from '@vue/test-utils'
|
|
3
|
+
import Schema from '../components/Schema.vue'
|
|
4
|
+
|
|
5
|
+
const TABLE_SCHEMA = {
|
|
6
|
+
type: 'object',
|
|
7
|
+
required: ['Shares'],
|
|
8
|
+
properties: {
|
|
9
|
+
Shares: {
|
|
10
|
+
type: 'array',
|
|
11
|
+
items: {
|
|
12
|
+
type: 'object',
|
|
13
|
+
properties: {
|
|
14
|
+
ShareId: { type: 'string' },
|
|
15
|
+
Password: { type: 'string' },
|
|
16
|
+
},
|
|
17
|
+
},
|
|
18
|
+
},
|
|
19
|
+
},
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function lastEmitted(wrapper: ReturnType<typeof mount>): Record<string, unknown> {
|
|
23
|
+
const events = wrapper.emitted('update:modelValue') || []
|
|
24
|
+
return events[events.length - 1][0] as Record<string, unknown>
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
afterEach(() => {
|
|
28
|
+
document.body.innerHTML = ''
|
|
29
|
+
})
|
|
30
|
+
|
|
31
|
+
describe('TablerSchema defaults', () => {
|
|
32
|
+
it('seeds missing keys from the schema default and leaves numbers without one unset', async () => {
|
|
33
|
+
const wrapper = mount(Schema, {
|
|
34
|
+
props: {
|
|
35
|
+
modelValue: {},
|
|
36
|
+
schema: {
|
|
37
|
+
type: 'object',
|
|
38
|
+
properties: {
|
|
39
|
+
Name: { type: 'string', default: 'Fred' },
|
|
40
|
+
Count: { type: 'integer', default: 5 },
|
|
41
|
+
Ratio: { type: 'number' },
|
|
42
|
+
Flag: { type: 'boolean', default: true },
|
|
43
|
+
Tags: { type: 'array', items: { type: 'string' } },
|
|
44
|
+
},
|
|
45
|
+
},
|
|
46
|
+
},
|
|
47
|
+
})
|
|
48
|
+
|
|
49
|
+
await flushPromises()
|
|
50
|
+
|
|
51
|
+
const data = lastEmitted(wrapper)
|
|
52
|
+
expect(data).toEqual({ Name: 'Fred', Count: 5, Flag: true, Tags: [] })
|
|
53
|
+
expect('Ratio' in data).toBe(false)
|
|
54
|
+
})
|
|
55
|
+
|
|
56
|
+
it('renders integers with a whole number step and numbers with any step', async () => {
|
|
57
|
+
const wrapper = mount(Schema, {
|
|
58
|
+
props: {
|
|
59
|
+
modelValue: {},
|
|
60
|
+
schema: {
|
|
61
|
+
type: 'object',
|
|
62
|
+
properties: {
|
|
63
|
+
Count: { type: 'integer' },
|
|
64
|
+
Ratio: { type: 'number' },
|
|
65
|
+
},
|
|
66
|
+
},
|
|
67
|
+
},
|
|
68
|
+
})
|
|
69
|
+
|
|
70
|
+
await flushPromises()
|
|
71
|
+
|
|
72
|
+
const inputs = wrapper.findAll('input[type="number"]')
|
|
73
|
+
expect(inputs.map((i) => i.attributes('step'))).toEqual(['1', 'any'])
|
|
74
|
+
})
|
|
75
|
+
})
|
|
76
|
+
|
|
77
|
+
describe('TablerSchema object tables', () => {
|
|
78
|
+
it('renders an array of objects as a table with one column per property', async () => {
|
|
79
|
+
const wrapper = mount(Schema, {
|
|
80
|
+
props: {
|
|
81
|
+
modelValue: { Shares: [{ ShareId: 'a', Password: 'x' }, { ShareId: 'b', Password: '' }] },
|
|
82
|
+
schema: TABLE_SCHEMA,
|
|
83
|
+
},
|
|
84
|
+
})
|
|
85
|
+
|
|
86
|
+
await flushPromises()
|
|
87
|
+
|
|
88
|
+
expect(wrapper.findAll('thead th').map((th) => th.text())).toEqual(['ShareId', 'Password'])
|
|
89
|
+
expect(wrapper.findAll('tbody tr').length).toBe(2)
|
|
90
|
+
expect(wrapper.find('[title="Add Row"]').exists()).toBe(true)
|
|
91
|
+
})
|
|
92
|
+
|
|
93
|
+
it('hides the editing controls when disabled', async () => {
|
|
94
|
+
const wrapper = mount(Schema, {
|
|
95
|
+
props: {
|
|
96
|
+
modelValue: { Shares: [] },
|
|
97
|
+
schema: TABLE_SCHEMA,
|
|
98
|
+
disabled: true,
|
|
99
|
+
},
|
|
100
|
+
})
|
|
101
|
+
|
|
102
|
+
await flushPromises()
|
|
103
|
+
|
|
104
|
+
expect(wrapper.find('[title="Add Row"]').exists()).toBe(false)
|
|
105
|
+
expect(wrapper.find('.btn-primary').exists()).toBe(false)
|
|
106
|
+
})
|
|
107
|
+
|
|
108
|
+
it('adds a row through the row editor modal', async () => {
|
|
109
|
+
const wrapper = mount(Schema, {
|
|
110
|
+
attachTo: document.body,
|
|
111
|
+
props: {
|
|
112
|
+
modelValue: { Shares: [] },
|
|
113
|
+
schema: TABLE_SCHEMA,
|
|
114
|
+
},
|
|
115
|
+
})
|
|
116
|
+
|
|
117
|
+
await flushPromises()
|
|
118
|
+
await wrapper.find('[title="Add Row"]').trigger('click')
|
|
119
|
+
await flushPromises()
|
|
120
|
+
|
|
121
|
+
expect(document.body.querySelector('.modal-title')?.textContent).toBe('Row Editor')
|
|
122
|
+
|
|
123
|
+
const input = document.body.querySelector('.modal-body input') as HTMLInputElement
|
|
124
|
+
input.value = 'abc'
|
|
125
|
+
input.dispatchEvent(new Event('input'))
|
|
126
|
+
await flushPromises()
|
|
127
|
+
|
|
128
|
+
;(document.body.querySelector('.modal-body .btn-primary') as HTMLElement).click()
|
|
129
|
+
await flushPromises()
|
|
130
|
+
|
|
131
|
+
expect(lastEmitted(wrapper)).toEqual({ Shares: [{ ShareId: 'abc', Password: '' }] })
|
|
132
|
+
expect(document.body.querySelector('.modal-title')).toBeNull()
|
|
133
|
+
})
|
|
134
|
+
|
|
135
|
+
it('imports comma or tab separated rows into the table', async () => {
|
|
136
|
+
const wrapper = mount(Schema, {
|
|
137
|
+
attachTo: document.body,
|
|
138
|
+
props: {
|
|
139
|
+
modelValue: { Shares: [{ ShareId: 'z', Password: '' }] },
|
|
140
|
+
schema: TABLE_SCHEMA,
|
|
141
|
+
},
|
|
142
|
+
})
|
|
143
|
+
|
|
144
|
+
await flushPromises()
|
|
145
|
+
await wrapper.find('[title="Import CSV"]').trigger('click')
|
|
146
|
+
await flushPromises()
|
|
147
|
+
|
|
148
|
+
const textarea = document.body.querySelector('.modal-body textarea') as HTMLTextAreaElement
|
|
149
|
+
textarea.value = 'a,1\nb, 2\n\n'
|
|
150
|
+
textarea.dispatchEvent(new Event('input'))
|
|
151
|
+
await flushPromises()
|
|
152
|
+
|
|
153
|
+
;(document.body.querySelector('.modal-body .btn-primary') as HTMLElement).click()
|
|
154
|
+
await flushPromises()
|
|
155
|
+
|
|
156
|
+
expect(lastEmitted(wrapper)).toEqual({
|
|
157
|
+
Shares: [
|
|
158
|
+
{ ShareId: 'z', Password: '' },
|
|
159
|
+
{ ShareId: 'a', Password: '1' },
|
|
160
|
+
{ ShareId: 'b', Password: '2' },
|
|
161
|
+
],
|
|
162
|
+
})
|
|
163
|
+
})
|
|
164
|
+
})
|
|
165
|
+
|
|
166
|
+
const UNION_SCHEMA = {
|
|
167
|
+
type: 'object',
|
|
168
|
+
required: ['Source'],
|
|
169
|
+
properties: {
|
|
170
|
+
Source: {
|
|
171
|
+
anyOf: [
|
|
172
|
+
{
|
|
173
|
+
title: 'Single Map',
|
|
174
|
+
type: 'object',
|
|
175
|
+
required: ['Mode', 'MapId'],
|
|
176
|
+
properties: {
|
|
177
|
+
Mode: { const: 'Map', type: 'string' },
|
|
178
|
+
MapId: { type: 'string' },
|
|
179
|
+
},
|
|
180
|
+
},
|
|
181
|
+
{
|
|
182
|
+
title: 'Team Account',
|
|
183
|
+
type: 'object',
|
|
184
|
+
required: ['Mode', 'TeamId'],
|
|
185
|
+
properties: {
|
|
186
|
+
Mode: { const: 'Team', type: 'string' },
|
|
187
|
+
TeamId: { type: 'string' },
|
|
188
|
+
Verbose: { type: 'boolean', default: true },
|
|
189
|
+
},
|
|
190
|
+
},
|
|
191
|
+
],
|
|
192
|
+
},
|
|
193
|
+
},
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
describe('TablerSchema nested objects', () => {
|
|
197
|
+
it('renders an object property as a nested form and emits the nested value', async () => {
|
|
198
|
+
const wrapper = mount(Schema, {
|
|
199
|
+
props: {
|
|
200
|
+
modelValue: {},
|
|
201
|
+
schema: {
|
|
202
|
+
type: 'object',
|
|
203
|
+
properties: {
|
|
204
|
+
Auth: {
|
|
205
|
+
type: 'object',
|
|
206
|
+
properties: {
|
|
207
|
+
Username: { type: 'string' },
|
|
208
|
+
Remember: { type: 'boolean' },
|
|
209
|
+
},
|
|
210
|
+
},
|
|
211
|
+
},
|
|
212
|
+
},
|
|
213
|
+
},
|
|
214
|
+
})
|
|
215
|
+
|
|
216
|
+
await flushPromises()
|
|
217
|
+
|
|
218
|
+
const input = wrapper.find('input[type="text"]')
|
|
219
|
+
await input.setValue('fred')
|
|
220
|
+
await flushPromises()
|
|
221
|
+
|
|
222
|
+
expect(lastEmitted(wrapper)).toEqual({ Auth: { Username: 'fred', Remember: false } })
|
|
223
|
+
expect(wrapper.find('textarea').exists()).toBe(false)
|
|
224
|
+
})
|
|
225
|
+
})
|
|
226
|
+
|
|
227
|
+
describe('TablerSchema unions', () => {
|
|
228
|
+
it('seeds the first variant and hides the const discriminator', async () => {
|
|
229
|
+
const wrapper = mount(Schema, {
|
|
230
|
+
props: { modelValue: {}, schema: UNION_SCHEMA },
|
|
231
|
+
})
|
|
232
|
+
|
|
233
|
+
await flushPromises()
|
|
234
|
+
|
|
235
|
+
const select = wrapper.find('select')
|
|
236
|
+
expect(select.findAll('option').map((o) => o.text())).toEqual(['Single Map', 'Team Account'])
|
|
237
|
+
expect((select.element as HTMLSelectElement).value).toBe('Single Map')
|
|
238
|
+
expect(wrapper.findAll('input[type="text"]').length).toBe(1)
|
|
239
|
+
expect(lastEmitted(wrapper)).toEqual({ Source: { Mode: 'Map', MapId: '' } })
|
|
240
|
+
})
|
|
241
|
+
|
|
242
|
+
it('selects the variant matching a stored discriminator', async () => {
|
|
243
|
+
const wrapper = mount(Schema, {
|
|
244
|
+
props: {
|
|
245
|
+
modelValue: { Source: { Mode: 'Team', TeamId: 'abc', Verbose: false } },
|
|
246
|
+
schema: UNION_SCHEMA,
|
|
247
|
+
},
|
|
248
|
+
})
|
|
249
|
+
|
|
250
|
+
await flushPromises()
|
|
251
|
+
|
|
252
|
+
expect((wrapper.find('select').element as HTMLSelectElement).value).toBe('Team Account')
|
|
253
|
+
expect((wrapper.find('input[type="text"]').element as HTMLInputElement).value).toBe('abc')
|
|
254
|
+
})
|
|
255
|
+
|
|
256
|
+
it('resets the value to the new variant when switched', async () => {
|
|
257
|
+
const wrapper = mount(Schema, {
|
|
258
|
+
props: {
|
|
259
|
+
modelValue: { Source: { Mode: 'Map', MapId: 'xyz' } },
|
|
260
|
+
schema: UNION_SCHEMA,
|
|
261
|
+
},
|
|
262
|
+
})
|
|
263
|
+
|
|
264
|
+
await flushPromises()
|
|
265
|
+
await wrapper.find('select').setValue('Team Account')
|
|
266
|
+
await flushPromises()
|
|
267
|
+
|
|
268
|
+
expect(lastEmitted(wrapper)).toEqual({ Source: { Mode: 'Team', TeamId: '', Verbose: true } })
|
|
269
|
+
expect(wrapper.find('input[type="checkbox"]').exists()).toBe(true)
|
|
270
|
+
})
|
|
271
|
+
|
|
272
|
+
it('does not treat a nullable anyOf as a union', async () => {
|
|
273
|
+
const wrapper = mount(Schema, {
|
|
274
|
+
props: {
|
|
275
|
+
modelValue: {},
|
|
276
|
+
schema: {
|
|
277
|
+
type: 'object',
|
|
278
|
+
properties: {
|
|
279
|
+
Note: { anyOf: [{ type: 'string' }, { type: 'null' }] },
|
|
280
|
+
},
|
|
281
|
+
},
|
|
282
|
+
},
|
|
283
|
+
})
|
|
284
|
+
|
|
285
|
+
await flushPromises()
|
|
286
|
+
|
|
287
|
+
expect(wrapper.find('select').exists()).toBe(false)
|
|
288
|
+
})
|
|
289
|
+
})
|
|
290
|
+
|
|
291
|
+
const PRIMITIVE_SCHEMA = {
|
|
292
|
+
type: 'object',
|
|
293
|
+
properties: {
|
|
294
|
+
Name: { type: 'string' },
|
|
295
|
+
Count: { type: 'integer' },
|
|
296
|
+
Ratio: { type: 'number' },
|
|
297
|
+
Flag: { type: 'boolean' },
|
|
298
|
+
Level: { type: 'string', enum: ['low', 'high'] },
|
|
299
|
+
Roles: { type: 'array', uniqueItems: true, items: { type: 'string', enum: ['admin', 'user'] } },
|
|
300
|
+
},
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
describe('TablerSchema editing primitives', () => {
|
|
304
|
+
it('emits typed values for string, integer, number and boolean inputs', async () => {
|
|
305
|
+
const wrapper = mount(Schema, {
|
|
306
|
+
props: { modelValue: {}, schema: PRIMITIVE_SCHEMA },
|
|
307
|
+
})
|
|
308
|
+
|
|
309
|
+
await flushPromises()
|
|
310
|
+
|
|
311
|
+
await wrapper.find('input[type="text"]').setValue('Fred')
|
|
312
|
+
const numbers = wrapper.findAll('input[type="number"]')
|
|
313
|
+
await numbers[0].setValue('3')
|
|
314
|
+
await numbers[1].setValue('1.5')
|
|
315
|
+
await wrapper.find('.form-switch input').setValue(true)
|
|
316
|
+
await flushPromises()
|
|
317
|
+
|
|
318
|
+
const data = lastEmitted(wrapper)
|
|
319
|
+
expect(data.Name).toBe('Fred')
|
|
320
|
+
expect(data.Count).toBe(3)
|
|
321
|
+
expect(data.Ratio).toBe(1.5)
|
|
322
|
+
expect(data.Flag).toBe(true)
|
|
323
|
+
})
|
|
324
|
+
|
|
325
|
+
it('emits the selected enum option', async () => {
|
|
326
|
+
const wrapper = mount(Schema, {
|
|
327
|
+
props: { modelValue: {}, schema: PRIMITIVE_SCHEMA },
|
|
328
|
+
})
|
|
329
|
+
|
|
330
|
+
await flushPromises()
|
|
331
|
+
await wrapper.find('select').setValue('high')
|
|
332
|
+
await flushPromises()
|
|
333
|
+
|
|
334
|
+
expect(lastEmitted(wrapper).Level).toBe('high')
|
|
335
|
+
})
|
|
336
|
+
|
|
337
|
+
it('toggles multi enum options in and out of the array', async () => {
|
|
338
|
+
const wrapper = mount(Schema, {
|
|
339
|
+
props: { modelValue: {}, schema: PRIMITIVE_SCHEMA },
|
|
340
|
+
})
|
|
341
|
+
|
|
342
|
+
await flushPromises()
|
|
343
|
+
|
|
344
|
+
const boxes = wrapper.findAll('.form-check input[type="checkbox"]')
|
|
345
|
+
expect(boxes.length).toBe(2)
|
|
346
|
+
|
|
347
|
+
await boxes[0].trigger('change')
|
|
348
|
+
await boxes[1].trigger('change')
|
|
349
|
+
await flushPromises()
|
|
350
|
+
expect(lastEmitted(wrapper).Roles).toEqual(['admin', 'user'])
|
|
351
|
+
|
|
352
|
+
await boxes[0].trigger('change')
|
|
353
|
+
await flushPromises()
|
|
354
|
+
expect(lastEmitted(wrapper).Roles).toEqual(['user'])
|
|
355
|
+
})
|
|
356
|
+
|
|
357
|
+
it('renders a multi line string as a textarea and keeps newlines', async () => {
|
|
358
|
+
const wrapper = mount(Schema, {
|
|
359
|
+
props: {
|
|
360
|
+
modelValue: { Name: 'one\ntwo' },
|
|
361
|
+
schema: { type: 'object', properties: { Name: { type: 'string' } } },
|
|
362
|
+
},
|
|
363
|
+
})
|
|
364
|
+
|
|
365
|
+
await flushPromises()
|
|
366
|
+
|
|
367
|
+
const textarea = wrapper.find('textarea')
|
|
368
|
+
expect(textarea.exists()).toBe(true)
|
|
369
|
+
await textarea.setValue('one\ntwo\nthree')
|
|
370
|
+
await flushPromises()
|
|
371
|
+
|
|
372
|
+
expect(lastEmitted(wrapper).Name).toBe('one\ntwo\nthree')
|
|
373
|
+
})
|
|
374
|
+
|
|
375
|
+
it('disables every primitive input when disabled', async () => {
|
|
376
|
+
const wrapper = mount(Schema, {
|
|
377
|
+
props: { modelValue: {}, schema: PRIMITIVE_SCHEMA, disabled: true },
|
|
378
|
+
})
|
|
379
|
+
|
|
380
|
+
await flushPromises()
|
|
381
|
+
|
|
382
|
+
const controls = wrapper.findAll('input, select')
|
|
383
|
+
expect(controls.length).toBeGreaterThan(0)
|
|
384
|
+
expect(controls.every((c) => (c.element as HTMLInputElement).disabled)).toBe(true)
|
|
385
|
+
})
|
|
386
|
+
})
|
|
387
|
+
|
|
388
|
+
const DEFAULT_SCHEMA = {
|
|
389
|
+
type: 'object',
|
|
390
|
+
properties: {
|
|
391
|
+
Name: { type: 'string', default: 'Fred' },
|
|
392
|
+
Count: { type: 'integer', default: 5 },
|
|
393
|
+
Level: { type: 'string', enum: ['low', 'high'], default: 'high' },
|
|
394
|
+
Tags: { type: 'array', items: { type: 'string' }, default: ['a'] },
|
|
395
|
+
Kind: { type: 'string', const: 'thing' },
|
|
396
|
+
Auth: {
|
|
397
|
+
type: 'object',
|
|
398
|
+
properties: {
|
|
399
|
+
Remember: { type: 'boolean', default: true },
|
|
400
|
+
Username: { type: 'string' },
|
|
401
|
+
},
|
|
402
|
+
},
|
|
403
|
+
},
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
describe('TablerSchema applying default values', () => {
|
|
407
|
+
it('keeps existing values and only fills missing keys', async () => {
|
|
408
|
+
const wrapper = mount(Schema, {
|
|
409
|
+
props: {
|
|
410
|
+
modelValue: { Name: 'Bob', Tags: [] },
|
|
411
|
+
schema: DEFAULT_SCHEMA,
|
|
412
|
+
},
|
|
413
|
+
})
|
|
414
|
+
|
|
415
|
+
await flushPromises()
|
|
416
|
+
|
|
417
|
+
const data = lastEmitted(wrapper)
|
|
418
|
+
expect(data.Name).toBe('Bob')
|
|
419
|
+
expect(data.Tags).toEqual([])
|
|
420
|
+
expect(data.Count).toBe(5)
|
|
421
|
+
expect(data.Level).toBe('high')
|
|
422
|
+
})
|
|
423
|
+
|
|
424
|
+
it('seeds a root level const and hides it from the form', async () => {
|
|
425
|
+
const wrapper = mount(Schema, {
|
|
426
|
+
props: { modelValue: {}, schema: DEFAULT_SCHEMA },
|
|
427
|
+
})
|
|
428
|
+
|
|
429
|
+
await flushPromises()
|
|
430
|
+
|
|
431
|
+
expect(lastEmitted(wrapper).Kind).toBe('thing')
|
|
432
|
+
expect(wrapper.findAll('label').map((l) => l.text())).not.toContain('Kind')
|
|
433
|
+
})
|
|
434
|
+
|
|
435
|
+
it('seeds nested object defaults through the nested form', async () => {
|
|
436
|
+
const wrapper = mount(Schema, {
|
|
437
|
+
props: { modelValue: {}, schema: DEFAULT_SCHEMA },
|
|
438
|
+
})
|
|
439
|
+
|
|
440
|
+
await flushPromises()
|
|
441
|
+
|
|
442
|
+
expect(lastEmitted(wrapper).Auth).toEqual({ Remember: true, Username: '' })
|
|
443
|
+
})
|
|
444
|
+
|
|
445
|
+
it('clones array defaults so edits do not leak back into the schema', async () => {
|
|
446
|
+
const schema = JSON.parse(JSON.stringify(DEFAULT_SCHEMA))
|
|
447
|
+
const wrapper = mount(Schema, {
|
|
448
|
+
props: { modelValue: {}, schema },
|
|
449
|
+
})
|
|
450
|
+
|
|
451
|
+
await flushPromises()
|
|
452
|
+
|
|
453
|
+
const tags = lastEmitted(wrapper).Tags as string[]
|
|
454
|
+
expect(tags).toEqual(['a'])
|
|
455
|
+
tags.push('b')
|
|
456
|
+
expect(schema.properties.Tags.default).toEqual(['a'])
|
|
457
|
+
})
|
|
458
|
+
|
|
459
|
+
it('applies defaults again when the parent replaces the model value', async () => {
|
|
460
|
+
const wrapper = mount(Schema, {
|
|
461
|
+
props: { modelValue: { Name: 'Bob' }, schema: DEFAULT_SCHEMA },
|
|
462
|
+
})
|
|
463
|
+
|
|
464
|
+
await flushPromises()
|
|
465
|
+
await wrapper.setProps({ modelValue: { Name: 'Zed' } })
|
|
466
|
+
await flushPromises()
|
|
467
|
+
|
|
468
|
+
const data = lastEmitted(wrapper)
|
|
469
|
+
expect(data.Name).toBe('Zed')
|
|
470
|
+
expect(data.Count).toBe(5)
|
|
471
|
+
expect(data.Kind).toBe('thing')
|
|
472
|
+
})
|
|
473
|
+
|
|
474
|
+
it('applies defaults for properties added when the schema changes', async () => {
|
|
475
|
+
const wrapper = mount(Schema, {
|
|
476
|
+
props: {
|
|
477
|
+
modelValue: {},
|
|
478
|
+
schema: { type: 'object', properties: { Name: { type: 'string' } } },
|
|
479
|
+
},
|
|
480
|
+
})
|
|
481
|
+
|
|
482
|
+
await flushPromises()
|
|
483
|
+
expect(lastEmitted(wrapper)).toEqual({ Name: '' })
|
|
484
|
+
|
|
485
|
+
await wrapper.setProps({
|
|
486
|
+
schema: {
|
|
487
|
+
type: 'object',
|
|
488
|
+
required: ['Name'],
|
|
489
|
+
properties: {
|
|
490
|
+
Name: { type: 'string' },
|
|
491
|
+
Count: { type: 'integer', default: 7 },
|
|
492
|
+
},
|
|
493
|
+
},
|
|
494
|
+
})
|
|
495
|
+
await flushPromises()
|
|
496
|
+
|
|
497
|
+
expect(lastEmitted(wrapper)).toEqual({ Name: '', Count: 7 })
|
|
498
|
+
expect(wrapper.find('.text-red').exists()).toBe(true)
|
|
499
|
+
})
|
|
500
|
+
|
|
501
|
+
it('re-detects the union variant when the parent replaces the model value', async () => {
|
|
502
|
+
const wrapper = mount(Schema, {
|
|
503
|
+
props: {
|
|
504
|
+
modelValue: { Source: { Mode: 'Map', MapId: 'x' } },
|
|
505
|
+
schema: UNION_SCHEMA,
|
|
506
|
+
},
|
|
507
|
+
})
|
|
508
|
+
|
|
509
|
+
await flushPromises()
|
|
510
|
+
expect((wrapper.find('select').element as HTMLSelectElement).value).toBe('Single Map')
|
|
511
|
+
|
|
512
|
+
await wrapper.setProps({ modelValue: { Source: { Mode: 'Team', TeamId: 't' } } })
|
|
513
|
+
await flushPromises()
|
|
514
|
+
|
|
515
|
+
expect((wrapper.find('select').element as HTMLSelectElement).value).toBe('Team Account')
|
|
516
|
+
expect(lastEmitted(wrapper)).toEqual({ Source: { Mode: 'Team', TeamId: 't', Verbose: true } })
|
|
517
|
+
})
|
|
518
|
+
})
|
package/vitest.config.mjs
CHANGED