@carto/ps-react-ui 4.3.2 → 4.3.4

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 (299) hide show
  1. package/dist/components.js +3 -3
  2. package/dist/components.js.map +1 -1
  3. package/dist/{lasso-tool-BwRzEW7k.js → lasso-tool-wFqOD6wk.js} +13 -13
  4. package/dist/lasso-tool-wFqOD6wk.js.map +1 -0
  5. package/dist/types/components/common-types.d.ts +41 -0
  6. package/dist/types/components/types.d.ts +1 -1
  7. package/dist/types/widgets/toolbar-actions/toolbar-actions.d.ts +6 -3
  8. package/dist/widgets/actions.js +1 -1
  9. package/dist/widgets/bar.js +1 -1
  10. package/dist/widgets/category.js +1 -1
  11. package/dist/widgets/formula.js +1 -1
  12. package/dist/widgets/histogram.js +1 -1
  13. package/dist/widgets/markdown.js +1 -1
  14. package/dist/widgets/pie.js +1 -1
  15. package/dist/widgets/scatterplot.js +1 -1
  16. package/dist/widgets/spread.js +1 -1
  17. package/dist/widgets/table.js +1 -1
  18. package/dist/widgets/timeseries.js +1 -1
  19. package/dist/widgets/toolbar-actions.js +1343 -1332
  20. package/dist/widgets/toolbar-actions.js.map +1 -1
  21. package/dist/widgets/wrapper.js +1 -1
  22. package/package.json +8 -3
  23. package/src/components/basemaps/basemaps.test.tsx +196 -0
  24. package/src/components/basemaps/basemaps.tsx +128 -0
  25. package/src/components/basemaps/const.ts +13 -0
  26. package/src/components/basemaps/group-wrapper.test.tsx +38 -0
  27. package/src/components/basemaps/group-wrapper.tsx +28 -0
  28. package/src/components/basemaps/group.test.tsx +52 -0
  29. package/src/components/basemaps/group.tsx +42 -0
  30. package/src/components/basemaps/header.test.tsx +54 -0
  31. package/src/components/basemaps/header.tsx +36 -0
  32. package/src/components/basemaps/styles.ts +76 -0
  33. package/src/components/basemaps/types.ts +30 -0
  34. package/src/components/common-types.ts +1 -0
  35. package/src/components/geolocation-controls/const.ts +6 -0
  36. package/src/components/geolocation-controls/geolocation-controls.test.tsx +133 -0
  37. package/src/components/geolocation-controls/geolocation-controls.tsx +95 -0
  38. package/src/components/geolocation-controls/types.ts +17 -0
  39. package/src/components/index.ts +64 -0
  40. package/src/components/lasso-tool/chip.tsx +37 -0
  41. package/src/components/lasso-tool/const.tsx +70 -0
  42. package/src/components/lasso-tool/icons.tsx +91 -0
  43. package/src/components/lasso-tool/lasso-tool-inline.test.tsx +168 -0
  44. package/src/components/lasso-tool/lasso-tool-inline.tsx +245 -0
  45. package/src/components/lasso-tool/lasso-tool.test.tsx +212 -0
  46. package/src/components/lasso-tool/lasso-tool.tsx +479 -0
  47. package/src/components/lasso-tool/styles.ts +143 -0
  48. package/src/components/lasso-tool/types.ts +114 -0
  49. package/src/components/list-data/list-data-skeleton.test.tsx +10 -0
  50. package/src/components/list-data/list-data-skeleton.tsx +40 -0
  51. package/src/components/list-data/list-data.test.tsx +94 -0
  52. package/src/components/list-data/list-data.tsx +106 -0
  53. package/src/components/list-data/styles.ts +37 -0
  54. package/src/components/list-data/types.ts +25 -0
  55. package/src/components/measurement-tools/const.tsx +108 -0
  56. package/src/components/measurement-tools/icons.tsx +54 -0
  57. package/src/components/measurement-tools/measurement-tools.test.tsx +165 -0
  58. package/src/components/measurement-tools/measurement-tools.tsx +443 -0
  59. package/src/components/measurement-tools/styles.ts +91 -0
  60. package/src/components/measurement-tools/types.ts +77 -0
  61. package/src/components/no-data-alert/no-data-alert.test.tsx +31 -0
  62. package/src/components/no-data-alert/no-data-alert.tsx +59 -0
  63. package/src/components/responsive-drawer/responsive-drawer.test.tsx +91 -0
  64. package/src/components/responsive-drawer/responsive-drawer.tsx +53 -0
  65. package/src/components/smart-tooltip/smart-tooltip.test.tsx +168 -0
  66. package/src/components/smart-tooltip/smart-tooltip.tsx +40 -0
  67. package/src/components/tooltip/tooltip.test.tsx +86 -0
  68. package/src/components/tooltip/tooltip.tsx +30 -0
  69. package/src/components/types.ts +1 -0
  70. package/src/components/zoom-controls/styles.ts +27 -0
  71. package/src/components/zoom-controls/types.ts +19 -0
  72. package/src/components/zoom-controls/zoom-controls.test.tsx +101 -0
  73. package/src/components/zoom-controls/zoom-controls.tsx +114 -0
  74. package/src/hooks/index.ts +2 -0
  75. package/src/hooks/use-debounce.ts +55 -0
  76. package/src/hooks/use-skeleton.test.tsx +32 -0
  77. package/src/hooks/use-skeleton.ts +19 -0
  78. package/src/hooks/use-widget-ref.ts +33 -0
  79. package/src/widgets/README.md +277 -0
  80. package/src/widgets/_shared/chart-config/config-factory.ts +67 -0
  81. package/src/widgets/_shared/chart-config/csv-modifiers.ts +56 -0
  82. package/src/widgets/_shared/chart-config/index.ts +21 -0
  83. package/src/widgets/_shared/chart-config/option-builders.ts +203 -0
  84. package/src/widgets/_shared/skeleton/index.ts +5 -0
  85. package/src/widgets/_shared/skeleton/styles.ts +20 -0
  86. package/src/widgets/actions/change-column/change-column-icon.tsx +10 -0
  87. package/src/widgets/actions/change-column/change-column.test.tsx +163 -0
  88. package/src/widgets/actions/change-column/change-column.tsx +141 -0
  89. package/src/widgets/actions/change-column/sortable-column-item.tsx +49 -0
  90. package/src/widgets/actions/change-column/types.ts +20 -0
  91. package/src/widgets/actions/download/download.test.tsx +322 -0
  92. package/src/widgets/actions/download/download.tsx +118 -0
  93. package/src/widgets/actions/download/exports.test.tsx +275 -0
  94. package/src/widgets/actions/download/exports.tsx +103 -0
  95. package/src/widgets/actions/download/types.ts +21 -0
  96. package/src/widgets/actions/fullscreen/fullscreen.test.tsx +269 -0
  97. package/src/widgets/actions/fullscreen/fullscreen.tsx +82 -0
  98. package/src/widgets/actions/fullscreen/styles.ts +17 -0
  99. package/src/widgets/actions/fullscreen/types.ts +27 -0
  100. package/src/widgets/actions/index.ts +51 -0
  101. package/src/widgets/actions/lock-selection/lock-selection.test.tsx +186 -0
  102. package/src/widgets/actions/lock-selection/lock-selection.tsx +133 -0
  103. package/src/widgets/actions/lock-selection/types.ts +41 -0
  104. package/src/widgets/actions/relative-data/relative-data.test.tsx +267 -0
  105. package/src/widgets/actions/relative-data/relative-data.tsx +133 -0
  106. package/src/widgets/actions/relative-data/style.ts +9 -0
  107. package/src/widgets/actions/relative-data/types.ts +31 -0
  108. package/src/widgets/actions/relative-data/utils.test.ts +223 -0
  109. package/src/widgets/actions/relative-data/utils.ts +58 -0
  110. package/src/widgets/actions/searcher/searcher-toggle.test.tsx +354 -0
  111. package/src/widgets/actions/searcher/searcher-toggle.tsx +73 -0
  112. package/src/widgets/actions/searcher/searcher.tsx +205 -0
  113. package/src/widgets/actions/searcher/types.ts +72 -0
  114. package/src/widgets/actions/shared/styles.ts +12 -0
  115. package/src/widgets/actions/stack-toggle/grouped-bar-chart-icon.tsx +14 -0
  116. package/src/widgets/actions/stack-toggle/stack-toggle.test.tsx +270 -0
  117. package/src/widgets/actions/stack-toggle/stack-toggle.tsx +146 -0
  118. package/src/widgets/actions/stack-toggle/types.ts +29 -0
  119. package/src/widgets/actions/zoom-toggle/index.ts +2 -0
  120. package/src/widgets/actions/zoom-toggle/style.ts +14 -0
  121. package/src/widgets/actions/zoom-toggle/types.ts +44 -0
  122. package/src/widgets/actions/zoom-toggle/zoom-toggle.tsx +186 -0
  123. package/src/widgets/bar/config.ts +122 -0
  124. package/src/widgets/bar/index.ts +9 -0
  125. package/src/widgets/bar/skeleton.tsx +60 -0
  126. package/src/widgets/bar/style.ts +33 -0
  127. package/src/widgets/bar/types.ts +16 -0
  128. package/src/widgets/category/category-ui.test.tsx +399 -0
  129. package/src/widgets/category/category-ui.tsx +156 -0
  130. package/src/widgets/category/components/category-bar.tsx +28 -0
  131. package/src/widgets/category/components/category-legend.tsx +30 -0
  132. package/src/widgets/category/components/category-row-multi.tsx +50 -0
  133. package/src/widgets/category/components/category-row-other.tsx +23 -0
  134. package/src/widgets/category/components/category-row-single.tsx +47 -0
  135. package/src/widgets/category/components/index.ts +14 -0
  136. package/src/widgets/category/config.ts +85 -0
  137. package/src/widgets/category/index.ts +30 -0
  138. package/src/widgets/category/skeleton.tsx +24 -0
  139. package/src/widgets/category/style.ts +133 -0
  140. package/src/widgets/category/types.ts +40 -0
  141. package/src/widgets/echart/const.ts +1 -0
  142. package/src/widgets/echart/echart-ui.test.tsx +519 -0
  143. package/src/widgets/echart/echart-ui.tsx +80 -0
  144. package/src/widgets/echart/echart.test.tsx +537 -0
  145. package/src/widgets/echart/echart.tsx +60 -0
  146. package/src/widgets/echart/index.ts +16 -0
  147. package/src/widgets/echart/options.ts +53 -0
  148. package/src/widgets/echart/types.ts +41 -0
  149. package/src/widgets/echart/utils.ts +169 -0
  150. package/src/widgets/error/error.test.tsx +331 -0
  151. package/src/widgets/error/error.tsx +40 -0
  152. package/src/widgets/error/index.ts +2 -0
  153. package/src/widgets/error/types.ts +14 -0
  154. package/src/widgets/formula/components/item.test.tsx +249 -0
  155. package/src/widgets/formula/components/item.tsx +18 -0
  156. package/src/widgets/formula/components/prefix.test.tsx +341 -0
  157. package/src/widgets/formula/components/prefix.tsx +18 -0
  158. package/src/widgets/formula/components/row.test.tsx +364 -0
  159. package/src/widgets/formula/components/row.tsx +21 -0
  160. package/src/widgets/formula/components/series.tsx +34 -0
  161. package/src/widgets/formula/components/suffix.test.tsx +383 -0
  162. package/src/widgets/formula/components/suffix.tsx +28 -0
  163. package/src/widgets/formula/components/value.test.tsx +329 -0
  164. package/src/widgets/formula/components/value.tsx +29 -0
  165. package/src/widgets/formula/config.ts +27 -0
  166. package/src/widgets/formula/formula-ui.test.tsx +399 -0
  167. package/src/widgets/formula/formula-ui.tsx +27 -0
  168. package/src/widgets/formula/index.ts +24 -0
  169. package/src/widgets/formula/serializer.test.tsx +144 -0
  170. package/src/widgets/formula/serializer.ts +28 -0
  171. package/src/widgets/formula/skeleton.tsx +10 -0
  172. package/src/widgets/formula/style.ts +23 -0
  173. package/src/widgets/formula/types.ts +50 -0
  174. package/src/widgets/histogram/config.ts +143 -0
  175. package/src/widgets/histogram/index.ts +8 -0
  176. package/src/widgets/histogram/skeleton.tsx +52 -0
  177. package/src/widgets/histogram/style.ts +8 -0
  178. package/src/widgets/histogram/types.ts +17 -0
  179. package/src/widgets/index.ts +25 -0
  180. package/src/widgets/loader/index.ts +4 -0
  181. package/src/widgets/loader/loader.tsx +70 -0
  182. package/src/widgets/loader/types.ts +11 -0
  183. package/src/widgets/loader/utils.test.ts +112 -0
  184. package/src/widgets/loader/utils.ts +35 -0
  185. package/src/widgets/markdown/config.ts +18 -0
  186. package/src/widgets/markdown/index.ts +14 -0
  187. package/src/widgets/markdown/markdown-ui.test.tsx +341 -0
  188. package/src/widgets/markdown/markdown-ui.tsx +52 -0
  189. package/src/widgets/markdown/markdown.tsx +20 -0
  190. package/src/widgets/markdown/skeleton.tsx +12 -0
  191. package/src/widgets/markdown/style.ts +28 -0
  192. package/src/widgets/markdown/types.ts +28 -0
  193. package/src/widgets/no-data/index.ts +2 -0
  194. package/src/widgets/no-data/no-data.test.tsx +447 -0
  195. package/src/widgets/no-data/no-data.tsx +116 -0
  196. package/src/widgets/no-data/style.ts +18 -0
  197. package/src/widgets/no-data/types.ts +72 -0
  198. package/src/widgets/note/index.ts +2 -0
  199. package/src/widgets/note/note.test.tsx +391 -0
  200. package/src/widgets/note/note.tsx +114 -0
  201. package/src/widgets/note/style.ts +29 -0
  202. package/src/widgets/note/types.ts +9 -0
  203. package/src/widgets/pie/config.ts +177 -0
  204. package/src/widgets/pie/index.ts +8 -0
  205. package/src/widgets/pie/skeleton.tsx +70 -0
  206. package/src/widgets/pie/style.ts +8 -0
  207. package/src/widgets/pie/types.ts +16 -0
  208. package/src/widgets/range/components/range-item.tsx +213 -0
  209. package/src/widgets/range/config.ts +10 -0
  210. package/src/widgets/range/index.ts +16 -0
  211. package/src/widgets/range/range-ui.test.tsx +203 -0
  212. package/src/widgets/range/range-ui.tsx +11 -0
  213. package/src/widgets/range/serializer.test.ts +70 -0
  214. package/src/widgets/range/serializer.ts +27 -0
  215. package/src/widgets/range/skeleton.tsx +14 -0
  216. package/src/widgets/range/style.ts +37 -0
  217. package/src/widgets/range/types.ts +39 -0
  218. package/src/widgets/scatterplot/config.ts +138 -0
  219. package/src/widgets/scatterplot/index.ts +8 -0
  220. package/src/widgets/scatterplot/skeleton.tsx +59 -0
  221. package/src/widgets/scatterplot/style.ts +21 -0
  222. package/src/widgets/scatterplot/types.ts +17 -0
  223. package/src/widgets/selection-summary/index.ts +6 -0
  224. package/src/widgets/selection-summary/selection-summary.tsx +46 -0
  225. package/src/widgets/selection-summary/style.ts +10 -0
  226. package/src/widgets/selection-summary/types.ts +14 -0
  227. package/src/widgets/skeleton-loader/index.ts +2 -0
  228. package/src/widgets/skeleton-loader/skeleton-loader.test.tsx +139 -0
  229. package/src/widgets/skeleton-loader/skeleton-loader.tsx +28 -0
  230. package/src/widgets/skeleton-loader/types.ts +8 -0
  231. package/src/widgets/spread/components/max-value.tsx +29 -0
  232. package/src/widgets/spread/components/min-value.tsx +29 -0
  233. package/src/widgets/spread/components/separator.tsx +6 -0
  234. package/src/widgets/spread/config.ts +34 -0
  235. package/src/widgets/spread/index.ts +23 -0
  236. package/src/widgets/spread/skeleton.tsx +10 -0
  237. package/src/widgets/spread/spread-ui.test.tsx +368 -0
  238. package/src/widgets/spread/spread-ui.tsx +29 -0
  239. package/src/widgets/spread/style.ts +22 -0
  240. package/src/widgets/spread/types.ts +47 -0
  241. package/src/widgets/stores/index.ts +9 -0
  242. package/src/widgets/stores/types.ts +192 -0
  243. package/src/widgets/stores/widget-store.test.ts +601 -0
  244. package/src/widgets/stores/widget-store.ts +239 -0
  245. package/src/widgets/subheader/index.ts +3 -0
  246. package/src/widgets/subheader/style.ts +20 -0
  247. package/src/widgets/subheader/subheader.test.tsx +45 -0
  248. package/src/widgets/subheader/subheader.tsx +16 -0
  249. package/src/widgets/subheader/types.ts +11 -0
  250. package/src/widgets/table/components/cell-header.tsx +58 -0
  251. package/src/widgets/table/components/cell.tsx +80 -0
  252. package/src/widgets/table/components/index.ts +4 -0
  253. package/src/widgets/table/components/pagination-actions.tsx +67 -0
  254. package/src/widgets/table/components/pagination.tsx +41 -0
  255. package/src/widgets/table/components/row.tsx +60 -0
  256. package/src/widgets/table/config.ts +71 -0
  257. package/src/widgets/table/helpers.test.ts +244 -0
  258. package/src/widgets/table/helpers.ts +107 -0
  259. package/src/widgets/table/hooks/index.ts +7 -0
  260. package/src/widgets/table/hooks/use-pagination.test.ts +294 -0
  261. package/src/widgets/table/hooks/use-pagination.ts +155 -0
  262. package/src/widgets/table/hooks/use-selection.test.ts +504 -0
  263. package/src/widgets/table/hooks/use-selection.ts +189 -0
  264. package/src/widgets/table/hooks/use-sort.test.ts +296 -0
  265. package/src/widgets/table/hooks/use-sort.ts +138 -0
  266. package/src/widgets/table/index.ts +53 -0
  267. package/src/widgets/table/serializer.ts +54 -0
  268. package/src/widgets/table/skeleton.tsx +48 -0
  269. package/src/widgets/table/style.ts +34 -0
  270. package/src/widgets/table/table-ui.tsx +64 -0
  271. package/src/widgets/table/types.ts +223 -0
  272. package/src/widgets/timeseries/config.ts +135 -0
  273. package/src/widgets/timeseries/index.ts +8 -0
  274. package/src/widgets/timeseries/skeleton.tsx +55 -0
  275. package/src/widgets/timeseries/style.ts +36 -0
  276. package/src/widgets/timeseries/types.ts +17 -0
  277. package/src/widgets/toolbar-actions/index.ts +6 -0
  278. package/src/widgets/toolbar-actions/styles.ts +38 -0
  279. package/src/widgets/toolbar-actions/toolbar-actions.test.tsx +691 -0
  280. package/src/widgets/toolbar-actions/toolbar-actions.tsx +145 -0
  281. package/src/widgets/toolbar-actions/types.ts +60 -0
  282. package/src/widgets/wrapper/components/actions.test.tsx +101 -0
  283. package/src/widgets/wrapper/components/actions.tsx +30 -0
  284. package/src/widgets/wrapper/components/options.test.tsx +323 -0
  285. package/src/widgets/wrapper/components/options.tsx +73 -0
  286. package/src/widgets/wrapper/components/title.test.tsx +126 -0
  287. package/src/widgets/wrapper/components/title.tsx +32 -0
  288. package/src/widgets/wrapper/index.ts +16 -0
  289. package/src/widgets/wrapper/styles.ts +98 -0
  290. package/src/widgets/wrapper/types.ts +55 -0
  291. package/src/widgets/wrapper/wrapper-ui.test.tsx +232 -0
  292. package/src/widgets/wrapper/wrapper-ui.tsx +57 -0
  293. package/src/widgets/wrapper/wrapper.test.tsx +365 -0
  294. package/src/widgets/wrapper/wrapper.tsx +50 -0
  295. package/dist/lasso-tool-BwRzEW7k.js.map +0 -1
  296. package/dist/types/common/common.d.ts +0 -3
  297. package/dist/types/common/index.d.ts +0 -26
  298. package/dist/types/common/lasso-tools.d.ts +0 -36
  299. package/dist/types/common/measurement-tools.d.ts +0 -65
@@ -0,0 +1,294 @@
1
+ import { describe, it, expect, beforeEach } from 'vitest'
2
+ import { renderHook, act } from '@testing-library/react'
3
+ import { usePagination } from './use-pagination'
4
+ import { useWidgetStore } from '../../stores/widget-store'
5
+ import type { TableRow } from '../types'
6
+
7
+ describe('usePagination', () => {
8
+ const createTestData = (count: number): TableRow[] =>
9
+ Array.from({ length: count }, (_, i) => ({
10
+ id: i + 1,
11
+ name: `Item ${i + 1}`,
12
+ }))
13
+
14
+ // Clear widget store before each test
15
+ beforeEach(() => {
16
+ useWidgetStore.getState().clearWidgets()
17
+ })
18
+
19
+ describe('local mode', () => {
20
+ it('should initialize with default values', () => {
21
+ const data = createTestData(25)
22
+ const { result } = renderHook(() => usePagination('test-widget', data))
23
+
24
+ expect(result.current.page).toBe(0)
25
+ expect(result.current.rowsPerPage).toBe(10)
26
+ expect(result.current.total).toBe(25)
27
+ expect(result.current.rowsPerPageOptions).toEqual([5, 10, 25, 50])
28
+ })
29
+
30
+ it('should initialize with custom values', () => {
31
+ useWidgetStore.getState().setWidget('test-widget', {
32
+ pagination: {
33
+ page: 0,
34
+ rowsPerPage: 10,
35
+ rowsPerPageOptions: [10, 25, 50, 100],
36
+ },
37
+ })
38
+ const data = createTestData(100)
39
+ const { result } = renderHook(() => usePagination('test-widget', data))
40
+
41
+ expect(result.current.page).toBe(0)
42
+ expect(result.current.rowsPerPage).toBe(10)
43
+ expect(result.current.rowsPerPageOptions).toEqual([10, 25, 50, 100])
44
+ })
45
+
46
+ it('should paginate data correctly', () => {
47
+ useWidgetStore.getState().setWidget('test-widget', {
48
+ pagination: {
49
+ page: 0,
50
+ rowsPerPage: 10,
51
+ },
52
+ })
53
+ const data = createTestData(25)
54
+ const { result } = renderHook(() => usePagination('test-widget', data))
55
+
56
+ expect(result.current.paginatedData).toHaveLength(10)
57
+ expect(result.current.paginatedData[0]?.id).toBe(1)
58
+ expect(result.current.paginatedData[9]?.id).toBe(10)
59
+ })
60
+
61
+ it('should change page', () => {
62
+ const data = createTestData(25)
63
+ const { result } = renderHook(() => usePagination('test-widget', data))
64
+
65
+ act(() => {
66
+ result.current.setPage(1)
67
+ })
68
+
69
+ expect(result.current.page).toBe(1)
70
+ expect(result.current.paginatedData[0]?.id).toBe(11)
71
+ })
72
+
73
+ it('should bound page to max page', () => {
74
+ const data = createTestData(25)
75
+ const { result } = renderHook(() => usePagination('test-widget', data))
76
+
77
+ act(() => {
78
+ result.current.setPage(100)
79
+ })
80
+
81
+ // Max page for 25 items with 10 per page is 2 (pages 0, 1, 2)
82
+ expect(result.current.page).toBe(2)
83
+ })
84
+
85
+ it('should bound page to 0 for negative values', () => {
86
+ const data = createTestData(25)
87
+ const { result } = renderHook(() => usePagination('test-widget', data))
88
+
89
+ act(() => {
90
+ result.current.setPage(-5)
91
+ })
92
+
93
+ expect(result.current.page).toBe(0)
94
+ })
95
+
96
+ it('should change rows per page and reset to first page', () => {
97
+ const data = createTestData(50)
98
+ const { result } = renderHook(() => usePagination('test-widget', data))
99
+
100
+ // First navigate to page 2
101
+ act(() => {
102
+ result.current.setPage(2)
103
+ })
104
+
105
+ expect(result.current.page).toBe(2)
106
+
107
+ act(() => {
108
+ result.current.setRowsPerPage(25)
109
+ })
110
+
111
+ expect(result.current.rowsPerPage).toBe(25)
112
+ expect(result.current.page).toBe(0)
113
+ })
114
+
115
+ it('should navigate to first page', () => {
116
+ const data = createTestData(50)
117
+ const { result } = renderHook(() => usePagination('test-widget', data))
118
+
119
+ // First navigate to page 3
120
+ act(() => {
121
+ result.current.setPage(3)
122
+ })
123
+
124
+ act(() => {
125
+ result.current.goToFirstPage()
126
+ })
127
+
128
+ expect(result.current.page).toBe(0)
129
+ })
130
+
131
+ it('should navigate to last page', () => {
132
+ const data = createTestData(50)
133
+ const { result } = renderHook(() => usePagination('test-widget', data))
134
+
135
+ act(() => {
136
+ result.current.goToLastPage()
137
+ })
138
+
139
+ // 50 items / 10 per page = 5 pages (0-4)
140
+ expect(result.current.page).toBe(4)
141
+ })
142
+
143
+ it('should navigate to next page', () => {
144
+ const data = createTestData(50)
145
+ const { result } = renderHook(() => usePagination('test-widget', data))
146
+
147
+ act(() => {
148
+ result.current.goToNextPage()
149
+ })
150
+
151
+ expect(result.current.page).toBe(1)
152
+ })
153
+
154
+ it('should navigate to previous page', () => {
155
+ const data = createTestData(50)
156
+ const { result } = renderHook(() => usePagination('test-widget', data))
157
+
158
+ // First navigate to page 2
159
+ act(() => {
160
+ result.current.setPage(2)
161
+ })
162
+
163
+ act(() => {
164
+ result.current.goToPreviousPage()
165
+ })
166
+
167
+ expect(result.current.page).toBe(1)
168
+ })
169
+
170
+ it('should handle empty data', () => {
171
+ const { result } = renderHook(() => usePagination('test-widget', []))
172
+
173
+ expect(result.current.total).toBe(0)
174
+ expect(result.current.paginatedData).toEqual([])
175
+ })
176
+
177
+ it('should persist state in widget store', () => {
178
+ const data = createTestData(50)
179
+ const { result, unmount } = renderHook(() =>
180
+ usePagination('test-widget', data),
181
+ )
182
+
183
+ // Change page
184
+ act(() => {
185
+ result.current.setPage(2)
186
+ })
187
+
188
+ expect(result.current.page).toBe(2)
189
+
190
+ // Unmount and remount - state should persist
191
+ unmount()
192
+
193
+ const { result: newResult } = renderHook(() =>
194
+ usePagination('test-widget', data),
195
+ )
196
+
197
+ expect(newResult.current.page).toBe(2)
198
+ })
199
+ })
200
+
201
+ describe('remote mode', () => {
202
+ it('should use remote total', () => {
203
+ useWidgetStore.getState().setWidget('test-widget', {
204
+ mode: 'remote',
205
+ pagination: {
206
+ page: 0,
207
+ rowsPerPage: 10,
208
+ rowsPerPageOptions: [10],
209
+ total: 100,
210
+ },
211
+ })
212
+ const data = createTestData(10)
213
+ const { result } = renderHook(() => usePagination('test-widget', data))
214
+
215
+ expect(result.current.total).toBe(100)
216
+ // In remote mode, data is already paginated from server
217
+ expect(result.current.paginatedData).toHaveLength(10)
218
+ })
219
+
220
+ it('should update store state when page changes in remote mode', () => {
221
+ useWidgetStore.getState().setWidget('test-widget', {
222
+ mode: 'remote',
223
+ pagination: {
224
+ page: 0,
225
+ rowsPerPage: 10,
226
+ rowsPerPageOptions: [10],
227
+ total: 100,
228
+ },
229
+ })
230
+ const data = createTestData(10)
231
+ const { result } = renderHook(() => usePagination('test-widget', data))
232
+
233
+ act(() => {
234
+ result.current.setPage(2)
235
+ })
236
+
237
+ expect(result.current.page).toBe(2)
238
+ })
239
+
240
+ it('should update store state when rows per page changes in remote mode', () => {
241
+ useWidgetStore.getState().setWidget('test-widget', {
242
+ mode: 'remote',
243
+ pagination: {
244
+ page: 0,
245
+ rowsPerPage: 10,
246
+ rowsPerPageOptions: [10],
247
+ total: 100,
248
+ },
249
+ })
250
+ const data = createTestData(10)
251
+ const { result } = renderHook(() => usePagination('test-widget', data))
252
+
253
+ act(() => {
254
+ result.current.setRowsPerPage(25)
255
+ })
256
+
257
+ expect(result.current.rowsPerPage).toBe(25)
258
+ expect(result.current.page).toBe(0)
259
+ })
260
+
261
+ it('should handle undefined remote total', () => {
262
+ useWidgetStore.getState().setWidget('test-widget', {
263
+ mode: 'remote',
264
+ pagination: {
265
+ page: 0,
266
+ rowsPerPage: 10,
267
+ rowsPerPageOptions: [10],
268
+ },
269
+ })
270
+ const data = createTestData(10)
271
+ const { result } = renderHook(() => usePagination('test-widget', data))
272
+
273
+ expect(result.current.total).toBe(10)
274
+ })
275
+
276
+ it('should return data as-is in remote mode', () => {
277
+ useWidgetStore.getState().setWidget('test-widget', {
278
+ mode: 'remote',
279
+ pagination: {
280
+ page: 0,
281
+ rowsPerPage: 10,
282
+ rowsPerPageOptions: [10],
283
+ total: 100,
284
+ },
285
+ })
286
+ const data = createTestData(5)
287
+ const { result } = renderHook(() => usePagination('test-widget', data))
288
+
289
+ // In remote mode, paginatedData should return the data as-is
290
+ // since server already handles pagination
291
+ expect(result.current.paginatedData).toEqual(data)
292
+ })
293
+ })
294
+ })
@@ -0,0 +1,155 @@
1
+ import { useCallback, useMemo } from 'react'
2
+ import type { TableRow, TableWidgetState } from '../types'
3
+ import { paginateData } from '../helpers'
4
+ import { useWidgetStore } from '../../stores/widget-store'
5
+ import { useShallow } from 'zustand/shallow'
6
+ import {
7
+ DEFAULT_PAGE,
8
+ DEFAULT_ROWS_PER_PAGE,
9
+ DEFAULT_ROWS_PER_PAGE_OPTIONS,
10
+ } from '../config'
11
+
12
+ export interface UsePaginationResult<T> {
13
+ /** Whether pagination is enabled */
14
+ paginationEnabled: boolean
15
+ /** Current page (0-indexed) */
16
+ page: number
17
+ /** Rows per page */
18
+ rowsPerPage: number
19
+ /** Total row count */
20
+ total: number
21
+ /** Available rows per page options */
22
+ rowsPerPageOptions: number[]
23
+ /** Paginated data (for local mode) */
24
+ paginatedData: T[]
25
+ /** Set current page */
26
+ setPage: (page: number) => void
27
+ /** Set rows per page */
28
+ setRowsPerPage: (rowsPerPage: number) => void
29
+ /** Go to first page */
30
+ goToFirstPage: () => void
31
+ /** Go to last page */
32
+ goToLastPage: () => void
33
+ /** Go to next page */
34
+ goToNextPage: () => void
35
+ /** Go to previous page */
36
+ goToPreviousPage: () => void
37
+ }
38
+
39
+ /**
40
+ * Hook for managing table pagination
41
+ * Supports both local (client-side) and remote (server-side) pagination
42
+ * State is persisted in the widget store for the given widgetId
43
+ */
44
+ export function usePagination<T extends TableRow>(
45
+ widgetId: string,
46
+ data: T[],
47
+ ): UsePaginationResult<T> {
48
+ // Get store actions and state
49
+ const setWidget = useWidgetStore((state) => state.setWidget)
50
+ const getWidget = useWidgetStore((state) => state.getWidget)
51
+ const paginationState = useWidgetStore(
52
+ useShallow((state) => {
53
+ const widget = state.widgets[widgetId] as TableWidgetState | undefined
54
+ return {
55
+ paginationEnabled: !!widget?.pagination,
56
+ page: widget?.pagination?.page ?? DEFAULT_PAGE,
57
+ rowsPerPage: widget?.pagination?.rowsPerPage ?? DEFAULT_ROWS_PER_PAGE,
58
+ rowsPerPageOptions:
59
+ widget?.pagination?.rowsPerPageOptions ??
60
+ DEFAULT_ROWS_PER_PAGE_OPTIONS,
61
+ total: widget?.pagination?.total ?? data.length,
62
+ }
63
+ }),
64
+ )
65
+
66
+ const mode = useWidgetStore(
67
+ useShallow((state) => {
68
+ const widget = state.widgets[widgetId] as TableWidgetState | undefined
69
+ return widget?.mode
70
+ }),
71
+ )
72
+
73
+ // Calculate max page
74
+ const maxPage = Math.max(
75
+ 0,
76
+ Math.ceil(paginationState.total / (paginationState?.rowsPerPage ?? 1)) - 1,
77
+ )
78
+
79
+ // Set page with bounds checking
80
+ const setPage = useCallback(
81
+ (newPage: number) => {
82
+ // Read current state directly from store to avoid stale closures
83
+ const widget = getWidget<TableWidgetState>(widgetId)
84
+ const currentTotal = widget?.pagination?.total ?? data.length
85
+ const currentRowsPerPage =
86
+ widget?.pagination?.rowsPerPage ?? DEFAULT_ROWS_PER_PAGE
87
+ const currentMaxPage = Math.max(
88
+ 0,
89
+ Math.ceil(currentTotal / currentRowsPerPage) - 1,
90
+ )
91
+ const boundedPage = Math.max(0, Math.min(newPage, currentMaxPage))
92
+
93
+ setWidget<TableWidgetState>(widgetId, {
94
+ pagination: {
95
+ ...widget?.pagination,
96
+ page: boundedPage,
97
+ },
98
+ })
99
+ },
100
+ [data.length, getWidget, setWidget, widgetId],
101
+ )
102
+
103
+ // Set rows per page and reset to first page
104
+ const setRowsPerPage = useCallback(
105
+ (newRowsPerPage: number) => {
106
+ const widget = getWidget<TableWidgetState>(widgetId)
107
+ setWidget<TableWidgetState>(widgetId, {
108
+ pagination: {
109
+ ...widget?.pagination,
110
+ rowsPerPage: newRowsPerPage,
111
+ page: 0, // Reset to first page when changing rows per page
112
+ },
113
+ })
114
+ },
115
+ [getWidget, setWidget, widgetId],
116
+ )
117
+
118
+ // Navigation helpers
119
+ const goToFirstPage = useCallback(() => setPage(0), [setPage])
120
+ const goToLastPage = useCallback(() => setPage(maxPage), [setPage, maxPage])
121
+ const goToNextPage = useCallback(
122
+ () => setPage(paginationState.page + 1),
123
+ [setPage, paginationState.page],
124
+ )
125
+ const goToPreviousPage = useCallback(
126
+ () => setPage(paginationState.page - 1),
127
+ [setPage, paginationState.page],
128
+ )
129
+
130
+ // Paginated data for local mode
131
+ const paginatedData = useMemo(() => {
132
+ if (mode === 'remote' || !paginationState.paginationEnabled) {
133
+ // For remote mode, data is already paginated from server
134
+ return data
135
+ }
136
+ return paginateData(data, paginationState.page, paginationState.rowsPerPage)
137
+ }, [
138
+ data,
139
+ mode,
140
+ paginationState.page,
141
+ paginationState.paginationEnabled,
142
+ paginationState.rowsPerPage,
143
+ ])
144
+
145
+ return {
146
+ ...paginationState,
147
+ paginatedData,
148
+ setPage,
149
+ setRowsPerPage,
150
+ goToFirstPage,
151
+ goToLastPage,
152
+ goToNextPage,
153
+ goToPreviousPage,
154
+ }
155
+ }