@svgrid/grid 1.2.22 → 1.2.24

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 (263) hide show
  1. package/dist/GridFooter.svelte +34 -20
  2. package/dist/GridFooter.svelte.d.ts +8 -0
  3. package/dist/SvAutoComplete.svelte +89 -0
  4. package/dist/SvAutoComplete.svelte.d.ts +16 -0
  5. package/dist/SvButton.svelte +97 -0
  6. package/dist/SvButton.svelte.d.ts +23 -0
  7. package/dist/SvCalendar.svelte +409 -0
  8. package/dist/SvCalendar.svelte.d.ts +52 -0
  9. package/dist/SvCheckBox.svelte +82 -0
  10. package/dist/SvCheckBox.svelte.d.ts +19 -0
  11. package/dist/SvColorInput.svelte +160 -0
  12. package/dist/SvColorInput.svelte.d.ts +15 -0
  13. package/dist/SvComboBox.svelte +108 -0
  14. package/dist/SvComboBox.svelte.d.ts +15 -0
  15. package/dist/SvCountryInput.svelte +125 -0
  16. package/dist/SvCountryInput.svelte.d.ts +14 -0
  17. package/dist/SvDateTimePicker.svelte +265 -0
  18. package/dist/SvDateTimePicker.svelte.d.ts +38 -0
  19. package/dist/SvDropDownList.svelte +120 -0
  20. package/dist/SvDropDownList.svelte.d.ts +15 -0
  21. package/dist/SvForm.svelte +147 -0
  22. package/dist/SvForm.svelte.d.ts +29 -0
  23. package/dist/SvGauge.svelte +97 -0
  24. package/dist/SvGauge.svelte.d.ts +26 -0
  25. package/dist/SvGrid.controller.svelte.d.ts +16 -2
  26. package/dist/SvGrid.controller.svelte.js +63 -8
  27. package/dist/SvGrid.css +84 -31
  28. package/dist/SvGrid.helpers.d.ts +4 -0
  29. package/dist/SvGrid.helpers.js +40 -12
  30. package/dist/SvGrid.svelte +84 -5
  31. package/dist/SvGrid.types.d.ts +36 -0
  32. package/dist/SvGridDropdown.svelte +16 -30
  33. package/dist/SvGridDropdown.svelte.d.ts +4 -0
  34. package/dist/SvListBox.svelte +89 -0
  35. package/dist/SvListBox.svelte.d.ts +22 -0
  36. package/dist/SvMaskedInput.svelte +66 -0
  37. package/dist/SvMaskedInput.svelte.d.ts +15 -0
  38. package/dist/SvNumberInput.svelte +116 -0
  39. package/dist/SvNumberInput.svelte.d.ts +18 -0
  40. package/dist/SvPasswordInput.svelte +100 -0
  41. package/dist/SvPasswordInput.svelte.d.ts +18 -0
  42. package/dist/SvPhoneInput.svelte +87 -0
  43. package/dist/SvPhoneInput.svelte.d.ts +19 -0
  44. package/dist/SvRadioGroup.svelte +85 -0
  45. package/dist/SvRadioGroup.svelte.d.ts +20 -0
  46. package/dist/SvRating.svelte +91 -0
  47. package/dist/SvRating.svelte.d.ts +18 -0
  48. package/dist/SvRepeatButton.svelte +89 -0
  49. package/dist/SvRepeatButton.svelte.d.ts +21 -0
  50. package/dist/SvSlider.svelte +155 -0
  51. package/dist/SvSlider.svelte.d.ts +20 -0
  52. package/dist/SvSwitchButton.svelte +87 -0
  53. package/dist/SvSwitchButton.svelte.d.ts +14 -0
  54. package/dist/SvTabs.svelte +109 -0
  55. package/dist/SvTabs.svelte.d.ts +26 -0
  56. package/dist/SvTagsInput.svelte +70 -0
  57. package/dist/SvTagsInput.svelte.d.ts +14 -0
  58. package/dist/SvTimePicker.svelte +201 -0
  59. package/dist/SvTimePicker.svelte.d.ts +30 -0
  60. package/dist/SvToggleButton.svelte +57 -0
  61. package/dist/SvToggleButton.svelte.d.ts +16 -0
  62. package/dist/SvTree.svelte +131 -0
  63. package/dist/SvTree.svelte.d.ts +27 -0
  64. package/dist/build-api.js +78 -1
  65. package/dist/cdn/svgrid.js +11674 -5907
  66. package/dist/cdn/svgrid.svelte-external.js +10436 -4671
  67. package/dist/cell-render.d.ts +4 -0
  68. package/dist/cell-render.js +35 -1
  69. package/dist/clipboard.js +19 -1
  70. package/dist/collaboration.d.ts +2 -0
  71. package/dist/collaboration.js +4 -0
  72. package/dist/core.d.ts +22 -1
  73. package/dist/countries.d.ts +15 -0
  74. package/dist/countries.js +61 -0
  75. package/dist/createAutocomplete.svelte.d.ts +77 -0
  76. package/dist/createAutocomplete.svelte.js +100 -0
  77. package/dist/createCalendar.svelte.d.ts +192 -0
  78. package/dist/createCalendar.svelte.js +402 -0
  79. package/dist/createCheckbox.svelte.d.ts +45 -0
  80. package/dist/createCheckbox.svelte.js +26 -0
  81. package/dist/createColorInput.svelte.d.ts +67 -0
  82. package/dist/createColorInput.svelte.js +100 -0
  83. package/dist/createCombobox.svelte.d.ts +102 -0
  84. package/dist/createCombobox.svelte.js +171 -0
  85. package/dist/createCountryInput.svelte.d.ts +86 -0
  86. package/dist/createCountryInput.svelte.js +126 -0
  87. package/dist/createDateTimePicker.svelte.d.ts +83 -0
  88. package/dist/createDateTimePicker.svelte.js +202 -0
  89. package/dist/createDropdownList.svelte.d.ts +78 -0
  90. package/dist/createDropdownList.svelte.js +117 -0
  91. package/dist/createGauge.svelte.d.ts +56 -0
  92. package/dist/createGauge.svelte.js +49 -0
  93. package/dist/createListbox.svelte.d.ts +79 -0
  94. package/dist/createListbox.svelte.js +117 -0
  95. package/dist/createMaskedInput.svelte.d.ts +30 -0
  96. package/dist/createMaskedInput.svelte.js +62 -0
  97. package/dist/createNumberInput.svelte.d.ts +72 -0
  98. package/dist/createNumberInput.svelte.js +167 -0
  99. package/dist/createPasswordInput.svelte.d.ts +60 -0
  100. package/dist/createPasswordInput.svelte.js +72 -0
  101. package/dist/createPhoneInput.svelte.d.ts +46 -0
  102. package/dist/createPhoneInput.svelte.js +105 -0
  103. package/dist/createRadioGroup.svelte.d.ts +64 -0
  104. package/dist/createRadioGroup.svelte.js +73 -0
  105. package/dist/createRating.svelte.d.ts +70 -0
  106. package/dist/createRating.svelte.js +85 -0
  107. package/dist/createSlider.svelte.d.ts +71 -0
  108. package/dist/createSlider.svelte.js +123 -0
  109. package/dist/createSwitch.svelte.d.ts +43 -0
  110. package/dist/createSwitch.svelte.js +41 -0
  111. package/dist/createTabs.svelte.d.ts +78 -0
  112. package/dist/createTabs.svelte.js +92 -0
  113. package/dist/createTagsInput.svelte.d.ts +65 -0
  114. package/dist/createTagsInput.svelte.js +93 -0
  115. package/dist/createTimePicker.svelte.d.ts +106 -0
  116. package/dist/createTimePicker.svelte.js +248 -0
  117. package/dist/createToggle.svelte.d.ts +43 -0
  118. package/dist/createToggle.svelte.js +39 -0
  119. package/dist/createTree.svelte.d.ts +96 -0
  120. package/dist/createTree.svelte.js +194 -0
  121. package/dist/datetime/date-core.d.ts +88 -0
  122. package/dist/datetime/date-core.js +165 -0
  123. package/dist/datetime/date-format.d.ts +39 -0
  124. package/dist/datetime/date-format.js +371 -0
  125. package/dist/datetime/date-restrict.d.ts +23 -0
  126. package/dist/datetime/date-restrict.js +50 -0
  127. package/dist/datetime/date-selection.d.ts +25 -0
  128. package/dist/datetime/date-selection.js +94 -0
  129. package/dist/datetime/mask.d.ts +21 -0
  130. package/dist/datetime/mask.js +90 -0
  131. package/dist/editing.d.ts +1 -1
  132. package/dist/editing.js +7 -0
  133. package/dist/editor-contract.d.ts +48 -0
  134. package/dist/editor-contract.js +26 -0
  135. package/dist/editors/cell-editors.d.ts +2 -2
  136. package/dist/editors/cell-editors.js +3 -1
  137. package/dist/export-format.d.ts +178 -0
  138. package/dist/export-format.js +419 -0
  139. package/dist/hyperformula-adapter.d.ts +4 -0
  140. package/dist/hyperformula-adapter.js +15 -6
  141. package/dist/index.d.ts +60 -0
  142. package/dist/index.js +72 -0
  143. package/dist/list-option.d.ts +10 -0
  144. package/dist/list-option.js +7 -0
  145. package/dist/menus.js +18 -2
  146. package/dist/popover.d.ts +48 -0
  147. package/dist/popover.js +65 -0
  148. package/dist/row-resize.d.ts +4 -4
  149. package/dist/row-resize.js +8 -5
  150. package/dist/selection.d.ts +6 -0
  151. package/dist/selection.js +31 -5
  152. package/dist/server-data-source.d.ts +43 -0
  153. package/dist/server-data-source.js +100 -0
  154. package/dist/sparkline.js +8 -4
  155. package/dist/svgrid-wrapper.types.d.ts +26 -1
  156. package/package.json +6 -1
  157. package/src/GridFooter.svelte +34 -20
  158. package/src/SvAutoComplete.svelte +89 -0
  159. package/src/SvButton.svelte +97 -0
  160. package/src/SvCalendar.svelte +409 -0
  161. package/src/SvCalendar.test.ts +211 -0
  162. package/src/SvCheckBox.svelte +82 -0
  163. package/src/SvColorInput.svelte +160 -0
  164. package/src/SvComboBox.svelte +108 -0
  165. package/src/SvCountryInput.svelte +125 -0
  166. package/src/SvDateTimePicker.svelte +265 -0
  167. package/src/SvDateTimePicker.test.ts +136 -0
  168. package/src/SvDropDownList.svelte +120 -0
  169. package/src/SvForm.svelte +147 -0
  170. package/src/SvGauge.svelte +97 -0
  171. package/src/SvGrid.controller.svelte.ts +74 -8
  172. package/src/SvGrid.css +84 -31
  173. package/src/SvGrid.helpers.ts +41 -12
  174. package/src/SvGrid.svelte +84 -5
  175. package/src/SvGrid.types.ts +33 -0
  176. package/src/SvGridDropdown.svelte +16 -30
  177. package/src/SvListBox.svelte +89 -0
  178. package/src/SvMaskedInput.svelte +66 -0
  179. package/src/SvNumberInput.svelte +116 -0
  180. package/src/SvPasswordInput.svelte +100 -0
  181. package/src/SvPhoneInput.svelte +87 -0
  182. package/src/SvRadioGroup.svelte +85 -0
  183. package/src/SvRating.svelte +91 -0
  184. package/src/SvRepeatButton.svelte +89 -0
  185. package/src/SvSlider.svelte +155 -0
  186. package/src/SvSwitchButton.svelte +87 -0
  187. package/src/SvTabs.svelte +109 -0
  188. package/src/SvTagsInput.svelte +70 -0
  189. package/src/SvTimePicker.svelte +201 -0
  190. package/src/SvTimePicker.test.ts +98 -0
  191. package/src/SvToggleButton.svelte +57 -0
  192. package/src/SvTree.svelte +131 -0
  193. package/src/build-api.ts +115 -0
  194. package/src/cell-render.test.ts +38 -0
  195. package/src/cell-render.ts +43 -0
  196. package/src/clipboard.test.ts +18 -0
  197. package/src/clipboard.ts +24 -1
  198. package/src/collaboration.test.ts +30 -0
  199. package/src/collaboration.ts +6 -0
  200. package/src/core.ts +27 -3
  201. package/src/countries.ts +71 -0
  202. package/src/createAutocomplete.svelte.ts +104 -0
  203. package/src/createCalendar.svelte.ts +488 -0
  204. package/src/createCheckbox.svelte.ts +74 -0
  205. package/src/createColorInput.svelte.ts +115 -0
  206. package/src/createCombobox.svelte.ts +158 -0
  207. package/src/createCountryInput.svelte.ts +126 -0
  208. package/src/createDateTimePicker.svelte.ts +216 -0
  209. package/src/createDropdownList.svelte.ts +125 -0
  210. package/src/createGauge.svelte.ts +86 -0
  211. package/src/createListbox.svelte.ts +186 -0
  212. package/src/createMaskedInput.svelte.ts +81 -0
  213. package/src/createNumberInput.svelte.ts +193 -0
  214. package/src/createPasswordInput.svelte.ts +87 -0
  215. package/src/createPhoneInput.svelte.ts +119 -0
  216. package/src/createRadioGroup.svelte.ts +132 -0
  217. package/src/createRating.svelte.ts +145 -0
  218. package/src/createSlider.svelte.ts +160 -0
  219. package/src/createSwitch.svelte.ts +78 -0
  220. package/src/createTabs.svelte.ts +129 -0
  221. package/src/createTagsInput.svelte.ts +98 -0
  222. package/src/createTimePicker.svelte.ts +289 -0
  223. package/src/createToggle.svelte.ts +69 -0
  224. package/src/createTree.svelte.ts +216 -0
  225. package/src/datetime/date-core.test.ts +217 -0
  226. package/src/datetime/date-core.ts +204 -0
  227. package/src/datetime/date-format.test.ts +121 -0
  228. package/src/datetime/date-format.ts +317 -0
  229. package/src/datetime/date-restrict.ts +60 -0
  230. package/src/datetime/date-selection.test.ts +129 -0
  231. package/src/datetime/date-selection.ts +137 -0
  232. package/src/datetime/mask.test.ts +36 -0
  233. package/src/datetime/mask.ts +84 -0
  234. package/src/editing.test.ts +22 -0
  235. package/src/editing.ts +7 -1
  236. package/src/editor-contract.test.ts +32 -0
  237. package/src/editor-contract.ts +60 -0
  238. package/src/editors/cell-editors.ts +7 -1
  239. package/src/export-data-api.test.ts +126 -0
  240. package/src/export-format.test.ts +107 -0
  241. package/src/export-format.ts +598 -0
  242. package/src/hyperformula-adapter.test.ts +35 -1
  243. package/src/hyperformula-adapter.ts +18 -6
  244. package/src/index.ts +110 -0
  245. package/src/list-option.ts +15 -0
  246. package/src/menus.ts +21 -2
  247. package/src/popover.ts +79 -0
  248. package/src/row-resize.test.ts +25 -0
  249. package/src/row-resize.ts +8 -5
  250. package/src/selection.test.ts +36 -4
  251. package/src/selection.ts +30 -9
  252. package/src/server-data-source.test.ts +201 -0
  253. package/src/server-data-source.ts +148 -0
  254. package/src/sparkline.test.ts +9 -0
  255. package/src/sparkline.ts +9 -4
  256. package/src/svgrid-wrapper.types.ts +28 -1
  257. package/src/svgrid.behavior.test.ts +27 -0
  258. package/src/svgrid.new-features.wrapper.test.ts +27 -1
  259. package/src/ui-buttons.test.ts +105 -0
  260. package/src/ui-composite.test.ts +89 -0
  261. package/src/ui-inputs.test.ts +118 -0
  262. package/src/ui-range.test.ts +61 -0
  263. package/src/ui-selection.test.ts +119 -0
@@ -0,0 +1,137 @@
1
+ /**
2
+ * date-selection - implements every Smart calendar `selectionMode` as a pure
3
+ * reducer over the current selection. The calendar component owns pointer/key
4
+ * handling and delegates the "what gets selected" decision here so the rules
5
+ * are unit-testable in isolation.
6
+ */
7
+ import { addDays, compareDay, isSameDay, startOfDay, startOfWeek } from './date-core'
8
+
9
+ export type SelectionMode =
10
+ | 'none'
11
+ | 'default'
12
+ | 'one'
13
+ | 'zeroOrOne'
14
+ | 'oneExtended'
15
+ | 'many'
16
+ | 'zeroOrMany'
17
+ | 'oneOrMany'
18
+ | 'week'
19
+ | 'range'
20
+
21
+ export type SelectionModifiers = { ctrl?: boolean; shift?: boolean }
22
+
23
+ export type SelectionState = {
24
+ /** Selected days (deduped, sorted ascending), always local-midnight. */
25
+ dates: Date[]
26
+ /** Anchor for shift-extend (oneExtended). */
27
+ anchor: Date | null
28
+ /** Pending range start (range mode, between first and second click). */
29
+ rangeStart: Date | null
30
+ }
31
+
32
+ export const emptySelection = (): SelectionState => ({ dates: [], anchor: null, rangeStart: null })
33
+
34
+ /** True when `day` is in `dates` (by calendar day). */
35
+ export function isSelected(dates: ReadonlyArray<Date>, day: Date): boolean {
36
+ return dates.some((d) => isSameDay(d, day))
37
+ }
38
+
39
+ /** Inclusive ordered list of day-midnight dates from a..b (either order). */
40
+ export function rangeDays(a: Date, b: Date): Date[] {
41
+ const lo = compareDay(a, b) <= 0 ? startOfDay(a) : startOfDay(b)
42
+ const hi = compareDay(a, b) <= 0 ? startOfDay(b) : startOfDay(a)
43
+ const out: Date[] = []
44
+ let cursor = lo
45
+ while (compareDay(cursor, hi) <= 0) {
46
+ out.push(cursor)
47
+ cursor = addDays(cursor, 1)
48
+ }
49
+ return out
50
+ }
51
+
52
+ function sortDedup(dates: Date[]): Date[] {
53
+ const uniq: Date[] = []
54
+ for (const d of dates) if (!uniq.some((u) => isSameDay(u, d))) uniq.push(startOfDay(d))
55
+ uniq.sort((a, b) => a.getTime() - b.getTime())
56
+ return uniq
57
+ }
58
+
59
+ function toggle(dates: Date[], day: Date): Date[] {
60
+ return isSelected(dates, day) ? dates.filter((d) => !isSameDay(d, day)) : [...dates, startOfDay(day)]
61
+ }
62
+
63
+ /**
64
+ * Apply a click on `clicked` to `state` under `mode`. `firstDayOfWeek` only
65
+ * matters for `week` mode. Returns a fresh state (never mutates the input).
66
+ */
67
+ export function selectDate(
68
+ state: SelectionState,
69
+ clicked: Date,
70
+ mode: SelectionMode,
71
+ modifiers: SelectionModifiers = {},
72
+ firstDayOfWeek = 0,
73
+ ): SelectionState {
74
+ const day = startOfDay(clicked)
75
+ const single = (): SelectionState => ({ dates: [day], anchor: day, rangeStart: null })
76
+
77
+ switch (mode) {
78
+ case 'none':
79
+ return { ...state }
80
+
81
+ case 'default':
82
+ case 'one':
83
+ return single()
84
+
85
+ case 'zeroOrOne':
86
+ // Toggle: clicking the sole selected day clears it.
87
+ return isSelected(state.dates, day) && state.dates.length === 1
88
+ ? emptySelection()
89
+ : single()
90
+
91
+ case 'many':
92
+ case 'zeroOrMany': {
93
+ const next = sortDedup(toggle(state.dates, day))
94
+ return { dates: next, anchor: day, rangeStart: null }
95
+ }
96
+
97
+ case 'oneOrMany': {
98
+ const toggled = toggle(state.dates, day)
99
+ // Never allow emptying below one.
100
+ const next = toggled.length === 0 ? [day] : toggled
101
+ return { dates: sortDedup(next), anchor: day, rangeStart: null }
102
+ }
103
+
104
+ case 'oneExtended': {
105
+ if (modifiers.shift && state.anchor) {
106
+ return { dates: rangeDays(state.anchor, day), anchor: state.anchor, rangeStart: null }
107
+ }
108
+ if (modifiers.ctrl) {
109
+ const toggled = sortDedup(toggle(state.dates, day))
110
+ return { dates: toggled, anchor: day, rangeStart: null }
111
+ }
112
+ return single()
113
+ }
114
+
115
+ case 'week': {
116
+ const start = startOfWeek(day, firstDayOfWeek)
117
+ const days = Array.from({ length: 7 }, (_, i) => addDays(start, i))
118
+ return { dates: days, anchor: start, rangeStart: null }
119
+ }
120
+
121
+ case 'range': {
122
+ // First click sets the start; second click completes the range.
123
+ if (!state.rangeStart) {
124
+ return { dates: [day], anchor: day, rangeStart: day }
125
+ }
126
+ return { dates: rangeDays(state.rangeStart, day), anchor: state.rangeStart, rangeStart: null }
127
+ }
128
+
129
+ default:
130
+ return single()
131
+ }
132
+ }
133
+
134
+ /** Whether a mode ever holds more than one date (drives multi-select UI hints). */
135
+ export function isMultiSelectMode(mode: SelectionMode): boolean {
136
+ return mode === 'many' || mode === 'zeroOrMany' || mode === 'oneOrMany' || mode === 'oneExtended' || mode === 'week' || mode === 'range'
137
+ }
@@ -0,0 +1,36 @@
1
+ import { describe, expect, it } from 'vitest'
2
+ import { applyMask, unmask, isMaskComplete } from './mask'
3
+
4
+ describe('applyMask', () => {
5
+ it('formats a phone pattern, inserting literals', () => {
6
+ expect(applyMask('1234567890', '(###) ###-####').formatted).toBe('(123) 456-7890')
7
+ expect(applyMask('123', '(###) ###-####').formatted).toBe('(123')
8
+ expect(applyMask('1234', '(###) ###-####').formatted).toBe('(123) 4')
9
+ })
10
+ it('drops chars that do not fit the token', () => {
11
+ expect(applyMask('12ab34', '####').formatted).toBe('1234')
12
+ expect(applyMask('ab12', 'AA##').formatted).toBe('ab12')
13
+ // *### : '*' takes 'a', then the three '#' take the digits, skipping 'b'.
14
+ expect(applyMask('a1b2', '*###').formatted).toBe('a12')
15
+ })
16
+ it('* accepts letter or digit', () => {
17
+ expect(applyMask('a1', '**').formatted).toBe('a1')
18
+ })
19
+ it('empty input yields empty', () => {
20
+ expect(applyMask('', '(###)').formatted).toBe('')
21
+ })
22
+ })
23
+
24
+ describe('unmask', () => {
25
+ it('returns only data characters', () => {
26
+ expect(unmask('(123) 456-7890', '(###) ###-####')).toBe('1234567890')
27
+ expect(unmask('ab-12', 'AA-##')).toBe('ab12')
28
+ })
29
+ })
30
+
31
+ describe('isMaskComplete', () => {
32
+ it('true only when every token is filled', () => {
33
+ expect(isMaskComplete('(123) 456-7890', '(###) ###-####')).toBe(true)
34
+ expect(isMaskComplete('(123) 456', '(###) ###-####')).toBe(false)
35
+ })
36
+ })
@@ -0,0 +1,84 @@
1
+ /**
2
+ * mask - a tiny pattern-mask engine shared by SvMaskedInput / SvPhoneInput.
3
+ * Pattern tokens:
4
+ * # a digit (0-9)
5
+ * A a letter (a-z A-Z)
6
+ * * a letter or digit
7
+ * Any other character is a literal that is auto-inserted. Framework-free.
8
+ */
9
+
10
+ const isDigit = (c: string) => c >= '0' && c <= '9'
11
+ const isAlpha = (c: string) => /[a-zA-Z]/.test(c)
12
+
13
+ function matches(token: string, ch: string): boolean {
14
+ if (token === '#') return isDigit(ch)
15
+ if (token === 'A') return isAlpha(ch)
16
+ if (token === '*') return isDigit(ch) || isAlpha(ch)
17
+ return false
18
+ }
19
+
20
+ const isToken = (c: string) => c === '#' || c === 'A' || c === '*'
21
+
22
+ /**
23
+ * Apply `pattern` to raw input `value`, inserting literals and dropping chars
24
+ * that don't fit the next token. Returns the formatted string and the count of
25
+ * consumed input characters (useful for caret math).
26
+ */
27
+ export function applyMask(value: string, pattern: string): { formatted: string; filled: number } {
28
+ let out = ''
29
+ let vi = 0
30
+ let filled = 0
31
+ for (let pi = 0; pi < pattern.length; pi++) {
32
+ const token = pattern[pi]!
33
+ if (!isToken(token)) {
34
+ // Literal: emit it only once we have (or will have) more input to place.
35
+ out += token
36
+ // Skip a matching literal the user may have typed.
37
+ if (value[vi] === token) vi++
38
+ continue
39
+ }
40
+ // Advance through input until a char fits this token.
41
+ while (vi < value.length && !matches(token, value[vi]!)) vi++
42
+ if (vi >= value.length) break // no more input for this token
43
+ out += value[vi]!
44
+ vi++
45
+ filled++
46
+ }
47
+ // Drop any trailing literals with no following data char.
48
+ return { formatted: trimTrailingLiterals(out), filled }
49
+ }
50
+
51
+ function trimTrailingLiterals(out: string): string {
52
+ // Count how many token-positions are filled; cut the string at the last
53
+ // filled data char plus any literals strictly before the next token.
54
+ let lastData = -1
55
+ for (let i = 0; i < out.length; i++) {
56
+ const c = out[i]!
57
+ if (isDigit(c) || isAlpha(c)) lastData = i
58
+ }
59
+ if (lastData < 0) return ''
60
+ // Keep literals that sit between data chars but trim the tail after last data.
61
+ return out.slice(0, lastData + 1)
62
+ }
63
+
64
+ /** Strip literals, returning only the data characters the user supplied. */
65
+ export function unmask(value: string, pattern: string): string {
66
+ let out = ''
67
+ let vi = 0
68
+ for (let pi = 0; pi < pattern.length && vi < value.length; pi++) {
69
+ const token = pattern[pi]!
70
+ if (!isToken(token)) {
71
+ if (value[vi] === token) vi++
72
+ continue
73
+ }
74
+ while (vi < value.length && !matches(token, value[vi]!)) vi++
75
+ if (vi < value.length) { out += value[vi]!; vi++ }
76
+ }
77
+ return out
78
+ }
79
+
80
+ /** True when every token position in the pattern is filled. */
81
+ export function isMaskComplete(value: string, pattern: string): boolean {
82
+ const tokenCount = [...pattern].filter(isToken).length
83
+ return unmask(value, pattern).length >= tokenCount
84
+ }
@@ -834,4 +834,26 @@ describe('clipboard fallbacks (non-paste branches only)', () => {
834
834
  expect(ctx.internalData[0]).toMatchObject({ a: 'X', b: 'Y' })
835
835
  expect(ctx.internalData[1]).toMatchObject({ a: 'Z', b: 'W' })
836
836
  })
837
+
838
+ it('onGridPaste bails while a cell is being edited (native editor paste wins)', () => {
839
+ Object.defineProperty(navigator, 'clipboard', {
840
+ value: undefined, // fallback path (Firefox / insecure) - would otherwise hijack
841
+ configurable: true,
842
+ })
843
+ const { ctx, ed } = editingFor({
844
+ columns: [{ id: 'a', field: 'a', editorType: 'text', editable: true }],
845
+ data: [{ a: 'orig' }],
846
+ })
847
+ ctx.selectionRange = { anchor: { rowIndex: 0, colIndex: 0 }, focus: { rowIndex: 0, colIndex: 0 } }
848
+ ctx.editingCell = { rowId: '0', columnId: 'a', editorType: 'text', value: 'orig' }
849
+ const ev = {
850
+ clipboardData: { getData: () => 'PASTED' },
851
+ preventDefault: vi.fn(),
852
+ } as unknown as ClipboardEvent
853
+ ed.onGridPaste(ev)
854
+ // The grid must NOT preventDefault (so the editor's native paste proceeds)
855
+ // and must NOT write to the data cell.
856
+ expect(ev.preventDefault).not.toHaveBeenCalled()
857
+ expect(ctx.internalData[0]).toMatchObject({ a: 'orig' })
858
+ })
837
859
  })
package/src/editing.ts CHANGED
@@ -342,7 +342,7 @@ export function createEditing<
342
342
  }
343
343
  }
344
344
 
345
- function updateEditingCellValue(value: string) {
345
+ function updateEditingCellValue(value: unknown) {
346
346
  ctx.editingCell = ctx.editingCell ? { ...ctx.editingCell, value } : ctx.editingCell;
347
347
  }
348
348
 
@@ -442,6 +442,12 @@ export function createEditing<
442
442
  * (no preventDefault) whenever the async API is unavailable.
443
443
  */
444
444
  function onGridPaste(event: ClipboardEvent) {
445
+ // A cell is being edited: the focused editor input handles its own paste
446
+ // natively. Never hijack it for a range paste - doing so (on Firefox /
447
+ // insecure contexts, where the async Clipboard API is unavailable) would
448
+ // preventDefault the editor's paste and write to the data cell instead,
449
+ // so the edit "wouldn't update".
450
+ if (ctx.editingCell) return;
445
451
  // When the async API is available the keydown handler already pasted via
446
452
  // pasteFromClipboard(); ignore the (suppressed) native event so we don't
447
453
  // paste twice.
@@ -0,0 +1,32 @@
1
+ import { describe, expect, it } from 'vitest'
2
+ import { editorAria, editorErrorId } from './editor-contract'
3
+
4
+ describe('editor-contract', () => {
5
+ it('emits ARIA only for the flags that are set (undefined => attribute omitted)', () => {
6
+ expect(editorAria({})).toEqual({
7
+ 'aria-invalid': undefined,
8
+ 'aria-required': undefined,
9
+ 'aria-describedby': undefined,
10
+ 'aria-label': undefined,
11
+ })
12
+ })
13
+
14
+ it('wires invalid + required + describedby + label', () => {
15
+ const a = editorAria({ id: 'qty', invalid: true, required: true, error: 'Too big', ariaLabel: 'Quantity' })
16
+ expect(a['aria-invalid']).toBe('true')
17
+ expect(a['aria-required']).toBe('true')
18
+ expect(a['aria-describedby']).toBe('qty__error')
19
+ expect(a['aria-label']).toBe('Quantity')
20
+ })
21
+
22
+ it('describedby is only set when there is an error AND an id', () => {
23
+ expect(editorAria({ id: 'qty', error: '' })['aria-describedby']).toBeUndefined()
24
+ expect(editorAria({ error: 'x' })['aria-describedby']).toBeUndefined() // no id
25
+ expect(editorAria({ id: 'qty', error: 'x' })['aria-describedby']).toBe('qty__error')
26
+ })
27
+
28
+ it('editorErrorId', () => {
29
+ expect(editorErrorId('a')).toBe('a__error')
30
+ expect(editorErrorId(undefined)).toBeUndefined()
31
+ })
32
+ })
@@ -0,0 +1,60 @@
1
+ /**
2
+ * The shared "editor contract" for the SvGrid UI kit.
3
+ *
4
+ * Every value-bearing editor (inputs, selects, pickers) accepts this common set
5
+ * of props so forms, validation and accessibility behave IDENTICALLY across the
6
+ * kit - the same spirit as the grid's headless core, applied to the editors.
7
+ * Components spread their own `value` / options / behavior on top of this base.
8
+ *
9
+ * Framework-free (no Svelte, no DOM) so it can be unit-tested and reused.
10
+ */
11
+
12
+ export type EditorSize = 'sm' | 'md' | 'lg'
13
+
14
+ /** Props shared by every value-bearing editor in the kit. */
15
+ export type SvEditorProps = {
16
+ /** Disable interaction + form submission. */
17
+ disabled?: boolean
18
+ /** Read-only: the value is shown but not editable. */
19
+ readonly?: boolean
20
+ /** Required for validation (adds `aria-required` + participates in validity). */
21
+ required?: boolean
22
+ /** Marks the control invalid (`aria-invalid` + error styling). */
23
+ invalid?: boolean
24
+ /** Error message; when set it is announced via `aria-describedby` and shown. */
25
+ error?: string
26
+ /** Control size / density. */
27
+ size?: EditorSize
28
+ /** Form field name; the editor emits a hidden input carrying its value. */
29
+ name?: string
30
+ /** Root/control element id (the error-text id derives from it). */
31
+ id?: string
32
+ /** Accessible name when there is no visible `<label>`. */
33
+ ariaLabel?: string
34
+ }
35
+
36
+ /** Stable DOM id for an editor's error text, for `aria-describedby` wiring. */
37
+ export const editorErrorId = (id?: string): string | undefined => (id ? `${id}__error` : undefined)
38
+
39
+ /** A subset of {@link SvEditorProps} that drives ARIA on the focusable control. */
40
+ export type EditorAriaState = Pick<SvEditorProps, 'id' | 'invalid' | 'required' | 'error' | 'ariaLabel'>
41
+
42
+ /**
43
+ * ARIA attributes for the focusable control of an editor, derived from its state.
44
+ * Spread onto the input/button/combobox:
45
+ * `<input {...editorAria({ id, invalid, required, error, ariaLabel })} />`
46
+ * Undefined values are omitted by Svelte, so unset props add no attributes.
47
+ */
48
+ export function editorAria(state: EditorAriaState): {
49
+ 'aria-invalid': 'true' | undefined
50
+ 'aria-required': 'true' | undefined
51
+ 'aria-describedby': string | undefined
52
+ 'aria-label': string | undefined
53
+ } {
54
+ return {
55
+ 'aria-invalid': state.invalid ? 'true' : undefined,
56
+ 'aria-required': state.required ? 'true' : undefined,
57
+ 'aria-describedby': state.error ? editorErrorId(state.id) : undefined,
58
+ 'aria-label': state.ariaLabel,
59
+ }
60
+ }
@@ -4,6 +4,10 @@
4
4
  | 'date'
5
5
  | 'datetime'
6
6
  | 'time'
7
+ // Opt-out of the rich date/time pickers: render the plain native inputs.
8
+ | 'date-native'
9
+ | 'datetime-native'
10
+ | 'time-native'
7
11
  | 'password'
8
12
  | 'checkbox'
9
13
  | 'list'
@@ -50,10 +54,12 @@ export function normalizeEditorOptions(
50
54
  }
51
55
 
52
56
  export function parseEditorValue(
53
- type: CellEditorType,
57
+ rawType: CellEditorType,
54
58
  value: unknown,
55
59
  opts?: ParseEditorValueOptions,
56
60
  ) {
61
+ // `date-native` etc. parse identically to their rich counterparts.
62
+ const type = rawType.replace(/-native$/, '') as CellEditorType
57
63
  if (type === 'number') {
58
64
  const parsed = Number(value)
59
65
  return Number.isFinite(parsed) ? parsed : null
@@ -0,0 +1,126 @@
1
+ /**
2
+ * The free CSV / TSV / JSON export + copy-to-clipboard now on the grid API.
3
+ * Mounts a real <SvGrid /> and drives api.exportCsv/exportTsv/exportJson/
4
+ * copyToClipboard with download:false so nothing hits the DOM download path.
5
+ */
6
+ import { afterEach, describe, expect, it, vi } from 'vitest'
7
+ import { mount, unmount } from 'svelte'
8
+ import SvGrid from './SvGrid.svelte'
9
+ import {
10
+ columnFilteringFeature,
11
+ rowSelectionFeature,
12
+ rowSortingFeature,
13
+ tableFeatures,
14
+ } from './index'
15
+ import type { ColumnDef, SvGridApi } from './index'
16
+
17
+ type Row = { id: number; name: string; price: number; when: string }
18
+ const rows: Row[] = [
19
+ { id: 1, name: 'ACME, Inc', price: 1234.5, when: '2026-03-04' },
20
+ { id: 2, name: 'Globex', price: 20, when: '2026-06-01' },
21
+ ]
22
+ const columns: ColumnDef<any, Row>[] = [
23
+ { field: 'name', header: 'Company', width: 180 },
24
+ { field: 'price', header: 'Price', width: 120, align: 'right', format: { type: 'currency', currency: 'USD' } },
25
+ { field: 'when', header: 'Date', width: 120 }, // plain string, no format (deterministic)
26
+ ]
27
+
28
+ const features = tableFeatures({ columnFilteringFeature, rowSortingFeature, rowSelectionFeature })
29
+
30
+ function mountGrid(): Promise<{ api: SvGridApi<any, Row>; destroy: () => void }> {
31
+ return new Promise((resolve, reject) => {
32
+ const target = document.createElement('div')
33
+ document.body.appendChild(target)
34
+ let done = false
35
+ const app = mount(SvGrid, {
36
+ target,
37
+ props: {
38
+ data: rows.map((r) => ({ ...r })),
39
+ columns: columns.map((c) => ({ ...c })),
40
+ features,
41
+ getRowId: (r: Row) => String(r.id),
42
+ rowHeight: 34,
43
+ containerHeight: 400,
44
+ virtualization: false,
45
+ onApiReady(api: SvGridApi<any, Row>) {
46
+ done = true
47
+ resolve({ api, destroy: () => { unmount(app); target.remove() } })
48
+ },
49
+ } as any,
50
+ })
51
+ queueMicrotask(() => { if (!done) reject(new Error('onApiReady never fired')) })
52
+ })
53
+ }
54
+
55
+ let cleanup: (() => void) | null = null
56
+ afterEach(() => { cleanup?.(); cleanup = null; vi.restoreAllMocks() })
57
+
58
+ describe('free grid export: exportCsv', () => {
59
+ it('serializes visible rows with faithful formatted values + a BOM header', async () => {
60
+ const { api, destroy } = await mountGrid()
61
+ cleanup = destroy
62
+ const csv = await api.exportCsv({ download: false })
63
+ const lines = csv.replace(/^/, '').split('\r\n')
64
+ expect(lines[0]).toBe('Company,Price,Date')
65
+ // Formatted as on screen; the comma in "ACME, Inc" AND in "$1,234.50" both
66
+ // force RFC-4180 quoting.
67
+ expect(lines[1]).toBe('"ACME, Inc","$1,234.50",2026-03-04')
68
+ expect(csv.startsWith('')).toBe(true)
69
+ })
70
+
71
+ it('rawValues:true writes the underlying numbers/strings', async () => {
72
+ const { api, destroy } = await mountGrid()
73
+ cleanup = destroy
74
+ const csv = await api.exportCsv({ download: false, rawValues: true, bom: false })
75
+ const lines = csv.split('\r\n')
76
+ expect(lines[1]).toBe('"ACME, Inc",1234.5,2026-03-04')
77
+ })
78
+
79
+ it('honors a column subset in the given order', async () => {
80
+ const { api, destroy } = await mountGrid()
81
+ cleanup = destroy
82
+ const csv = await api.exportCsv({ download: false, bom: false, columns: ['when', 'name'] })
83
+ expect(csv.split('\r\n')[0]).toBe('Date,Company')
84
+ })
85
+ })
86
+
87
+ describe('free grid export: exportTsv / exportJson', () => {
88
+ it('exportTsv is tab-delimited', async () => {
89
+ const { api, destroy } = await mountGrid()
90
+ cleanup = destroy
91
+ const tsv = await api.exportTsv({ download: false, bom: false })
92
+ expect(tsv.split('\r\n')[0]).toBe('Company\tPrice\tDate')
93
+ })
94
+
95
+ it('exportJson emits an array of formatted records', async () => {
96
+ const { api, destroy } = await mountGrid()
97
+ cleanup = destroy
98
+ const json = JSON.parse(await api.exportJson({ download: false }))
99
+ expect(json).toHaveLength(2)
100
+ expect(json[0]).toEqual({ name: 'ACME, Inc', price: '$1,234.50', when: '2026-03-04' })
101
+ })
102
+ })
103
+
104
+ describe('free grid export: copyToClipboard', () => {
105
+ it('writes TSV to the clipboard by default', async () => {
106
+ const writeText = vi.fn().mockResolvedValue(undefined)
107
+ vi.stubGlobal('navigator', { clipboard: { writeText } })
108
+ const { api, destroy } = await mountGrid()
109
+ cleanup = destroy
110
+ const text = await api.copyToClipboard()
111
+ expect(writeText).toHaveBeenCalledOnce()
112
+ expect(text.split('\r\n')[0]).toBe('Company\tPrice\tDate')
113
+ expect(text.startsWith('')).toBe(false) // no BOM on clipboard
114
+ })
115
+
116
+ it('can copy a Markdown table', async () => {
117
+ const writeText = vi.fn().mockResolvedValue(undefined)
118
+ vi.stubGlobal('navigator', { clipboard: { writeText } })
119
+ const { api, destroy } = await mountGrid()
120
+ cleanup = destroy
121
+ const md = await api.copyToClipboard({ format: 'markdown' })
122
+ const lines = md.split('\n')
123
+ expect(lines[0]).toBe('| Company | Price | Date |')
124
+ expect(lines[1]).toBe('| --- | ---: | --- |') // Price is right-aligned
125
+ })
126
+ })
@@ -0,0 +1,107 @@
1
+ import { describe, expect, it } from 'vitest'
2
+ import {
3
+ coerceExportDate,
4
+ formatValueForExport,
5
+ toExcelNumFmt,
6
+ valueForExcel,
7
+ } from './export-format'
8
+ import type { CellFormatConfig } from './core'
9
+
10
+ describe('formatValueForExport', () => {
11
+ it('returns "" for null / undefined', () => {
12
+ expect(formatValueForExport(null, undefined)).toBe('')
13
+ expect(formatValueForExport(undefined, { type: 'number' })).toBe('')
14
+ })
15
+
16
+ it('stringifies when there is no format', () => {
17
+ expect(formatValueForExport(42, undefined)).toBe('42')
18
+ expect(formatValueForExport('hi', undefined)).toBe('hi')
19
+ })
20
+
21
+ it('formats currency like the grid does', () => {
22
+ const f: CellFormatConfig = { type: 'currency', currency: 'USD', locales: 'en-US' }
23
+ expect(formatValueForExport(19.95, f)).toBe('$19.95')
24
+ })
25
+
26
+ it('formats a percent from 0..100 points', () => {
27
+ const f: CellFormatConfig = { type: 'percent', valueIsPercentPoints: true, locales: 'en-US' }
28
+ expect(formatValueForExport(42, f)).toBe('42%')
29
+ })
30
+
31
+ it('formats a number with grouping', () => {
32
+ const f: CellFormatConfig = {
33
+ type: 'number',
34
+ locales: 'en-US',
35
+ options: { minimumFractionDigits: 2 },
36
+ }
37
+ expect(formatValueForExport(1234567, f)).toBe('1,234,567.00')
38
+ })
39
+
40
+ it('formats a date with the y-m-d pattern', () => {
41
+ const f: CellFormatConfig = { type: 'date', pattern: 'y-m-d', locales: 'en-US' }
42
+ expect(formatValueForExport(new Date('2026-03-04T00:00:00Z'), f)).toMatch(/2026/)
43
+ })
44
+
45
+ it('falls back to String() when a date value is not parseable', () => {
46
+ const f: CellFormatConfig = { type: 'date' }
47
+ expect(formatValueForExport('not-a-date', f)).toBe('not-a-date')
48
+ })
49
+ })
50
+
51
+ describe('coerceExportDate', () => {
52
+ it('accepts Date, epoch ms, and ISO strings; rejects junk', () => {
53
+ expect(coerceExportDate(new Date('2020-01-01')) instanceof Date).toBe(true)
54
+ expect(coerceExportDate(1_600_000_000_000) instanceof Date).toBe(true)
55
+ expect(coerceExportDate('2021-06-15') instanceof Date).toBe(true)
56
+ expect(coerceExportDate('nope')).toBeNull()
57
+ expect(coerceExportDate(new Date('bad'))).toBeNull()
58
+ expect(coerceExportDate(null)).toBeNull()
59
+ })
60
+ })
61
+
62
+ describe('toExcelNumFmt', () => {
63
+ it('maps number/currency/percent/date to Excel codes', () => {
64
+ expect(toExcelNumFmt({ type: 'number' })).toBe('#,##0')
65
+ expect(toExcelNumFmt({ type: 'number', options: { maximumFractionDigits: 2 } })).toBe('#,##0.00')
66
+ expect(toExcelNumFmt({ type: 'currency', currency: 'USD' })).toBe('"$"#,##0.00')
67
+ expect(toExcelNumFmt({ type: 'currency', currency: 'EUR' })).toBe('"€"#,##0.00')
68
+ expect(toExcelNumFmt({ type: 'percent' })).toBe('0.00%')
69
+ expect(toExcelNumFmt({ type: 'date' })).toBe('yyyy-mm-dd')
70
+ expect(toExcelNumFmt({ type: 'datetime' })).toBe('yyyy-mm-dd hh:mm')
71
+ })
72
+
73
+ it('falls back to the ISO code for currencies without a glyph', () => {
74
+ expect(toExcelNumFmt({ type: 'currency', currency: 'CHF' })).toBe('"CHF"#,##0.00')
75
+ })
76
+
77
+ it('returns undefined without a format', () => {
78
+ expect(toExcelNumFmt(undefined)).toBeUndefined()
79
+ })
80
+ })
81
+
82
+ describe('valueForExcel', () => {
83
+ it('keeps numbers/currency numeric', () => {
84
+ expect(valueForExcel(19.95, { type: 'currency' })).toEqual({ ok: true, value: 19.95 })
85
+ expect(valueForExcel(1234, { type: 'number' })).toEqual({ ok: true, value: 1234 })
86
+ })
87
+
88
+ it('divides percent points to a fraction', () => {
89
+ expect(valueForExcel(42, { type: 'percent', valueIsPercentPoints: true })).toEqual({
90
+ ok: true,
91
+ value: 0.42,
92
+ })
93
+ expect(valueForExcel(0.42, { type: 'percent' })).toEqual({ ok: true, value: 0.42 })
94
+ })
95
+
96
+ it('returns real Dates for date formats', () => {
97
+ const r = valueForExcel('2026-03-04', { type: 'date' })
98
+ expect(r.ok).toBe(true)
99
+ if (r.ok) expect(r.value).toBeInstanceOf(Date)
100
+ })
101
+
102
+ it('reports not-ok for non-representable values', () => {
103
+ expect(valueForExcel('abc', { type: 'number' })).toEqual({ ok: false })
104
+ expect(valueForExcel(5, undefined)).toEqual({ ok: false })
105
+ expect(valueForExcel(null, { type: 'number' })).toEqual({ ok: false })
106
+ })
107
+ })