@tak-ps/vue-tabler 5.3.0 → 5.5.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/CHANGELOG.md CHANGED
@@ -12,6 +12,21 @@
12
12
 
13
13
  ### Pending
14
14
 
15
+ ### v5.5.0
16
+
17
+ - :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
18
+ - :rocket: Seed missing `TablerSchema` values from the JSON Schema `default` and leave numbers without a default unset instead of an empty string
19
+ - :rocket: Add a `step` prop to `TablerInput`, set to `1` for integer and `any` for number fields by `TablerSchema`
20
+ - :bug: Fix `TablerSchema` self reference for nested arrays and crash when adding to an array without `items`
21
+
22
+ ### v5.4.0
23
+
24
+ - :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
25
+ - :rocket: Use human readable field type names in `TablerSchemaBuilder` (Text, Dropdown, Toggle, Number, Whole Number) instead of raw JSON Schema types, with a matching icon per type
26
+ - :bug: Shift a `TablerDropdown` menu up to stay within the viewport instead of shrinking it into a scrolling panel when the trigger sits near the bottom of the screen
27
+ - :tada: Add an `advanced` scoped slot to `TablerAlert` and `TablerError` to customize how the error body is rendered
28
+ - :rocket: Wrap long lines in the `TablerAlert` Advanced body
29
+
15
30
  ### v5.3.0
16
31
 
17
32
  - :arrow_up: Update all Core Deps
@@ -51,18 +51,24 @@
51
51
  />
52
52
  <span>Advanced</span>
53
53
  </div>
54
- <pre
54
+ <slot
55
55
  v-if='open'
56
- class='my-3'
57
- v-text='err.body || err.stack'
58
- />
56
+ name='advanced'
57
+ :body='body'
58
+ >
59
+ <pre
60
+ class='my-3'
61
+ style='white-space: pre-wrap; word-break: break-word;'
62
+ v-text='body'
63
+ />
64
+ </slot>
59
65
  </div>
60
66
  </template>
61
67
  </div>
62
68
  </template>
63
69
 
64
70
  <script setup lang="ts">
65
- import { ref } from 'vue'
71
+ import { ref, computed } from 'vue'
66
72
  import {
67
73
  IconAlertTriangle,
68
74
  IconChevronRight,
@@ -71,17 +77,26 @@ import {
71
77
 
72
78
  export interface AlertProps {
73
79
  title?: string;
74
- err?: Error & { body?: string };
80
+ err?: Error & { body?: string | Record<string, unknown> };
75
81
  compact?: boolean;
76
82
  advanced?: boolean;
77
83
  }
78
84
 
79
- withDefaults(defineProps<AlertProps>(), {
85
+ const props = withDefaults(defineProps<AlertProps>(), {
80
86
  title: 'Generic Error',
81
87
  err: () => new Error('Something is amiss'),
82
88
  compact: false,
83
89
  advanced: true
84
90
  });
85
91
 
92
+ defineSlots<{
93
+ advanced?: (props: { body: string }) => unknown;
94
+ }>()
95
+
86
96
  const open = ref(false)
97
+
98
+ const body = computed<string>(() => {
99
+ const raw = props.err.body || props.err.stack || '';
100
+ return typeof raw === 'string' ? raw : JSON.stringify(raw, null, 4);
101
+ })
87
102
  </script>
@@ -119,11 +119,11 @@ function calcPosition() {
119
119
  left = t.right - m.width;
120
120
  }
121
121
 
122
- // Keep within viewport
122
+ // Keep within viewport - shift the menu rather than shrink it, so only a
123
+ // menu taller than the viewport itself ends up scrolling
123
124
  left = Math.max(gap, Math.min(left, vw - m.width - gap));
124
- top = Math.max(gap, Math.min(top, vh - gap));
125
+ top = Math.max(gap, Math.min(top, vh - m.height - gap));
125
126
 
126
- // Constrain height to available space below the computed top
127
127
  const maxHeight = Math.max(100, vh - top - gap);
128
128
 
129
129
  floatingStyle.value = {
@@ -11,7 +11,17 @@
11
11
  <Alert
12
12
  :title='title'
13
13
  :err='err'
14
- />
14
+ >
15
+ <template
16
+ v-if='$slots.advanced'
17
+ #advanced='slotProps'
18
+ >
19
+ <slot
20
+ name='advanced'
21
+ v-bind='slotProps'
22
+ />
23
+ </template>
24
+ </Alert>
15
25
 
16
26
  <div class='modal-footer'>
17
27
  <div class='w-100'>
@@ -43,6 +53,10 @@ withDefaults(defineProps<ErrProps>(), {
43
53
  trace: true
44
54
  });
45
55
 
56
+ defineSlots<{
57
+ advanced?: (props: { body: string }) => unknown;
58
+ }>()
59
+
46
60
  const emit = defineEmits<{
47
61
  (e: 'close'): void
48
62
  }>()
@@ -7,7 +7,17 @@
7
7
  :key='key'
8
8
  class='py-2 floating-input'
9
9
  >
10
- <template v-if='s.properties[key].enum'>
10
+ <template v-if='isMultiEnum(s.properties[key])'>
11
+ <TablerMultiEnum
12
+ v-model='data[key]'
13
+ :label='key'
14
+ :disabled='disabled'
15
+ :required='s.properties[key].required || false'
16
+ :description='s.properties[key].description || ""'
17
+ :options='s.properties[key].items.enum'
18
+ />
19
+ </template>
20
+ <template v-else-if='s.properties[key].enum'>
11
21
  <TablerEnum
12
22
  v-model='data[key]'
13
23
  :label='key'
@@ -32,6 +42,7 @@
32
42
  <TablerInput
33
43
  v-model='data[key]'
34
44
  :type='s.properties[key].type'
45
+ :step='s.properties[key].type === "integer" ? 1 : "any"'
35
46
  :label='key'
36
47
  :disabled='disabled'
37
48
  :required='s.properties[key].required || false'
@@ -47,6 +58,86 @@
47
58
  :description='s.properties[key].description || ""'
48
59
  />
49
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>
50
141
  <template v-else-if='s.properties[key].type === "array"'>
51
142
  <div class='d-flex'>
52
143
  <label
@@ -103,20 +194,92 @@
103
194
  </template>
104
195
  </div>
105
196
  </template>
197
+
198
+ <TablerModal v-if='edit.shown'>
199
+ <button
200
+ type='button'
201
+ class='btn-close'
202
+ aria-label='Close'
203
+ @click='edit.shown = false'
204
+ />
205
+ <div class='modal-status bg-yellow' />
206
+ <div class='modal-header'>
207
+ <span class='modal-title'>Row Editor</span>
208
+ <div class='ms-auto'>
209
+ <TablerDelete
210
+ v-if='!disabled && edit.index !== null'
211
+ displaytype='icon'
212
+ @delete='removeRow'
213
+ />
214
+ </div>
215
+ </div>
216
+ <div class='modal-body py-4'>
217
+ <TablerSchema
218
+ v-model='edit.row'
219
+ :schema='s.properties[edit.key].items'
220
+ :disabled='disabled'
221
+ />
222
+
223
+ <button
224
+ v-if='!disabled'
225
+ class='btn btn-primary w-100 mt-4'
226
+ @click='saveRow'
227
+ >
228
+ Done
229
+ </button>
230
+ </div>
231
+ </TablerModal>
232
+
233
+ <TablerModal v-if='csv.shown'>
234
+ <button
235
+ type='button'
236
+ class='btn-close'
237
+ aria-label='Close'
238
+ @click='csv.shown = false'
239
+ />
240
+ <div class='modal-status bg-yellow' />
241
+ <div class='modal-header'>
242
+ <span class='modal-title'>Import CSV</span>
243
+ </div>
244
+ <div class='modal-body py-4'>
245
+ <TablerInput
246
+ v-model='csv.text'
247
+ label='CSV'
248
+ :rows='10'
249
+ :description='"Rows of " + columns(csv.key).join(", ") + " separated by commas or tabs"'
250
+ />
251
+
252
+ <button
253
+ class='btn btn-primary w-100 mt-4'
254
+ @click='importCSV'
255
+ >
256
+ Import
257
+ </button>
258
+ </div>
259
+ </TablerModal>
106
260
  </div>
107
261
  </template>
108
262
 
109
263
  <script setup lang="ts">
110
- import { ref, watch, onMounted } from 'vue'
264
+ import { ref, reactive, watch, onMounted } from 'vue'
111
265
  import TablerInput from './input/Input.vue';
112
266
  import TablerToggle from './input/Toggle.vue';
113
267
  import TablerEnum from './input/Enum.vue';
268
+ import TablerMultiEnum from './input/MultiEnum.vue';
114
269
  import TablerLoading from './Loading.vue';
270
+ import TablerModal from './Modal.vue';
271
+ import TablerDelete from './Delete.vue';
272
+ import TablerNone from './None.vue';
273
+ import TablerIconButton from './IconButton.vue';
274
+ import { isMultiEnum } from './schema-types';
115
275
  import {
116
276
  IconPlus,
117
277
  IconTrash,
278
+ IconDatabaseImport,
118
279
  } from '@tabler/icons-vue';
119
280
 
281
+ defineOptions({ name: 'TablerSchema' });
282
+
120
283
  export interface SchemaProps {
121
284
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
122
285
  modelValue: any;
@@ -140,19 +303,105 @@ const s = ref<any>({});
140
303
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
141
304
  const data = ref<any>({});
142
305
 
306
+ const edit = reactive<{
307
+ shown: boolean;
308
+ key: string;
309
+ index: number | null;
310
+ row: Record<string, unknown>;
311
+ }>({
312
+ shown: false,
313
+ key: '',
314
+ index: null,
315
+ row: {}
316
+ });
317
+
318
+ const csv = reactive({
319
+ shown: false,
320
+ key: '',
321
+ text: ''
322
+ });
323
+
324
+ /** An array of objects with known properties is rendered as a table with a row editor */
325
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
326
+ const isObjectTable = (prop: any): boolean => {
327
+ return prop.type === 'array'
328
+ && prop.items
329
+ && prop.items.type === 'object'
330
+ && !!prop.items.properties;
331
+ }
332
+
333
+ const columns = (key: string): string[] => {
334
+ const prop = s.value.properties[key];
335
+ if (!prop || !isObjectTable(prop)) return [];
336
+ return Object.keys(prop.items.properties);
337
+ }
338
+
143
339
  const push = (key: string) => {
144
- if (!props.schema.properties[key].items) data.value[key].push('');
145
- if (props.schema.properties[key].items.type === 'object') {
340
+ const items = props.schema.properties[key].items;
341
+
342
+ if (!items || items.type === 'string') {
343
+ data.value[key].push('');
344
+ } else if (items.type === 'object') {
146
345
  data.value[key].push({});
147
- } else if (props.schema.properties[key].items.type === 'array') {
346
+ } else if (items.type === 'array') {
148
347
  data.value[key].push([]);
149
- } else if (props.schema.properties[key].items.type === 'boolean') {
348
+ } else if (items.type === 'boolean') {
150
349
  data.value[key].push(false);
151
350
  } else {
152
351
  data.value[key].push('');
153
352
  }
154
353
  }
155
354
 
355
+ const openRow = (key: string, index?: number) => {
356
+ edit.key = key;
357
+ edit.index = index ?? null;
358
+ edit.row = index === undefined ? {} : JSON.parse(JSON.stringify(data.value[key][index]));
359
+ edit.shown = true;
360
+ }
361
+
362
+ const saveRow = () => {
363
+ const row = JSON.parse(JSON.stringify(edit.row));
364
+
365
+ if (edit.index === null) {
366
+ data.value[edit.key].push(row);
367
+ } else {
368
+ data.value[edit.key][edit.index] = row;
369
+ }
370
+
371
+ edit.shown = false;
372
+ }
373
+
374
+ const removeRow = () => {
375
+ if (edit.index !== null) {
376
+ data.value[edit.key].splice(edit.index, 1);
377
+ }
378
+
379
+ edit.shown = false;
380
+ }
381
+
382
+ const openImport = (key: string) => {
383
+ csv.key = key;
384
+ csv.text = '';
385
+ csv.shown = true;
386
+ }
387
+
388
+ const importCSV = () => {
389
+ const headers = columns(csv.key);
390
+ const lines = csv.text.split('\n').filter((line) => line.trim().length);
391
+ const delimiter = lines.length && lines[0].includes(',') ? ',' : '\t';
392
+
393
+ for (const line of lines) {
394
+ const cells = line.split(delimiter);
395
+ const row: Record<string, string> = {};
396
+ for (let i = 0; i < headers.length; i++) {
397
+ row[headers[i]] = (cells[i] ?? '').trim();
398
+ }
399
+ data.value[csv.key].push(row);
400
+ }
401
+
402
+ csv.shown = false;
403
+ }
404
+
156
405
  // Watch for data changes
157
406
  watch(data, () => {
158
407
  emit('update:modelValue', data.value);
@@ -162,24 +411,29 @@ const updateSchema = () => {
162
411
  s.value = JSON.parse(JSON.stringify(props.schema));
163
412
  if (s.value.type === 'object' && s.value.properties) {
164
413
  for (const req of (s.value.required || [])) {
165
- s.value.properties[req].required = true;
414
+ if (s.value.properties[req]) s.value.properties[req].required = true;
166
415
  }
167
416
  }
168
417
  }
169
418
 
419
+ /** Seed missing keys from the schema default, falling back to an empty value of the right shape */
170
420
  const updateDataDefaults = () => {
171
421
  if (s.value.type === 'object' && s.value.properties) {
172
422
  for (const key in s.value.properties) {
173
423
  if (data.value[key] !== undefined) continue;
174
424
 
175
- const type = s.value.properties[key].type;
425
+ const prop = s.value.properties[key];
176
426
 
177
- if (type === 'array') {
427
+ if (prop.default !== undefined) {
428
+ data.value[key] = JSON.parse(JSON.stringify(prop.default));
429
+ } else if (prop.type === 'array') {
178
430
  data.value[key] = [];
179
- } else if (type === 'boolean') {
431
+ } else if (prop.type === 'boolean') {
180
432
  data.value[key] = false;
181
- } else if (type === 'object') {
433
+ } else if (prop.type === 'object') {
182
434
  data.value[key] = {};
435
+ } else if (prop.type === 'number' || prop.type === 'integer') {
436
+ continue;
183
437
  } else {
184
438
  data.value[key] = '';
185
439
  }
@@ -27,55 +27,18 @@
27
27
  >
28
28
  <div class='m-1 text-center'>
29
29
  <div
30
- class='opt cursor-pointer py-1'
31
- @click='create({
32
- "name": "",
33
- "type": "string",
34
- "required": false,
35
- })'
30
+ v-for='option in SCHEMA_TYPES'
31
+ :key='option.label'
32
+ class='opt cursor-pointer py-1 d-flex align-items-center justify-content-center'
33
+ @click='add(option)'
36
34
  >
37
- String
38
- </div>
39
- <div
40
- class='opt cursor-pointer py-1'
41
- @click='create({
42
- "name": "",
43
- "type": "string",
44
- "enum": [],
45
- "required": false,
46
- })'
47
- >
48
- Enum
49
- </div>
50
- <div
51
- class='opt cursor-pointer py-1'
52
- @click='create({
53
- "name": "",
54
- "type": "boolean",
55
- "required": false,
56
- })'
57
- >
58
- Boolean
59
- </div>
60
- <div
61
- class='opt cursor-pointer py-1'
62
- @click='create({
63
- "name": "",
64
- "type": "number",
65
- "required": false,
66
- })'
67
- >
68
- Number
69
- </div>
70
- <div
71
- class='opt cursor-pointer py-1'
72
- @click='create({
73
- "name": "",
74
- "type": "integer",
75
- "required": false,
76
- })'
77
- >
78
- Integer
35
+ <component
36
+ :is='option.icon'
37
+ :size='20'
38
+ stroke='1'
39
+ class='me-2'
40
+ />
41
+ <span v-text='option.label' />
79
42
  </div>
80
43
  </div>
81
44
  </ul>
@@ -104,8 +67,17 @@
104
67
  />
105
68
  </div>
106
69
  <div class='overlay' />
70
+ <TablerMultiEnum
71
+ v-if='isMultiEnum(prop)'
72
+ :model-value='input[prop.name] || []'
73
+ :label='prop.name'
74
+ :disabled='true'
75
+ :required='prop.required || false'
76
+ :description='prop.description || ""'
77
+ :options='enumValues(prop) || []'
78
+ />
107
79
  <TablerEnum
108
- v-if='prop.enum'
80
+ v-else-if='prop.enum'
109
81
  :model-value='input[prop.name]'
110
82
  :label='prop.name'
111
83
  :disabled='true'
@@ -148,9 +120,12 @@ import {
148
120
  IconPlus,
149
121
  } from '@tabler/icons-vue';
150
122
  import BuilderEdit from './SchemaBuilderEdit.vue';
123
+ import { SCHEMA_TYPES, isMultiEnum, enumValues, schemaTemplate } from './schema-types';
124
+ import type { SchemaTypeOption } from './schema-types';
151
125
  import TablerNone from './None.vue';
152
126
  import TablerInput from './input/Input.vue';
153
127
  import TablerEnum from './input/Enum.vue';
128
+ import TablerMultiEnum from './input/MultiEnum.vue';
154
129
  import TablerToggle from './input/Toggle.vue';
155
130
  import TablerDelete from './Delete.vue';
156
131
 
@@ -231,6 +206,14 @@ onMounted(() => {
231
206
  }
232
207
  });
233
208
 
209
+ function add(option: SchemaTypeOption) {
210
+ create({
211
+ name: '',
212
+ required: false,
213
+ ...schemaTemplate(option),
214
+ } as SchemaProperty);
215
+ }
216
+
234
217
  function create(prop: SchemaProperty) {
235
218
  prop._id = Math.random().toString(36).substring(7);
236
219
  editIdx.value = null;
@@ -9,33 +9,14 @@
9
9
  <div class='modal-status bg-yellow' />
10
10
  <div class='modal-header'>
11
11
  <div class='d-flex align-items-center my-2'>
12
- <template v-if='edit.type === "string"'>
13
- <IconAlphabetLatin
14
- :size='32'
15
- stroke='1'
16
- />
17
- </template>
18
- <template v-else-if='edit.type === "number"'>
19
- <DecimalIcon
20
- :size='32'
21
- stroke='1'
22
- />
23
- </template>
24
- <template v-else-if='edit.type === "integer"'>
25
- <Sort09Icon
26
- :size='32'
27
- stroke='1'
28
- />
29
- </template>
30
- <template v-else>
31
- <BinaryIcon
32
- :size='32'
33
- stroke='1'
34
- />
35
- </template>
12
+ <component
13
+ :is='schemaType(edit).icon'
14
+ :size='32'
15
+ stroke='1'
16
+ />
36
17
  <span
37
18
  class='my-1 mx-2 strong'
38
- v-text='edit.type'
19
+ v-text='schemaType(edit).label'
39
20
  />
40
21
  </div>
41
22
  </div>
@@ -56,10 +37,9 @@
56
37
  label='Description'
57
38
  />
58
39
 
59
- <template v-if='edit.type === "string" && edit.enum === undefined' />
60
- <template v-else-if='edit.type === "string" && Array.isArray(edit.enum)'>
40
+ <template v-if='options'>
61
41
  <div class='subheader mt-2'>
62
- Enum Values
42
+ Options
63
43
  </div>
64
44
  <div class='col-12 d-flex'>
65
45
  <TablerInput
@@ -80,7 +60,7 @@
80
60
  </div>
81
61
  <div class='list-group list-group-flush mt-2'>
82
62
  <div
83
- v-for='(val, i) in edit.enum'
63
+ v-for='(val, i) in options'
84
64
  :key='i'
85
65
  class='list-group-item d-flex justify-content-between align-items-center py-1'
86
66
  >
@@ -89,13 +69,11 @@
89
69
  :size='16'
90
70
  stroke='1'
91
71
  class='cursor-pointer text-danger'
92
- @click='edit.enum?.splice(i, 1)'
72
+ @click='options.splice(i, 1)'
93
73
  />
94
74
  </div>
95
75
  </div>
96
76
  </template>
97
- <template v-else-if='edit.type === "number"' />
98
- <template v-else-if='edit.type === "integer"' />
99
77
  </div>
100
78
  </div>
101
79
  <div class='modal-footer'>
@@ -111,12 +89,12 @@
111
89
  </template>
112
90
 
113
91
  <script setup lang="ts">
114
- import { ref } from 'vue';
92
+ import { ref, computed } from 'vue';
115
93
  import {
116
- IconAlphabetLatin,
117
94
  IconPlus,
118
95
  IconTrash
119
96
  } from '@tabler/icons-vue';
97
+ import { schemaType, enumValues } from './schema-types';
120
98
  import TablerModal from './Modal.vue';
121
99
  import TablerInput from './input/Input.vue';
122
100
  import TablerToggle from './input/Toggle.vue';
@@ -127,6 +105,7 @@ interface SchemaProperty {
127
105
  required: boolean;
128
106
  description?: string;
129
107
  enum?: string[];
108
+ items?: { type: string; enum?: string[] };
130
109
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
131
110
  [key: string]: any;
132
111
  }
@@ -143,10 +122,13 @@ defineEmits<{
143
122
  const edit = ref<SchemaProperty>(JSON.parse(JSON.stringify(props.prop)));
144
123
  const newEnum = ref('');
145
124
 
125
+ /** Live option list of a Dropdown or Multiple Choice element */
126
+ const options = computed<string[] | undefined>(() => enumValues(edit.value));
127
+
146
128
  function addEnum() {
147
- if (!newEnum.value.trim()) return;
148
- if (!edit.value.enum) edit.value.enum = [];
149
- edit.value.enum.push(newEnum.value.trim());
129
+ const value = newEnum.value.trim();
130
+ if (!value || !options.value || options.value.includes(value)) return;
131
+ options.value.push(value);
150
132
  newEnum.value = '';
151
133
  }
152
134
  </script>
@@ -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: ''
@@ -0,0 +1,78 @@
1
+ <template>
2
+ <div class='row'>
3
+ <TablerLabel
4
+ v-if='label'
5
+ :label='label'
6
+ :description='description'
7
+ :required='required'
8
+ >
9
+ <slot />
10
+ </TablerLabel>
11
+ <div class='col-12'>
12
+ <label
13
+ v-for='option in options'
14
+ :key='option'
15
+ class='form-check'
16
+ >
17
+ <input
18
+ class='form-check-input'
19
+ type='checkbox'
20
+ :value='option'
21
+ :checked='current.includes(option)'
22
+ :disabled='disabled'
23
+ @change='toggle(option)'
24
+ @blur='$emit("blur")'
25
+ >
26
+ <span
27
+ class='form-check-label'
28
+ v-text='option'
29
+ />
30
+ </label>
31
+ </div>
32
+ </div>
33
+ </template>
34
+
35
+ <script setup lang="ts">
36
+ import { ref, watch } from 'vue'
37
+ import TablerLabel from '../internal/Label.vue'
38
+
39
+ export interface MultiEnumProps {
40
+ modelValue: string[];
41
+ required?: boolean;
42
+ description?: string;
43
+ options: string[];
44
+ disabled?: boolean;
45
+ label?: string;
46
+ }
47
+
48
+ const props = withDefaults(defineProps<MultiEnumProps>(), {
49
+ required: false,
50
+ description: '',
51
+ disabled: false,
52
+ label: ''
53
+ });
54
+
55
+ const emit = defineEmits<{
56
+ (e: 'blur'): void;
57
+ (e: 'update:modelValue', value: string[]): void;
58
+ }>();
59
+
60
+ const current = ref<string[]>([...(props.modelValue || [])])
61
+
62
+ watch(() => props.modelValue, (newValue) => {
63
+ current.value = [...(newValue || [])]
64
+ })
65
+
66
+ function toggle(option: string) {
67
+ if (current.value.includes(option)) {
68
+ current.value = current.value.filter((value) => value !== option)
69
+ } else {
70
+ // Preserve option order so the value is stable regardless of click order
71
+ current.value = props.options.filter((value) => {
72
+ return value === option || current.value.includes(value)
73
+ })
74
+ }
75
+
76
+ emit('update:modelValue', current.value)
77
+ }
78
+ </script>
@@ -0,0 +1,91 @@
1
+ import type { Component } from 'vue';
2
+ import {
3
+ IconDecimal,
4
+ IconSort09,
5
+ IconSelector,
6
+ IconListCheck,
7
+ IconToggleLeft,
8
+ IconAlphabetLatin,
9
+ } from '@tabler/icons-vue';
10
+
11
+ export interface SchemaTypeOption {
12
+ /** Human readable name shown in the builder */
13
+ label: string;
14
+ icon: Component;
15
+ /** JSON Schema type the option produces */
16
+ type: string;
17
+ /** Option produces a string constrained to an enum */
18
+ enum?: boolean;
19
+ /** Option produces an array of strings constrained to an enum */
20
+ multiple?: boolean;
21
+ }
22
+
23
+ export const SCHEMA_TYPES: SchemaTypeOption[] = [
24
+ { label: 'Text', icon: IconAlphabetLatin, type: 'string' },
25
+ { label: 'Dropdown', icon: IconSelector, type: 'string', enum: true },
26
+ { label: 'Multiple Choice', icon: IconListCheck, type: 'array', multiple: true },
27
+ { label: 'Toggle', icon: IconToggleLeft, type: 'boolean' },
28
+ { label: 'Number', icon: IconDecimal, type: 'number' },
29
+ { label: 'Whole Number', icon: IconSort09, type: 'integer' },
30
+ ];
31
+
32
+ /** A schema element whose value is one of a fixed set of options */
33
+ export function isEnum(prop: { enum?: unknown }): boolean {
34
+ return Array.isArray(prop.enum);
35
+ }
36
+
37
+ /** A schema element whose value is any number of a fixed set of options */
38
+ export function isMultiEnum(prop: { type?: unknown; items?: unknown }): boolean {
39
+ return prop.type === 'array'
40
+ && typeof prop.items === 'object'
41
+ && prop.items !== null
42
+ && isEnum(prop.items as { enum?: unknown });
43
+ }
44
+
45
+ /**
46
+ * The option values a Dropdown or Multiple Choice element allows - the array
47
+ * is the live one on the element so it can be edited in place
48
+ */
49
+ export function enumValues(prop: { type?: unknown; enum?: unknown; items?: unknown }): string[] | undefined {
50
+ if (isEnum(prop)) return prop.enum as string[];
51
+ if (isMultiEnum(prop)) return (prop.items as { enum: string[] }).enum;
52
+ return undefined;
53
+ }
54
+
55
+ /** The JSON Schema element a builder option creates */
56
+ export function schemaTemplate(option: SchemaTypeOption): Record<string, unknown> {
57
+ if (option.multiple) {
58
+ return {
59
+ type: 'array',
60
+ uniqueItems: true,
61
+ items: { type: 'string', enum: [] },
62
+ };
63
+ }
64
+
65
+ if (option.enum) return { type: option.type, enum: [] };
66
+
67
+ return { type: option.type };
68
+ }
69
+
70
+ /**
71
+ * Resolve the builder option a schema element was created from, falling back
72
+ * to a capitalized copy of the raw JSON Schema type for anything unrecognised
73
+ */
74
+ export function schemaType(prop: { type: string; enum?: unknown; items?: unknown }): SchemaTypeOption {
75
+ const multiple = isMultiEnum(prop);
76
+ const single = !multiple && isEnum(prop);
77
+
78
+ const match = SCHEMA_TYPES.find((option) => {
79
+ return option.type === prop.type
80
+ && Boolean(option.enum) === single
81
+ && Boolean(option.multiple) === multiple;
82
+ });
83
+
84
+ if (match) return match;
85
+
86
+ return {
87
+ label: prop.type.charAt(0).toUpperCase() + prop.type.slice(1),
88
+ icon: IconAlphabetLatin,
89
+ type: prop.type,
90
+ };
91
+ }
package/lib.ts CHANGED
@@ -7,6 +7,7 @@ export { default as TablerToggle } from './components/input/Toggle.vue'
7
7
  export { default as TablerInput } from './components/input/Input.vue'
8
8
  export { default as TablerFileInput } from './components/input/FileInput.vue'
9
9
  export { default as TablerEnum } from './components/input/Enum.vue';
10
+ export { default as TablerMultiEnum } from './components/input/MultiEnum.vue';
10
11
 
11
12
  export { default as TablerAlert } from './components/Alert.vue'
12
13
  export { default as TablerBorder } from './components/Border.vue'
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@tak-ps/vue-tabler",
3
3
  "type": "module",
4
- "version": "5.3.0",
4
+ "version": "5.5.0",
5
5
  "lib": "lib.ts",
6
6
  "main": "lib.ts",
7
7
  "module": "lib.ts",
@@ -42,7 +42,7 @@
42
42
  "eslint": "^10.0.0",
43
43
  "eslint-plugin-vue": "^10.0.0",
44
44
  "globals": "^17.0.0",
45
- "jsdom": "^29.0.0",
45
+ "jsdom": "^30.0.0",
46
46
  "typescript": "^6.0.0",
47
47
  "typescript-eslint": "^8.36.0",
48
48
  "vite": "^8.0.0",
@@ -0,0 +1,164 @@
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
+ })
@@ -0,0 +1,122 @@
1
+ import { describe, expect, it } from 'vitest'
2
+ import { mount } from '@vue/test-utils'
3
+ import SchemaBuilder from '../components/SchemaBuilder.vue'
4
+ import SchemaBuilderEdit from '../components/SchemaBuilderEdit.vue'
5
+ import MultiEnum from '../components/input/MultiEnum.vue'
6
+ import { schemaType, schemaTemplate, enumValues, SCHEMA_TYPES } from '../components/schema-types'
7
+
8
+ describe('schemaType', () => {
9
+ it('maps JSON Schema types to human readable labels', () => {
10
+ expect(schemaType({ type: 'string' }).label).toBe('Text')
11
+ expect(schemaType({ type: 'string', enum: ['a'] }).label).toBe('Dropdown')
12
+ expect(schemaType({ type: 'array', items: { type: 'string', enum: ['a'] } }).label).toBe('Multiple Choice')
13
+ expect(schemaType({ type: 'boolean' }).label).toBe('Toggle')
14
+ expect(schemaType({ type: 'number' }).label).toBe('Number')
15
+ expect(schemaType({ type: 'integer' }).label).toBe('Whole Number')
16
+ })
17
+
18
+ it('falls back to a capitalized raw type for anything unrecognised', () => {
19
+ expect(schemaType({ type: 'array' }).label).toBe('Array')
20
+ expect(schemaType({ type: 'array', items: { type: 'object' } }).label).toBe('Array')
21
+ })
22
+ })
23
+
24
+ describe('schemaTemplate', () => {
25
+ it('builds a Multiple Choice element as an array of enum strings', () => {
26
+ const option = SCHEMA_TYPES.find((o) => o.label === 'Multiple Choice')!
27
+
28
+ expect(schemaTemplate(option)).toEqual({
29
+ type: 'array',
30
+ uniqueItems: true,
31
+ items: { type: 'string', enum: [] },
32
+ })
33
+ })
34
+
35
+ it('edits the option list in place for both enum shapes', () => {
36
+ const single = { type: 'string', enum: ['a'] }
37
+ const multiple = { type: 'array', items: { type: 'string', enum: ['a'] } }
38
+
39
+ enumValues(single)!.push('b')
40
+ enumValues(multiple)!.push('b')
41
+
42
+ expect(single.enum).toEqual(['a', 'b'])
43
+ expect(multiple.items.enum).toEqual(['a', 'b'])
44
+ expect(enumValues({ type: 'string' })).toBeUndefined()
45
+ })
46
+ })
47
+
48
+ describe('TablerSchemaBuilder', () => {
49
+ it('offers every field type by its human readable name', () => {
50
+ const wrapper = mount(SchemaBuilder, {
51
+ props: {
52
+ modelValue: { type: 'object', properties: {} },
53
+ },
54
+ })
55
+
56
+ const labels = wrapper.findAll('.opt').map((opt) => opt.text().trim())
57
+
58
+ expect(labels).toEqual(SCHEMA_TYPES.map((option) => option.label))
59
+ expect(labels).not.toContain('string')
60
+ expect(labels).not.toContain('String')
61
+ })
62
+
63
+ it('offers the option list for a Multiple Choice element', () => {
64
+ const wrapper = mount(SchemaBuilderEdit, {
65
+ attachTo: document.body,
66
+ props: {
67
+ prop: {
68
+ name: 'resources',
69
+ type: 'array',
70
+ required: false,
71
+ items: { type: 'string', enum: ['fire', 'police'] },
72
+ },
73
+ },
74
+ })
75
+
76
+ const body = document.body.textContent || ''
77
+
78
+ expect(document.body.querySelector('.modal-header')?.textContent?.trim()).toBe('Multiple Choice')
79
+ expect(body).toContain('Options')
80
+ expect(body).toContain('fire')
81
+ expect(body).toContain('police')
82
+
83
+ wrapper.unmount()
84
+ })
85
+
86
+ it('titles the property editor with the human readable type', () => {
87
+ const wrapper = mount(SchemaBuilderEdit, {
88
+ attachTo: document.body,
89
+ props: {
90
+ prop: { name: 'priority', type: 'string', required: false, enum: ['low', 'high'] },
91
+ },
92
+ })
93
+
94
+ const header = document.body.querySelector('.modal-header')
95
+
96
+ expect(header?.textContent?.trim()).toBe('Dropdown')
97
+
98
+ wrapper.unmount()
99
+ })
100
+ })
101
+
102
+ describe('TablerMultiEnum', () => {
103
+ it('emits the checked options in option order', async () => {
104
+ const wrapper = mount(MultiEnum, {
105
+ props: {
106
+ modelValue: [],
107
+ options: ['fire', 'police', 'medical'],
108
+ },
109
+ })
110
+
111
+ const boxes = wrapper.findAll('input[type="checkbox"]')
112
+
113
+ await boxes[2].setValue(true)
114
+ await boxes[0].setValue(true)
115
+
116
+ const emitted = wrapper.emitted('update:modelValue')!
117
+ expect(emitted[emitted.length - 1][0]).toEqual(['fire', 'medical'])
118
+
119
+ await boxes[2].setValue(false)
120
+ expect(wrapper.emitted('update:modelValue')!.at(-1)![0]).toEqual(['fire'])
121
+ })
122
+ })