@tak-ps/vue-tabler 5.4.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,13 @@
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
+
15
22
  ### v5.4.0
16
23
 
17
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
@@ -7,7 +7,7 @@
7
7
  :key='key'
8
8
  class='py-2 floating-input'
9
9
  >
10
- <template v-if='s.properties[key].type === "array" && s.properties[key].items && s.properties[key].items.enum'>
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,86 @@
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>
60
141
  <template v-else-if='s.properties[key].type === "array"'>
61
142
  <div class='d-flex'>
62
143
  <label
@@ -113,21 +194,92 @@
113
194
  </template>
114
195
  </div>
115
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>
116
260
  </div>
117
261
  </template>
118
262
 
119
263
  <script setup lang="ts">
120
- import { ref, watch, onMounted } from 'vue'
264
+ import { ref, reactive, watch, onMounted } from 'vue'
121
265
  import TablerInput from './input/Input.vue';
122
266
  import TablerToggle from './input/Toggle.vue';
123
267
  import TablerEnum from './input/Enum.vue';
124
268
  import TablerMultiEnum from './input/MultiEnum.vue';
125
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';
126
275
  import {
127
276
  IconPlus,
128
277
  IconTrash,
278
+ IconDatabaseImport,
129
279
  } from '@tabler/icons-vue';
130
280
 
281
+ defineOptions({ name: 'TablerSchema' });
282
+
131
283
  export interface SchemaProps {
132
284
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
133
285
  modelValue: any;
@@ -151,19 +303,105 @@ const s = ref<any>({});
151
303
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
152
304
  const data = ref<any>({});
153
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
+
154
339
  const push = (key: string) => {
155
- if (!props.schema.properties[key].items) data.value[key].push('');
156
- 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') {
157
345
  data.value[key].push({});
158
- } else if (props.schema.properties[key].items.type === 'array') {
346
+ } else if (items.type === 'array') {
159
347
  data.value[key].push([]);
160
- } else if (props.schema.properties[key].items.type === 'boolean') {
348
+ } else if (items.type === 'boolean') {
161
349
  data.value[key].push(false);
162
350
  } else {
163
351
  data.value[key].push('');
164
352
  }
165
353
  }
166
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
+
167
405
  // Watch for data changes
168
406
  watch(data, () => {
169
407
  emit('update:modelValue', data.value);
@@ -173,24 +411,29 @@ const updateSchema = () => {
173
411
  s.value = JSON.parse(JSON.stringify(props.schema));
174
412
  if (s.value.type === 'object' && s.value.properties) {
175
413
  for (const req of (s.value.required || [])) {
176
- s.value.properties[req].required = true;
414
+ if (s.value.properties[req]) s.value.properties[req].required = true;
177
415
  }
178
416
  }
179
417
  }
180
418
 
419
+ /** Seed missing keys from the schema default, falling back to an empty value of the right shape */
181
420
  const updateDataDefaults = () => {
182
421
  if (s.value.type === 'object' && s.value.properties) {
183
422
  for (const key in s.value.properties) {
184
423
  if (data.value[key] !== undefined) continue;
185
424
 
186
- const type = s.value.properties[key].type;
425
+ const prop = s.value.properties[key];
187
426
 
188
- if (type === 'array') {
427
+ if (prop.default !== undefined) {
428
+ data.value[key] = JSON.parse(JSON.stringify(prop.default));
429
+ } else if (prop.type === 'array') {
189
430
  data.value[key] = [];
190
- } else if (type === 'boolean') {
431
+ } else if (prop.type === 'boolean') {
191
432
  data.value[key] = false;
192
- } else if (type === 'object') {
433
+ } else if (prop.type === 'object') {
193
434
  data.value[key] = {};
435
+ } else if (prop.type === 'number' || prop.type === 'integer') {
436
+ continue;
194
437
  } else {
195
438
  data.value[key] = '';
196
439
  }
@@ -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.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
+ })