@svgrid/grid 1.0.2 → 1.1.1

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.
Files changed (216) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +137 -39
  3. package/dist/GridFooter.svelte +164 -0
  4. package/dist/GridFooter.svelte.d.ts +29 -0
  5. package/dist/GridMenus.svelte +648 -0
  6. package/dist/GridMenus.svelte.d.ts +31 -0
  7. package/dist/SvGrid.controller.svelte.d.ts +472 -0
  8. package/dist/SvGrid.controller.svelte.js +1917 -0
  9. package/dist/SvGrid.css +2012 -0
  10. package/dist/SvGrid.helpers.d.ts +35 -0
  11. package/dist/SvGrid.helpers.js +160 -0
  12. package/dist/SvGrid.helpers.test.d.ts +1 -0
  13. package/dist/SvGrid.helpers.test.js +298 -0
  14. package/dist/SvGrid.svelte +2346 -8742
  15. package/dist/SvGrid.svelte.d.ts +4 -357
  16. package/dist/SvGrid.types.d.ts +523 -0
  17. package/dist/SvGrid.types.js +1 -0
  18. package/dist/SvGridChart.svelte +1060 -23
  19. package/dist/SvGridChart.svelte.d.ts +17 -0
  20. package/dist/aligned-grids.d.ts +6 -0
  21. package/dist/aligned-grids.js +84 -0
  22. package/dist/aligned-grids.test.d.ts +1 -0
  23. package/dist/aligned-grids.test.js +75 -0
  24. package/dist/build-api.coverage.test.d.ts +20 -0
  25. package/dist/build-api.coverage.test.js +505 -0
  26. package/dist/build-api.d.ts +5 -0
  27. package/dist/build-api.js +557 -0
  28. package/dist/cell-render.d.ts +15 -0
  29. package/dist/cell-render.js +246 -0
  30. package/dist/cell-render.test.d.ts +1 -0
  31. package/dist/cell-render.test.js +338 -0
  32. package/dist/cell-values.d.ts +28 -0
  33. package/dist/cell-values.js +89 -0
  34. package/dist/chart-export.test.d.ts +1 -0
  35. package/dist/chart-export.test.js +302 -0
  36. package/dist/chart.coverage.test.d.ts +1 -0
  37. package/dist/chart.coverage.test.js +748 -0
  38. package/dist/chart.d.ts +370 -3
  39. package/dist/chart.js +1135 -42
  40. package/dist/clipboard.d.ts +15 -0
  41. package/dist/clipboard.js +420 -0
  42. package/dist/clipboard.test.d.ts +1 -0
  43. package/dist/clipboard.test.js +700 -0
  44. package/dist/collaboration.coverage.test.d.ts +1 -0
  45. package/dist/collaboration.coverage.test.js +200 -0
  46. package/dist/column-groups.d.ts +19 -0
  47. package/dist/column-groups.js +62 -0
  48. package/dist/column-groups.test.d.ts +1 -0
  49. package/dist/column-groups.test.js +56 -0
  50. package/dist/column-types.d.ts +10 -0
  51. package/dist/column-types.js +63 -0
  52. package/dist/column-types.test.d.ts +1 -0
  53. package/dist/column-types.test.js +62 -0
  54. package/dist/columns.d.ts +30 -0
  55. package/dist/columns.js +277 -0
  56. package/dist/columns.test.d.ts +1 -0
  57. package/dist/columns.test.js +625 -0
  58. package/dist/core.d.ts +86 -1
  59. package/dist/css.d.ts +3 -0
  60. package/dist/editing.d.ts +31 -0
  61. package/dist/editing.js +529 -0
  62. package/dist/editing.test.d.ts +1 -0
  63. package/dist/editing.test.js +732 -0
  64. package/dist/editors/cell-editors.coverage.test.d.ts +1 -0
  65. package/dist/editors/cell-editors.coverage.test.js +139 -0
  66. package/dist/editors/cell-editors.d.ts +1 -1
  67. package/dist/facet-buckets.d.ts +13 -0
  68. package/dist/facet-buckets.js +54 -0
  69. package/dist/facet-buckets.test.d.ts +1 -0
  70. package/dist/facet-buckets.test.js +296 -0
  71. package/dist/features.d.ts +5 -0
  72. package/dist/features.js +30 -0
  73. package/dist/filter-operators.d.ts +16 -0
  74. package/dist/filter-operators.js +69 -0
  75. package/dist/filter-operators.test.d.ts +1 -0
  76. package/dist/filter-operators.test.js +135 -0
  77. package/dist/hyperformula-adapter.d.ts +82 -0
  78. package/dist/hyperformula-adapter.js +73 -0
  79. package/dist/hyperformula-adapter.test.d.ts +1 -0
  80. package/dist/hyperformula-adapter.test.js +205 -0
  81. package/dist/index.d.ts +9 -3
  82. package/dist/index.js +8 -2
  83. package/dist/keyboard-handlers.coverage.test.d.ts +1 -0
  84. package/dist/keyboard-handlers.coverage.test.js +495 -0
  85. package/dist/keyboard-handlers.d.ts +7 -0
  86. package/dist/keyboard-handlers.js +202 -0
  87. package/dist/menus.d.ts +40 -0
  88. package/dist/menus.js +390 -0
  89. package/dist/menus.test.d.ts +1 -0
  90. package/dist/menus.test.js +560 -0
  91. package/dist/named-views.coverage.test.d.ts +1 -0
  92. package/dist/named-views.coverage.test.js +180 -0
  93. package/dist/named-views.d.ts +27 -0
  94. package/dist/named-views.js +39 -0
  95. package/dist/row-drag.d.ts +49 -0
  96. package/dist/row-drag.js +221 -0
  97. package/dist/row-drag.test.d.ts +1 -0
  98. package/dist/row-drag.test.js +142 -0
  99. package/dist/row-resize.d.ts +43 -0
  100. package/dist/row-resize.js +158 -0
  101. package/dist/row-resize.test.d.ts +1 -0
  102. package/dist/row-resize.test.js +329 -0
  103. package/dist/scroll-sync.d.ts +9 -0
  104. package/dist/scroll-sync.js +89 -0
  105. package/dist/scroll-sync.test.d.ts +1 -0
  106. package/dist/scroll-sync.test.js +290 -0
  107. package/dist/selection.d.ts +32 -0
  108. package/dist/selection.js +427 -0
  109. package/dist/selection.multi-range.test.d.ts +1 -0
  110. package/dist/selection.multi-range.test.js +55 -0
  111. package/dist/selection.test.d.ts +1 -0
  112. package/dist/selection.test.js +647 -0
  113. package/dist/server-data-source.coverage.test.d.ts +1 -0
  114. package/dist/server-data-source.coverage.test.js +154 -0
  115. package/dist/spreadsheet.d.ts +110 -0
  116. package/dist/spreadsheet.js +242 -0
  117. package/dist/spreadsheet.test.d.ts +1 -0
  118. package/dist/spreadsheet.test.js +446 -0
  119. package/dist/summaries.d.ts +12 -0
  120. package/dist/summaries.js +65 -0
  121. package/dist/sv-grid-scrollbar.js +13 -1
  122. package/dist/svgrid-wrapper.types.d.ts +31 -1
  123. package/dist/svgrid.behavior.test.js +20 -0
  124. package/dist/svgrid.comments-autocomplete.test.d.ts +1 -0
  125. package/dist/svgrid.comments-autocomplete.test.js +96 -0
  126. package/dist/svgrid.context-menu.test.d.ts +1 -0
  127. package/dist/svgrid.context-menu.test.js +102 -0
  128. package/dist/svgrid.interaction.test.js +31 -0
  129. package/dist/svgrid.new-features.wrapper.test.js +64 -6
  130. package/dist/svgrid.wrapper.test.js +27 -1
  131. package/dist/test-setup.js +9 -3
  132. package/dist/virtualization/column-virtualizer.d.ts +2 -0
  133. package/dist/virtualization/scroll-scaling.d.ts +45 -0
  134. package/dist/virtualization/scroll-scaling.js +99 -0
  135. package/dist/virtualization/scroll-scaling.test.d.ts +1 -0
  136. package/dist/virtualization/scroll-scaling.test.js +127 -0
  137. package/dist/virtualization/svelte-virtualizer.svelte.d.ts +2 -0
  138. package/dist/virtualization/svelte-virtualizer.svelte.js +2 -0
  139. package/dist/virtualization/virtualizer.d.ts +7 -0
  140. package/dist/virtualization/virtualizer.js +30 -0
  141. package/package.json +2 -1
  142. package/src/GridFooter.svelte +164 -0
  143. package/src/GridMenus.svelte +648 -0
  144. package/src/SvGrid.controller.svelte.ts +2352 -0
  145. package/src/SvGrid.css +2012 -0
  146. package/src/SvGrid.helpers.test.ts +415 -0
  147. package/src/SvGrid.helpers.ts +185 -0
  148. package/src/SvGrid.svelte +2346 -8742
  149. package/src/SvGrid.types.ts +537 -0
  150. package/src/SvGridChart.svelte +1060 -23
  151. package/src/aligned-grids.test.ts +80 -0
  152. package/src/aligned-grids.ts +87 -0
  153. package/src/build-api.coverage.test.ts +532 -0
  154. package/src/build-api.ts +683 -0
  155. package/src/cell-render.test.ts +451 -0
  156. package/src/cell-render.ts +426 -0
  157. package/src/cell-values.ts +114 -0
  158. package/src/chart-export.test.ts +370 -0
  159. package/src/chart.coverage.test.ts +814 -0
  160. package/src/chart.ts +1352 -47
  161. package/src/clipboard.test.ts +780 -0
  162. package/src/clipboard.ts +552 -0
  163. package/src/collaboration.coverage.test.ts +220 -0
  164. package/src/column-groups.test.ts +59 -0
  165. package/src/column-groups.ts +80 -0
  166. package/src/column-types.test.ts +68 -0
  167. package/src/column-types.ts +82 -0
  168. package/src/columns.test.ts +702 -0
  169. package/src/columns.ts +419 -0
  170. package/src/core.ts +86 -0
  171. package/src/css.d.ts +3 -0
  172. package/src/editing.test.ts +837 -0
  173. package/src/editing.ts +669 -0
  174. package/src/editors/cell-editors.coverage.test.ts +156 -0
  175. package/src/editors/cell-editors.ts +1 -0
  176. package/src/facet-buckets.test.ts +353 -0
  177. package/src/facet-buckets.ts +67 -0
  178. package/src/features.ts +128 -0
  179. package/src/filter-operators.test.ts +174 -0
  180. package/src/filter-operators.ts +87 -0
  181. package/src/hyperformula-adapter.test.ts +256 -0
  182. package/src/hyperformula-adapter.ts +124 -0
  183. package/src/index.ts +49 -0
  184. package/src/keyboard-handlers.coverage.test.ts +560 -0
  185. package/src/keyboard-handlers.ts +353 -0
  186. package/src/keyboard.ts +97 -97
  187. package/src/menus.test.ts +620 -0
  188. package/src/menus.ts +555 -0
  189. package/src/named-views.coverage.test.ts +210 -0
  190. package/src/named-views.ts +48 -0
  191. package/src/row-drag.test.ts +168 -0
  192. package/src/row-drag.ts +255 -0
  193. package/src/row-resize.test.ts +369 -0
  194. package/src/row-resize.ts +171 -0
  195. package/src/scroll-sync.test.ts +330 -0
  196. package/src/scroll-sync.ts +218 -0
  197. package/src/selection.multi-range.test.ts +61 -0
  198. package/src/selection.test.ts +722 -0
  199. package/src/selection.ts +579 -0
  200. package/src/server-data-source.coverage.test.ts +180 -0
  201. package/src/spreadsheet.test.ts +489 -0
  202. package/src/spreadsheet.ts +304 -0
  203. package/src/summaries.ts +204 -0
  204. package/src/sv-grid-scrollbar.ts +13 -1
  205. package/src/svgrid-wrapper.types.ts +31 -1
  206. package/src/svgrid.behavior.test.ts +22 -0
  207. package/src/svgrid.comments-autocomplete.test.ts +112 -0
  208. package/src/svgrid.context-menu.test.ts +126 -0
  209. package/src/svgrid.interaction.test.ts +30 -0
  210. package/src/svgrid.new-features.wrapper.test.ts +67 -6
  211. package/src/svgrid.wrapper.test.ts +27 -1
  212. package/src/test-setup.ts +9 -6
  213. package/src/virtualization/scroll-scaling.test.ts +148 -0
  214. package/src/virtualization/scroll-scaling.ts +121 -0
  215. package/src/virtualization/svelte-virtualizer.svelte.ts +2 -0
  216. package/src/virtualization/virtualizer.ts +26 -0
@@ -0,0 +1,220 @@
1
+ import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
2
+ import {
3
+ broadcastChannelTransport,
4
+ createCollaboration,
5
+ type CollabMessage,
6
+ type CollabTransport,
7
+ } from './collaboration'
8
+
9
+ const userA = { id: 'a', name: 'Ada', color: '#f00' }
10
+ const userB = { id: 'b', name: 'Bob', color: '#00f' }
11
+
12
+ /** A controllable transport whose subscriber we can drive by hand. */
13
+ function manualTransport() {
14
+ const handlers = new Set<(m: CollabMessage) => void>()
15
+ const posted: CollabMessage[] = []
16
+ const transport: CollabTransport = {
17
+ post: (msg) => {
18
+ posted.push(msg)
19
+ },
20
+ subscribe: (h) => {
21
+ handlers.add(h)
22
+ return () => handlers.delete(h)
23
+ },
24
+ }
25
+ const deliver = (msg: CollabMessage) => {
26
+ for (const h of [...handlers]) h(msg)
27
+ }
28
+ return { transport, posted, deliver, handlers }
29
+ }
30
+
31
+ describe('createCollaboration - presence / edit upserts', () => {
32
+ it('hello reply carries the responder own cell', () => {
33
+ const t = manualTransport()
34
+ const a = createCollaboration({ user: userA, transport: t.transport })
35
+ a.setCell({ rowId: 'r0', columnId: 'c0' })
36
+ t.posted.length = 0
37
+ // a newcomer says hello -> A replies presence with its own cell.
38
+ t.deliver({ kind: 'hello', user: userB })
39
+ const reply = t.posted.find((m) => m.kind === 'presence')
40
+ expect(reply).toEqual({ kind: 'presence', user: userA, cell: { rowId: 'r0', columnId: 'c0' } })
41
+ expect(a.peers().map((p) => p.id)).toEqual(['b'])
42
+ a.dispose()
43
+ })
44
+
45
+ it('presence message records the peer cursor', () => {
46
+ const t = manualTransport()
47
+ const onPeersChange = vi.fn()
48
+ const a = createCollaboration({ user: userA, transport: t.transport, onPeersChange })
49
+ t.deliver({ kind: 'presence', user: userB, cell: { rowId: 'r9', columnId: 'name' } })
50
+ expect(a.peers()[0]?.cell).toEqual({ rowId: 'r9', columnId: 'name' })
51
+ expect(onPeersChange).toHaveBeenCalled()
52
+ a.dispose()
53
+ })
54
+
55
+ it('edit from another peer sets a cursor AND fires onRemoteEdit', () => {
56
+ const t = manualTransport()
57
+ const onRemoteEdit = vi.fn()
58
+ const a = createCollaboration({ user: userA, transport: t.transport, onRemoteEdit })
59
+ t.deliver({ kind: 'edit', user: userB, rowId: 'r1', columnId: 'age', value: 30 })
60
+ expect(a.peers()[0]?.cell).toEqual({ rowId: 'r1', columnId: 'age' })
61
+ expect(onRemoteEdit).toHaveBeenCalledWith({
62
+ rowId: 'r1',
63
+ columnId: 'age',
64
+ value: 30,
65
+ user: userB,
66
+ })
67
+ a.dispose()
68
+ })
69
+
70
+ it('ignores own id on upsert (presence/edit echoed back)', () => {
71
+ const t = manualTransport()
72
+ const onRemoteEdit = vi.fn()
73
+ const a = createCollaboration({ user: userA, transport: t.transport, onRemoteEdit })
74
+ // self-echo: should not appear as a peer and should not call onRemoteEdit
75
+ t.deliver({ kind: 'presence', user: userA, cell: { rowId: 'x', columnId: 'y' } })
76
+ t.deliver({ kind: 'edit', user: userA, rowId: 'x', columnId: 'y', value: 1 })
77
+ expect(a.peers()).toHaveLength(0)
78
+ expect(onRemoteEdit).not.toHaveBeenCalled()
79
+ a.dispose()
80
+ })
81
+
82
+ it('bye for an unknown peer does not notify', () => {
83
+ const t = manualTransport()
84
+ const onPeersChange = vi.fn()
85
+ const a = createCollaboration({ user: userA, transport: t.transport, onPeersChange })
86
+ onPeersChange.mockClear()
87
+ t.deliver({ kind: 'bye', userId: 'ghost' })
88
+ expect(onPeersChange).not.toHaveBeenCalled()
89
+ a.dispose()
90
+ })
91
+
92
+ it('setCell broadcasts a presence with the new cell', () => {
93
+ const t = manualTransport()
94
+ const a = createCollaboration({ user: userA, transport: t.transport })
95
+ t.posted.length = 0
96
+ a.setCell(null)
97
+ expect(t.posted).toContainEqual({ kind: 'presence', user: userA, cell: null })
98
+ a.dispose()
99
+ })
100
+
101
+ it('sendEdit broadcasts an edit message', () => {
102
+ const t = manualTransport()
103
+ const a = createCollaboration({ user: userA, transport: t.transport })
104
+ t.posted.length = 0
105
+ a.sendEdit('r5', 'col', 'v')
106
+ expect(t.posted).toContainEqual({
107
+ kind: 'edit',
108
+ user: userA,
109
+ rowId: 'r5',
110
+ columnId: 'col',
111
+ value: 'v',
112
+ })
113
+ a.dispose()
114
+ })
115
+
116
+ it('drops messages after dispose and unsubscribes', () => {
117
+ const t = manualTransport()
118
+ const onPeersChange = vi.fn()
119
+ const a = createCollaboration({ user: userA, transport: t.transport, onPeersChange })
120
+ a.dispose()
121
+ onPeersChange.mockClear()
122
+ // After dispose the handler is unsubscribed; even if delivered it no-ops.
123
+ t.deliver({ kind: 'hello', user: userB })
124
+ expect(onPeersChange).not.toHaveBeenCalled()
125
+ expect(a.peers()).toHaveLength(0)
126
+ // second dispose is a no-op
127
+ a.dispose()
128
+ })
129
+ })
130
+
131
+ describe('createCollaboration - prune timer', () => {
132
+ beforeEach(() => vi.useFakeTimers())
133
+ afterEach(() => vi.useRealTimers())
134
+
135
+ it('prunes peers gone silent past the timeout', () => {
136
+ const t = manualTransport()
137
+ const onPeersChange = vi.fn()
138
+ const a = createCollaboration({
139
+ user: userA,
140
+ transport: t.transport,
141
+ onPeersChange,
142
+ peerTimeoutMs: 3000,
143
+ })
144
+ t.deliver({ kind: 'presence', user: userB, cell: null })
145
+ expect(a.peers()).toHaveLength(1)
146
+ onPeersChange.mockClear()
147
+ // advance past the timeout; prune interval is max(1000, timeout/3) = 1000
148
+ vi.advanceTimersByTime(4000)
149
+ expect(a.peers()).toHaveLength(0)
150
+ expect(onPeersChange).toHaveBeenCalled()
151
+ a.dispose()
152
+ })
153
+
154
+ it('prune tick with no expired peers does not notify', () => {
155
+ const t = manualTransport()
156
+ const onPeersChange = vi.fn()
157
+ const a = createCollaboration({
158
+ user: userA,
159
+ transport: t.transport,
160
+ onPeersChange,
161
+ peerTimeoutMs: 9000,
162
+ })
163
+ t.deliver({ kind: 'presence', user: userB, cell: null })
164
+ onPeersChange.mockClear()
165
+ vi.advanceTimersByTime(3000) // one tick, peer still fresh
166
+ expect(onPeersChange).not.toHaveBeenCalled()
167
+ expect(a.peers()).toHaveLength(1)
168
+ a.dispose()
169
+ })
170
+
171
+ it('dispose clears the prune timer', () => {
172
+ const clearSpy = vi.spyOn(globalThis, 'clearInterval')
173
+ const t = manualTransport()
174
+ const a = createCollaboration({ user: userA, transport: t.transport })
175
+ a.dispose()
176
+ expect(clearSpy).toHaveBeenCalled()
177
+ clearSpy.mockRestore()
178
+ })
179
+ })
180
+
181
+ describe('broadcastChannelTransport', () => {
182
+ it('round-trips a message across two channels of the same name', async () => {
183
+ const tx = broadcastChannelTransport('svgrid-test-chan')
184
+ const rx = broadcastChannelTransport('svgrid-test-chan')
185
+ const received: CollabMessage[] = []
186
+ const off = rx.subscribe((m) => received.push(m))
187
+ tx.post({ kind: 'hello', user: userA })
188
+ // BroadcastChannel delivery is async (microtask/macrotask).
189
+ await new Promise((r) => setTimeout(r, 10))
190
+ expect(received).toContainEqual({ kind: 'hello', user: userA })
191
+ off()
192
+ })
193
+
194
+ it('subscribe returns a working unsubscribe', async () => {
195
+ const tx = broadcastChannelTransport('svgrid-unsub-chan')
196
+ const rx = broadcastChannelTransport('svgrid-unsub-chan')
197
+ const received: CollabMessage[] = []
198
+ const off = rx.subscribe((m) => received.push(m))
199
+ off()
200
+ tx.post({ kind: 'bye', userId: 'a' })
201
+ await new Promise((r) => setTimeout(r, 10))
202
+ expect(received).toHaveLength(0)
203
+ })
204
+
205
+ it('no-ops gracefully when BroadcastChannel is unavailable', () => {
206
+ const original = globalThis.BroadcastChannel
207
+ // @ts-expect-error - simulate SSR / old environment
208
+ delete globalThis.BroadcastChannel
209
+ try {
210
+ const tx = broadcastChannelTransport('nope')
211
+ const handler = vi.fn()
212
+ const off = tx.subscribe(handler)
213
+ expect(() => tx.post({ kind: 'bye', userId: 'x' })).not.toThrow()
214
+ expect(() => off()).not.toThrow()
215
+ expect(handler).not.toHaveBeenCalled()
216
+ } finally {
217
+ globalThis.BroadcastChannel = original
218
+ }
219
+ })
220
+ })
@@ -0,0 +1,59 @@
1
+ import { describe, expect, it } from 'vitest'
2
+ import { computeColumnGroupMeta, hiddenLeavesForCollapse } from './column-groups'
3
+
4
+ // A group "Q1" with an always-on Total plus Jan/Feb that only show when open.
5
+ const columns = [
6
+ { field: 'name' },
7
+ {
8
+ id: 'q1',
9
+ header: 'Q1',
10
+ columns: [
11
+ { field: 'q1Total' },
12
+ { field: 'jan', columnGroupShow: 'open' },
13
+ { field: 'feb', columnGroupShow: 'open' },
14
+ ],
15
+ },
16
+ {
17
+ id: 'q2',
18
+ header: 'Q2',
19
+ openByDefault: true,
20
+ columns: [
21
+ { field: 'q2Total', columnGroupShow: 'closed' },
22
+ { field: 'apr', columnGroupShow: 'open' },
23
+ ],
24
+ },
25
+ ]
26
+
27
+ describe('computeColumnGroupMeta', () => {
28
+ it('marks groups with columnGroupShow children as collapsible', () => {
29
+ const meta = computeColumnGroupMeta(columns)
30
+ expect([...meta.collapsibleGroupIds].sort()).toEqual(['q1', 'q2'])
31
+ expect(meta.defaultOpen.get('q1')).toBe(false)
32
+ expect(meta.defaultOpen.get('q2')).toBe(true)
33
+ })
34
+ it('maps controlled leaves to their group + show mode', () => {
35
+ const meta = computeColumnGroupMeta(columns)
36
+ expect(meta.leafControl.get('jan')).toEqual({ groupId: 'q1', show: 'open' })
37
+ expect(meta.leafControl.get('q2Total')).toEqual({ groupId: 'q2', show: 'closed' })
38
+ // untagged always-on column is not controlled
39
+ expect(meta.leafControl.has('q1Total')).toBe(false)
40
+ expect(meta.leafControl.has('name')).toBe(false)
41
+ })
42
+ })
43
+
44
+ describe('hiddenLeavesForCollapse', () => {
45
+ const meta = computeColumnGroupMeta(columns)
46
+ it('hides open-columns while collapsed, shows them while expanded', () => {
47
+ const collapsed = new Set(['q1'])
48
+ const hidden = hiddenLeavesForCollapse(meta, collapsed)
49
+ expect(hidden.jan).toBe(true)
50
+ expect(hidden.feb).toBe(true)
51
+ expect(hidden.q1Total).toBeUndefined() // always on
52
+ })
53
+ it('hides closed-columns while expanded', () => {
54
+ const collapsed = new Set<string>() // q2 expanded
55
+ const hidden = hiddenLeavesForCollapse(meta, collapsed)
56
+ expect(hidden.q2Total).toBe(true) // 'closed' hides when expanded
57
+ expect(hidden.apr).toBeUndefined() // 'open' shows when expanded
58
+ })
59
+ })
@@ -0,0 +1,80 @@
1
+ // Collapsible column groups (AG-Grid `columnGroupShow`). A column group can
2
+ // carry a collapse toggle: its child columns tagged `columnGroupShow: 'open'`
3
+ // show only when the group is expanded, `'closed'` show only when collapsed,
4
+ // and untagged children always show.
5
+ //
6
+ // This module derives, from the user's column tree, the pure metadata the
7
+ // controller needs: which groups are collapsible, their default state, and each
8
+ // controlled leaf's group + show-mode. The controller turns that into a hidden
9
+ // map (merged into column visibility) AND feeds the same hidden set into the
10
+ // group-header colspan/width math, so headers stay aligned with the leaves.
11
+
12
+ export type ColumnGroupShow = "open" | "closed";
13
+
14
+ export type ColumnGroupMeta = {
15
+ /** Group ids that render a collapse toggle. */
16
+ collapsibleGroupIds: Set<string>;
17
+ /** Group id -> whether it starts expanded (openByDefault). */
18
+ defaultOpen: Map<string, boolean>;
19
+ /** Leaf column id -> the group controlling it + its show mode. */
20
+ leafControl: Map<string, { groupId: string; show: ColumnGroupShow }>;
21
+ };
22
+
23
+ // Mirror the id resolution used by the group-header derivation so group ids and
24
+ // leaf ids line up across both.
25
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
26
+ function idOf(def: any, parentId: string | undefined, ix: number): string {
27
+ return def.id ?? def.field ?? `${parentId ?? "col"}_d_${ix}`;
28
+ }
29
+
30
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
31
+ export function computeColumnGroupMeta(columns: ReadonlyArray<any>): ColumnGroupMeta {
32
+ const collapsibleGroupIds = new Set<string>();
33
+ const defaultOpen = new Map<string, boolean>();
34
+ const leafControl = new Map<string, { groupId: string; show: ColumnGroupShow }>();
35
+
36
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
37
+ function walk(defs: ReadonlyArray<any>, parentId: string | undefined, controllingGroup: string | undefined) {
38
+ defs.forEach((def, ix) => {
39
+ const id = idOf(def, parentId, ix);
40
+ if (def.columns?.length) {
41
+ // A group is collapsible when any DIRECT child declares columnGroupShow.
42
+ const collapsible = def.columns.some(
43
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
44
+ (c: any) => c.columnGroupShow === "open" || c.columnGroupShow === "closed",
45
+ );
46
+ if (collapsible) {
47
+ collapsibleGroupIds.add(id);
48
+ defaultOpen.set(id, def.openByDefault === true);
49
+ }
50
+ walk(def.columns, id, collapsible ? id : controllingGroup);
51
+ } else {
52
+ const show = def.columnGroupShow;
53
+ if ((show === "open" || show === "closed") && controllingGroup) {
54
+ leafControl.set(id, { groupId: controllingGroup, show });
55
+ }
56
+ }
57
+ });
58
+ }
59
+
60
+ walk(columns, undefined, undefined);
61
+ return { collapsibleGroupIds, defaultOpen, leafControl };
62
+ }
63
+
64
+ /**
65
+ * Which leaf ids are hidden right now, given the set of collapsed group ids.
66
+ * `'open'` leaves hide while their group is collapsed; `'closed'` leaves hide
67
+ * while it is expanded.
68
+ */
69
+ export function hiddenLeavesForCollapse(
70
+ meta: ColumnGroupMeta,
71
+ collapsed: ReadonlySet<string>,
72
+ ): Record<string, boolean> {
73
+ const hidden: Record<string, boolean> = {};
74
+ for (const [leafId, ctrl] of meta.leafControl) {
75
+ const isCollapsed = collapsed.has(ctrl.groupId);
76
+ const hide = ctrl.show === "open" ? isCollapsed : !isCollapsed;
77
+ if (hide) hidden[leafId] = true;
78
+ }
79
+ return hidden;
80
+ }
@@ -0,0 +1,68 @@
1
+ import { describe, expect, it } from 'vitest'
2
+ import { inferCellDataType, resolveColumnTypes } from './column-types'
3
+
4
+ describe('inferCellDataType', () => {
5
+ it('classifies primitives', () => {
6
+ expect(inferCellDataType(42)).toBe('number')
7
+ expect(inferCellDataType(true)).toBe('boolean')
8
+ expect(inferCellDataType(new Date())).toBe('date')
9
+ expect(inferCellDataType('2026-06-27')).toBe('dateString')
10
+ expect(inferCellDataType('2026-06-27T10:00:00Z')).toBe('dateString')
11
+ expect(inferCellDataType('hello')).toBe('text')
12
+ })
13
+ it('returns undefined for null/undefined so nothing is forced', () => {
14
+ expect(inferCellDataType(null)).toBeUndefined()
15
+ expect(inferCellDataType(undefined)).toBeUndefined()
16
+ })
17
+ })
18
+
19
+ describe('resolveColumnTypes', () => {
20
+ it('maps explicit cellDataType to editorType + format', () => {
21
+ const cols = [
22
+ { field: 'name', cellDataType: 'text' },
23
+ { field: 'age', cellDataType: 'number' },
24
+ { field: 'active', cellDataType: 'boolean' },
25
+ { field: 'joined', cellDataType: 'date' },
26
+ { field: 'iso', cellDataType: 'dateString' },
27
+ ]
28
+ const out = resolveColumnTypes(cols, undefined, false)
29
+ expect(out[0].editorType).toBe('text')
30
+ expect(out[1].editorType).toBe('number')
31
+ expect(out[2].editorType).toBe('checkbox')
32
+ expect(out[3].editorType).toBe('date')
33
+ expect(out[3].format).toEqual({ type: 'date' })
34
+ expect(out[4].editorType).toBe('date')
35
+ expect(out[4].format).toBeUndefined()
36
+ })
37
+
38
+ it('never overrides an explicit editorType or format', () => {
39
+ const cols = [{ field: 'age', cellDataType: 'number', editorType: 'text', format: { type: 'currency' } }]
40
+ const out = resolveColumnTypes(cols, undefined, false)
41
+ expect(out[0].editorType).toBe('text')
42
+ expect(out[0].format).toEqual({ type: 'currency' })
43
+ })
44
+
45
+ it('infers from a sample row only when infer=true and nothing is declared', () => {
46
+ const cols = [
47
+ { field: 'age' },
48
+ { field: 'name' },
49
+ { field: 'when' },
50
+ { field: 'flag' },
51
+ ]
52
+ const sample = { age: 30, name: 'Ada', when: '2026-01-02', flag: false }
53
+ const off = resolveColumnTypes(cols, sample, false)
54
+ expect(off[0].editorType).toBeUndefined() // inference off
55
+
56
+ const on = resolveColumnTypes(cols, sample, true)
57
+ expect(on[0].editorType).toBe('number')
58
+ expect(on[1].editorType).toBe('text')
59
+ expect(on[2].editorType).toBe('date')
60
+ expect(on[3].editorType).toBe('checkbox')
61
+ })
62
+
63
+ it('recurses into header groups', () => {
64
+ const cols = [{ header: 'Group', columns: [{ field: 'age', cellDataType: 'number' }] }]
65
+ const out = resolveColumnTypes(cols, undefined, false)
66
+ expect(out[0].columns[0].editorType).toBe('number')
67
+ })
68
+ })
@@ -0,0 +1,82 @@
1
+ // cellDataType resolution. A column's `cellDataType` (or grid-level
2
+ // `inferColumnTypes`) is a high-level shorthand that fills in `editorType`,
3
+ // `format`, and - transitively, via `getColumnAlign` and the filter-operator
4
+ // tables, which both key off `editorType` - alignment and filter operators.
5
+ //
6
+ // Anything set explicitly on the ColumnDef always wins; this only fills gaps.
7
+ // Resolution happens once when columns are ingested, producing plain ColumnDef
8
+ // objects, so every downstream reader (editing, filtering, formatting,
9
+ // alignment) sees a normal column with no new code paths.
10
+ export type CellDataType = "text" | "number" | "boolean" | "date" | "dateString";
11
+
12
+ type TypeDefaults = {
13
+ editorType: string;
14
+ format?: { type: string };
15
+ };
16
+
17
+ function defaultsFor(dt: CellDataType): TypeDefaults {
18
+ switch (dt) {
19
+ case "number":
20
+ return { editorType: "number" };
21
+ case "boolean":
22
+ return { editorType: "checkbox" };
23
+ case "date":
24
+ return { editorType: "date", format: { type: "date" } };
25
+ case "dateString":
26
+ // ISO date strings ('2026-06-27') - date editor, but no Date coercion.
27
+ return { editorType: "date" };
28
+ case "text":
29
+ default:
30
+ return { editorType: "text" };
31
+ }
32
+ }
33
+
34
+ /** Best-effort inference of a column's data type from a sample value. */
35
+ export function inferCellDataType(value: unknown): CellDataType | undefined {
36
+ if (value == null) return undefined;
37
+ if (typeof value === "number" && Number.isFinite(value)) return "number";
38
+ if (typeof value === "boolean") return "boolean";
39
+ if (value instanceof Date) return "date";
40
+ if (typeof value === "string" && /^\d{4}-\d{2}-\d{2}([T\s]|$)/.test(value)) {
41
+ return "dateString";
42
+ }
43
+ return "text";
44
+ }
45
+
46
+ /**
47
+ * Resolve `cellDataType` (explicit or, when `infer` is on, inferred from
48
+ * `sampleRow`) into concrete `editorType` / `format` defaults. Explicit fields
49
+ * on the ColumnDef are preserved. Columns that resolve to no type are returned
50
+ * untouched. Nested column groups are resolved recursively.
51
+ */
52
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
53
+ export function resolveColumnTypes(
54
+ columns: ReadonlyArray<any>,
55
+ sampleRow: unknown,
56
+ infer: boolean,
57
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
58
+ ): any[] {
59
+ return columns.map((col) => {
60
+ // Recurse into header groups first.
61
+ if (col.columns && col.columns.length) {
62
+ return { ...col, columns: resolveColumnTypes(col.columns, sampleRow, infer) };
63
+ }
64
+ // An explicit editorType means the author already chose - don't override.
65
+ const hasExplicitEditor = col.editorType != null;
66
+ let dt: CellDataType | undefined = col.cellDataType;
67
+ if (!dt && infer && !hasExplicitEditor && sampleRow != null) {
68
+ const field = col.field;
69
+ const raw =
70
+ col.accessorFn?.(sampleRow) ??
71
+ (typeof field === "string" ? (sampleRow as Record<string, unknown>)[field] : undefined);
72
+ dt = inferCellDataType(raw);
73
+ }
74
+ if (!dt) return col;
75
+ const d = defaultsFor(dt);
76
+ return {
77
+ ...col,
78
+ editorType: col.editorType ?? d.editorType,
79
+ format: col.format ?? d.format,
80
+ };
81
+ });
82
+ }