mgv-backoffice 1.45.0 → 1.46.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mgv-backoffice",
3
- "version": "1.45.0",
3
+ "version": "1.46.0",
4
4
  "description": "Shared Vue 3 UI component library",
5
5
  "type": "module",
6
6
  "main": "./dist/ui-lib.umd.cjs",
@@ -0,0 +1,73 @@
1
+ import { describe, it, expect } from 'vitest'
2
+ import { mount } from '@vue/test-utils'
3
+ import BaseGroupedChecklist from './BaseGroupedChecklist.vue'
4
+
5
+ const GROUPS = [
6
+ { key: 'STOCK', label: 'Stocks', items: ['AMD', 'NVDA'] },
7
+ { key: 'ETF', label: 'ETFs', items: [] },
8
+ { key: 'CRYPTO', label: 'Crypto', items: ['BTC/USD'] },
9
+ ]
10
+
11
+ function mountList(modelValue: string[] = []) {
12
+ return mount(BaseGroupedChecklist, {
13
+ props: {
14
+ groups: GROUPS,
15
+ modelValue,
16
+ allAriaLabel: 'Select all symbols',
17
+ itemAriaLabel: (s: string) => `Pick ${s}`,
18
+ 'onUpdate:modelValue': () => {},
19
+ },
20
+ })
21
+ }
22
+
23
+ const box = (w: ReturnType<typeof mountList>, label: string) =>
24
+ w.find<HTMLInputElement>(`input[aria-label="${label}"]`)
25
+
26
+ const lastEmit = (w: ReturnType<typeof mountList>) =>
27
+ (w.emitted('update:modelValue') as string[][][]).at(-1)![0]
28
+
29
+ describe('BaseGroupedChecklist', () => {
30
+ it('renders non-empty groups with counts and reflects the model', () => {
31
+ const w = mountList(['AMD'])
32
+ expect(w.text()).toContain('Select all (3)')
33
+ expect(w.text()).toContain('Stocks (2)')
34
+ expect(w.text()).not.toContain('ETFs')
35
+ expect(box(w, 'Pick AMD').element.checked).toBe(true)
36
+ expect(box(w, 'Pick NVDA').element.checked).toBe(false)
37
+ expect(box(w, 'Select all Stocks').element.checked).toBe(false)
38
+ expect(box(w, 'Select all symbols').element.checked).toBe(false)
39
+ })
40
+
41
+ it('toggles one item', async () => {
42
+ const w = mountList(['AMD'])
43
+ await box(w, 'Pick BTC/USD').trigger('change')
44
+ expect(lastEmit(w)).toEqual(['AMD', 'BTC/USD'])
45
+ await box(w, 'Pick AMD').trigger('change')
46
+ expect(lastEmit(w)).toEqual([])
47
+ })
48
+
49
+ it('selects and clears a whole group, leaving others alone', async () => {
50
+ const w = mountList(['BTC/USD'])
51
+ await box(w, 'Select all Stocks').trigger('change')
52
+ expect(lastEmit(w)).toEqual(['AMD', 'NVDA', 'BTC/USD'])
53
+ await w.setProps({ modelValue: ['AMD', 'NVDA', 'BTC/USD'] })
54
+ await box(w, 'Select all Stocks').trigger('change')
55
+ expect(lastEmit(w)).toEqual(['BTC/USD'])
56
+ })
57
+
58
+ it('master checkbox selects everything, then clears everything', async () => {
59
+ const w = mountList([])
60
+ await box(w, 'Select all symbols').trigger('change')
61
+ expect(lastEmit(w)).toEqual(['AMD', 'NVDA', 'BTC/USD'])
62
+ await w.setProps({ modelValue: ['AMD', 'NVDA', 'BTC/USD'] })
63
+ expect(box(w, 'Select all symbols').element.checked).toBe(true)
64
+ await box(w, 'Select all symbols').trigger('change')
65
+ expect(lastEmit(w)).toEqual([])
66
+ })
67
+
68
+ it('emits in group order regardless of click order', async () => {
69
+ const w = mountList(['BTC/USD'])
70
+ await box(w, 'Pick AMD').trigger('change')
71
+ expect(lastEmit(w)).toEqual(['AMD', 'BTC/USD'])
72
+ })
73
+ })
@@ -0,0 +1,122 @@
1
+ <template>
2
+ <div>
3
+ <label class="flex items-center gap-2 border-b border-slate-200 dark:border-slate-700 pb-2">
4
+ <input
5
+ type="checkbox"
6
+ class="h-4 w-4"
7
+ :checked="allSelected"
8
+ :aria-label="allAriaLabel"
9
+ @change="toggleAll"
10
+ />
11
+ <span class="text-sm font-medium text-slate-700 dark:text-slate-200">
12
+ {{ allLabel }} ({{ allItems.length }})
13
+ </span>
14
+ </label>
15
+ <div class="overflow-y-auto space-y-3 mt-3" :class="maxHeightClass">
16
+ <div v-for="g in visibleGroups" :key="g.key">
17
+ <label class="flex items-center gap-2 pb-1.5">
18
+ <input
19
+ type="checkbox"
20
+ class="h-4 w-4"
21
+ :checked="groupSelected(g)"
22
+ :aria-label="`${allLabel} ${g.label}`"
23
+ @change="toggleGroup(g)"
24
+ />
25
+ <span class="text-xs font-semibold uppercase tracking-wide text-slate-500 dark:text-slate-400">
26
+ {{ g.label }} ({{ g.items.length }})
27
+ </span>
28
+ </label>
29
+ <div class="grid grid-cols-[repeat(2,minmax(0,1fr))] sm:grid-cols-[repeat(3,minmax(0,1fr))] gap-x-3 gap-y-1.5">
30
+ <label v-for="item in g.items" :key="item" class="flex items-center gap-2">
31
+ <input
32
+ type="checkbox"
33
+ class="h-4 w-4"
34
+ :checked="selected.has(item)"
35
+ :aria-label="itemAriaLabel ? itemAriaLabel(item) : item"
36
+ @change="toggleItem(item)"
37
+ />
38
+ <span
39
+ class="text-sm text-slate-700 dark:text-slate-200"
40
+ :class="{ 'font-mono': mono }"
41
+ >{{ item }}</span>
42
+ </label>
43
+ </div>
44
+ </div>
45
+ </div>
46
+ </div>
47
+ </template>
48
+
49
+ <script setup lang="ts">
50
+ /**
51
+ * Multi-select checklist in groups: a master "Select all (N)" checkbox, a
52
+ * per-group select-all, and one checkbox per item. `v-model` is the array of
53
+ * selected item ids (order follows the groups). Items are plain strings and
54
+ * must be unique across groups; empty groups are not rendered.
55
+ *
56
+ * Typical use: "pick which symbols / users / records an action applies to"
57
+ * inside a confirmation modal.
58
+ */
59
+ import { computed } from 'vue'
60
+ import type { ChecklistGroup } from '../types/checklist'
61
+
62
+ interface Props {
63
+ groups: ChecklistGroup[]
64
+ modelValue: string[]
65
+ /** Text of the master checkbox; group checkboxes read "<allLabel> <group>". */
66
+ allLabel?: string
67
+ allAriaLabel?: string
68
+ /** Accessible name per item checkbox (defaults to the item itself). */
69
+ itemAriaLabel?: (item: string) => string
70
+ /** Render item labels in a monospace font (tickers, ids). */
71
+ mono?: boolean
72
+ maxHeightClass?: string
73
+ }
74
+
75
+ const props = withDefaults(defineProps<Props>(), {
76
+ allLabel: 'Select all',
77
+ allAriaLabel: 'Select all items',
78
+ itemAriaLabel: undefined,
79
+ mono: false,
80
+ maxHeightClass: 'max-h-72',
81
+ })
82
+
83
+ const emit = defineEmits<{ (e: 'update:modelValue', value: string[]): void }>()
84
+
85
+ const visibleGroups = computed(() => props.groups.filter((g) => g.items.length > 0))
86
+ const allItems = computed(() => visibleGroups.value.flatMap((g) => g.items))
87
+ const selected = computed(() => new Set(props.modelValue))
88
+
89
+ const allSelected = computed(
90
+ () => allItems.value.length > 0 && allItems.value.every((i) => selected.value.has(i)),
91
+ )
92
+
93
+ function groupSelected(g: ChecklistGroup): boolean {
94
+ return g.items.every((i) => selected.value.has(i))
95
+ }
96
+
97
+ // Emit in group order so the model is stable regardless of click order.
98
+ function commit(next: Set<string>) {
99
+ emit('update:modelValue', allItems.value.filter((i) => next.has(i)))
100
+ }
101
+
102
+ function toggleAll() {
103
+ commit(allSelected.value ? new Set() : new Set(allItems.value))
104
+ }
105
+
106
+ function toggleGroup(g: ChecklistGroup) {
107
+ const next = new Set(selected.value)
108
+ const on = !groupSelected(g)
109
+ for (const i of g.items) {
110
+ if (on) next.add(i)
111
+ else next.delete(i)
112
+ }
113
+ commit(next)
114
+ }
115
+
116
+ function toggleItem(item: string) {
117
+ const next = new Set(selected.value)
118
+ if (next.has(item)) next.delete(item)
119
+ else next.add(item)
120
+ commit(next)
121
+ }
122
+ </script>
@@ -0,0 +1,38 @@
1
+ import { describe, it, expect } from 'vitest'
2
+ import { reactive } from 'vue'
3
+ import { stableStringify, useDirtyState } from './useDirtyState'
4
+
5
+ describe('stableStringify', () => {
6
+ it('ignores key order at every depth but keeps array order', () => {
7
+ expect(stableStringify({ b: 1, a: { d: 2, c: 3 } })).toBe(stableStringify({ a: { c: 3, d: 2 }, b: 1 }))
8
+ expect(stableStringify([1, 2])).not.toBe(stableStringify([2, 1]))
9
+ })
10
+ })
11
+
12
+ describe('useDirtyState', () => {
13
+ it('starts clean, turns dirty on an edit and clean again when reverted', () => {
14
+ const form = reactive({ name: 'a', params: { x: 1 } as Record<string, number> })
15
+ const { dirty } = useDirtyState(() => form)
16
+ expect(dirty.value).toBe(false)
17
+ form.params.x = 2
18
+ expect(dirty.value).toBe(true)
19
+ form.params.x = 1
20
+ expect(dirty.value).toBe(false)
21
+ })
22
+
23
+ it('markClean() adopts the current state as the baseline', () => {
24
+ const form = reactive({ name: 'a' })
25
+ const { dirty, markClean } = useDirtyState(() => form)
26
+ form.name = 'b'
27
+ expect(dirty.value).toBe(true)
28
+ markClean()
29
+ expect(dirty.value).toBe(false)
30
+ })
31
+
32
+ it('a reordered but equal object is not an edit', () => {
33
+ const form = reactive<{ params: Record<string, number> }>({ params: { a: 1, b: 2 } })
34
+ const { dirty } = useDirtyState(() => form)
35
+ form.params = { b: 2, a: 1 }
36
+ expect(dirty.value).toBe(false)
37
+ })
38
+ })
@@ -0,0 +1,44 @@
1
+ import { computed, ref, type ComputedRef } from 'vue'
2
+
3
+ /**
4
+ * JSON-serialise a value with object keys sorted at every depth, so two
5
+ * structurally equal values always produce the same string regardless of
6
+ * key insertion order (arrays keep their order).
7
+ */
8
+ export function stableStringify(value: unknown): string {
9
+ return JSON.stringify(value, (_key, v: unknown) => {
10
+ if (v !== null && typeof v === 'object' && !Array.isArray(v)) {
11
+ const sorted: Record<string, unknown> = {}
12
+ for (const k of Object.keys(v as Record<string, unknown>).sort()) {
13
+ sorted[k] = (v as Record<string, unknown>)[k]
14
+ }
15
+ return sorted
16
+ }
17
+ return v
18
+ })
19
+ }
20
+
21
+ /**
22
+ * Unsaved-changes tracking for an edit form. `snapshot` returns the form's
23
+ * current state (read reactively); `dirty` is true whenever it differs from
24
+ * the last clean baseline. Call `markClean()` after loading or saving to make
25
+ * the current state the new baseline. Comparison is structural (key order is
26
+ * ignored), so a server echo that reorders keys never reads as an edit.
27
+ *
28
+ * ```ts
29
+ * const form = reactive({ name: '', params: {} })
30
+ * const { dirty, markClean } = useDirtyState(() => form)
31
+ * // …load → assign form → markClean(); Save enabled while dirty.value
32
+ * ```
33
+ */
34
+ export function useDirtyState(snapshot: () => unknown): {
35
+ dirty: ComputedRef<boolean>
36
+ markClean: () => void
37
+ } {
38
+ const baseline = ref(stableStringify(snapshot()))
39
+ const dirty = computed(() => stableStringify(snapshot()) !== baseline.value)
40
+ function markClean() {
41
+ baseline.value = stableStringify(snapshot())
42
+ }
43
+ return { dirty, markClean }
44
+ }
package/src/index.ts CHANGED
@@ -51,6 +51,7 @@ export { default as BaseFilterChip } from './components/BaseFilterChip.vue'
51
51
  export { default as BaseCredentialsForm } from './components/BaseCredentialsForm.vue'
52
52
  export { default as BasePillPickerModal } from './components/BasePillPickerModal.vue'
53
53
  export { default as BaseBarDistribution } from './components/BaseBarDistribution.vue'
54
+ export { default as BaseGroupedChecklist } from './components/BaseGroupedChecklist.vue'
54
55
  export type { CredentialsView, CredentialsUpdate } from './components/BaseCredentialsForm.vue'
55
56
 
56
57
  // Components — third-party wrappers
@@ -86,6 +87,7 @@ export type { FieldClasses } from './composables/useFieldClasses'
86
87
  export { usePolling } from './composables/usePolling'
87
88
  export type { UsePollingOptions } from './composables/usePolling'
88
89
  export { useLatestRequest, isAbortError } from './composables/useLatestRequest'
90
+ export { useDirtyState, stableStringify } from './composables/useDirtyState'
89
91
  export type { LatestRequestToken } from './composables/useLatestRequest'
90
92
 
91
93
  // Enums
@@ -113,6 +115,7 @@ export type { SpecField, SpecFieldType, SpecFieldValue } from './types/specField
113
115
  export type { StatBreakdownItem } from './types/statBreakdown'
114
116
  export type { PillPickerItem } from './types/pillPicker'
115
117
  export type { DistributionBar } from './types/distribution'
118
+ export type { ChecklistGroup } from './types/checklist'
116
119
 
117
120
  // Utils
118
121
  export { getBaseColor, getBaseColorOf } from './utils/util'
@@ -0,0 +1,6 @@
1
+ /** One group of a BaseGroupedChecklist: a heading plus its selectable item ids. */
2
+ export interface ChecklistGroup {
3
+ key: string
4
+ label: string
5
+ items: string[]
6
+ }