@object-ui/react 2.0.0 → 3.0.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.
- package/.turbo/turbo-build.log +1 -1
- package/CHANGELOG.md +28 -0
- package/dist/SchemaRenderer.d.ts +20 -1
- package/dist/SchemaRenderer.js +76 -13
- package/dist/context/DndContext.d.ts +135 -0
- package/dist/context/DndContext.js +120 -0
- package/dist/context/NotificationContext.d.ts +113 -0
- package/dist/context/NotificationContext.js +130 -0
- package/dist/context/index.d.ts +2 -0
- package/dist/context/index.js +2 -0
- package/dist/hooks/index.d.ts +17 -0
- package/dist/hooks/index.js +17 -0
- package/dist/hooks/useAnimation.d.ts +46 -0
- package/dist/hooks/useAnimation.js +59 -0
- package/dist/hooks/useClientNotifications.d.ts +53 -0
- package/dist/hooks/useClientNotifications.js +190 -0
- package/dist/hooks/useColumnSummary.d.ts +38 -0
- package/dist/hooks/useColumnSummary.js +54 -0
- package/dist/hooks/useCrudShortcuts.d.ts +39 -0
- package/dist/hooks/useCrudShortcuts.js +96 -0
- package/dist/hooks/useDensityMode.d.ts +48 -0
- package/dist/hooks/useDensityMode.js +60 -0
- package/dist/hooks/useETagCache.d.ts +75 -0
- package/dist/hooks/useETagCache.js +306 -0
- package/dist/hooks/useFocusManagement.d.ts +89 -0
- package/dist/hooks/useFocusManagement.js +188 -0
- package/dist/hooks/useFocusTrap.d.ts +41 -0
- package/dist/hooks/useFocusTrap.js +103 -0
- package/dist/hooks/useKeyboardShortcuts.d.ts +49 -0
- package/dist/hooks/useKeyboardShortcuts.js +86 -0
- package/dist/hooks/useOffline.d.ts +96 -0
- package/dist/hooks/useOffline.js +185 -0
- package/dist/hooks/usePageTransition.d.ts +60 -0
- package/dist/hooks/usePageTransition.js +111 -0
- package/dist/hooks/usePerformance.d.ts +87 -0
- package/dist/hooks/usePerformance.js +165 -0
- package/dist/hooks/usePerformanceBudget.d.ts +78 -0
- package/dist/hooks/usePerformanceBudget.js +209 -0
- package/dist/hooks/useReducedMotion.d.ts +28 -0
- package/dist/hooks/useReducedMotion.js +48 -0
- package/dist/hooks/useSchemaPersistence.d.ts +87 -0
- package/dist/hooks/useSchemaPersistence.js +173 -0
- package/dist/hooks/useViewSharing.d.ts +57 -0
- package/dist/hooks/useViewSharing.js +74 -0
- package/dist/hooks/useViewTransition.d.ts +54 -0
- package/dist/hooks/useViewTransition.js +83 -0
- package/package.json +7 -7
- package/src/SchemaRenderer.tsx +122 -13
- package/src/__tests__/SchemaRenderer.aria.test.tsx +118 -0
- package/src/__tests__/SchemaRenderer.errorBoundary.test.tsx +114 -0
- package/src/__tests__/SchemaRendererProvider.smoke.test.tsx +132 -0
- package/src/__tests__/useOffline.test.ts +198 -0
- package/src/__tests__/usePageTransition.test.ts +214 -0
- package/src/__tests__/usePerformance.test.ts +137 -0
- package/src/__tests__/view-switching.test.tsx +456 -0
- package/src/context/DndContext.tsx +260 -0
- package/src/context/NotificationContext.tsx +280 -0
- package/src/context/__tests__/DndContext.test.tsx +150 -0
- package/src/context/__tests__/NotificationContext.test.tsx +176 -0
- package/src/context/index.ts +2 -0
- package/src/hooks/__tests__/useAnimation.test.ts +82 -0
- package/src/hooks/__tests__/useColumnSummary.test.ts +93 -0
- package/src/hooks/__tests__/useDensityMode.test.ts +81 -0
- package/src/hooks/__tests__/useETagCache.test.ts +153 -0
- package/src/hooks/__tests__/useFocusTrap.test.ts +47 -0
- package/src/hooks/__tests__/useKeyboardShortcuts.test.ts +144 -0
- package/src/hooks/__tests__/usePerformanceBudget.test.ts +157 -0
- package/src/hooks/__tests__/useSchemaPersistence.test.ts +220 -0
- package/src/hooks/__tests__/useViewSharing.test.ts +121 -0
- package/src/hooks/__tests__/useViewTransition.test.ts +61 -0
- package/src/hooks/index.ts +17 -1
- package/src/hooks/useAnimation.ts +117 -0
- package/src/hooks/useClientNotifications.ts +251 -0
- package/src/hooks/useColumnSummary.ts +87 -0
- package/src/hooks/useCrudShortcuts.ts +126 -0
- package/src/hooks/useDensityMode.ts +102 -0
- package/src/hooks/useETagCache.ts +404 -0
- package/src/hooks/useFocusManagement.ts +248 -0
- package/src/hooks/useFocusTrap.ts +138 -0
- package/src/hooks/useKeyboardShortcuts.ts +119 -0
- package/src/hooks/useOffline.ts +301 -0
- package/src/hooks/usePageTransition.ts +187 -0
- package/src/hooks/usePerformance.ts +246 -0
- package/src/hooks/usePerformanceBudget.ts +313 -0
- package/src/hooks/useReducedMotion.ts +53 -0
- package/src/hooks/useSchemaPersistence.ts +222 -0
- package/src/hooks/useViewSharing.ts +140 -0
- package/src/hooks/useViewTransition.ts +144 -0
- package/tsconfig.tsbuildinfo +1 -1
|
@@ -0,0 +1,456 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ObjectUI — View Switching State Preservation Tests
|
|
3
|
+
* Copyright (c) 2024-present ObjectStack Inc.
|
|
4
|
+
*
|
|
5
|
+
* This source code is licensed under the MIT license found in the
|
|
6
|
+
* LICENSE file in the root directory of this source tree.
|
|
7
|
+
*
|
|
8
|
+
* Tests that switching between grid ↔ kanban ↔ calendar views preserves
|
|
9
|
+
* selection state, scroll position concept, and filter criteria.
|
|
10
|
+
* Part of P2.4 Performance at Scale roadmap.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
|
|
14
|
+
import { renderHook, act } from '@testing-library/react';
|
|
15
|
+
import React from 'react';
|
|
16
|
+
|
|
17
|
+
// ---------------------------------------------------------------------------
|
|
18
|
+
// Types for view switching state management
|
|
19
|
+
// ---------------------------------------------------------------------------
|
|
20
|
+
|
|
21
|
+
type ViewType = 'grid' | 'kanban' | 'calendar';
|
|
22
|
+
|
|
23
|
+
interface ViewState {
|
|
24
|
+
currentView: ViewType;
|
|
25
|
+
selectedIds: string[];
|
|
26
|
+
filters: Record<string, string>;
|
|
27
|
+
scrollPosition: number;
|
|
28
|
+
sortField?: string;
|
|
29
|
+
sortDirection?: 'asc' | 'desc';
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
// ---------------------------------------------------------------------------
|
|
33
|
+
// useViewSwitcher hook — manages cross-view state preservation
|
|
34
|
+
// ---------------------------------------------------------------------------
|
|
35
|
+
|
|
36
|
+
function useViewSwitcher(initialView: ViewType = 'grid') {
|
|
37
|
+
const [state, setState] = React.useState<ViewState>({
|
|
38
|
+
currentView: initialView,
|
|
39
|
+
selectedIds: [],
|
|
40
|
+
filters: {},
|
|
41
|
+
scrollPosition: 0,
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
const switchView = React.useCallback(
|
|
45
|
+
(newView: ViewType) => {
|
|
46
|
+
setState((prev) => ({
|
|
47
|
+
...prev,
|
|
48
|
+
currentView: newView,
|
|
49
|
+
}));
|
|
50
|
+
},
|
|
51
|
+
[],
|
|
52
|
+
);
|
|
53
|
+
|
|
54
|
+
const setSelectedIds = React.useCallback(
|
|
55
|
+
(ids: string[]) => {
|
|
56
|
+
setState((prev) => ({ ...prev, selectedIds: ids }));
|
|
57
|
+
},
|
|
58
|
+
[],
|
|
59
|
+
);
|
|
60
|
+
|
|
61
|
+
const setFilters = React.useCallback(
|
|
62
|
+
(filters: Record<string, string>) => {
|
|
63
|
+
setState((prev) => ({ ...prev, filters }));
|
|
64
|
+
},
|
|
65
|
+
[],
|
|
66
|
+
);
|
|
67
|
+
|
|
68
|
+
const setScrollPosition = React.useCallback(
|
|
69
|
+
(position: number) => {
|
|
70
|
+
setState((prev) => ({ ...prev, scrollPosition: position }));
|
|
71
|
+
},
|
|
72
|
+
[],
|
|
73
|
+
);
|
|
74
|
+
|
|
75
|
+
const setSort = React.useCallback(
|
|
76
|
+
(field: string, direction: 'asc' | 'desc') => {
|
|
77
|
+
setState((prev) => ({ ...prev, sortField: field, sortDirection: direction }));
|
|
78
|
+
},
|
|
79
|
+
[],
|
|
80
|
+
);
|
|
81
|
+
|
|
82
|
+
const clearSelection = React.useCallback(() => {
|
|
83
|
+
setState((prev) => ({ ...prev, selectedIds: [] }));
|
|
84
|
+
}, []);
|
|
85
|
+
|
|
86
|
+
const clearFilters = React.useCallback(() => {
|
|
87
|
+
setState((prev) => ({ ...prev, filters: {} }));
|
|
88
|
+
}, []);
|
|
89
|
+
|
|
90
|
+
return {
|
|
91
|
+
state,
|
|
92
|
+
switchView,
|
|
93
|
+
setSelectedIds,
|
|
94
|
+
setFilters,
|
|
95
|
+
setScrollPosition,
|
|
96
|
+
setSort,
|
|
97
|
+
clearSelection,
|
|
98
|
+
clearFilters,
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
// ---------------------------------------------------------------------------
|
|
103
|
+
// Tests
|
|
104
|
+
// ---------------------------------------------------------------------------
|
|
105
|
+
|
|
106
|
+
describe('View Switching: state preservation', () => {
|
|
107
|
+
it('should initialize with default view and empty state', () => {
|
|
108
|
+
const { result } = renderHook(() => useViewSwitcher('grid'));
|
|
109
|
+
|
|
110
|
+
expect(result.current.state.currentView).toBe('grid');
|
|
111
|
+
expect(result.current.state.selectedIds).toEqual([]);
|
|
112
|
+
expect(result.current.state.filters).toEqual({});
|
|
113
|
+
expect(result.current.state.scrollPosition).toBe(0);
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
it('should switch from grid to kanban while preserving selection', () => {
|
|
117
|
+
const { result } = renderHook(() => useViewSwitcher('grid'));
|
|
118
|
+
|
|
119
|
+
act(() => {
|
|
120
|
+
result.current.setSelectedIds(['row-1', 'row-5', 'row-10']);
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
expect(result.current.state.selectedIds).toEqual(['row-1', 'row-5', 'row-10']);
|
|
124
|
+
|
|
125
|
+
act(() => {
|
|
126
|
+
result.current.switchView('kanban');
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
expect(result.current.state.currentView).toBe('kanban');
|
|
130
|
+
expect(result.current.state.selectedIds).toEqual(['row-1', 'row-5', 'row-10']);
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
it('should switch from kanban to calendar while preserving selection', () => {
|
|
134
|
+
const { result } = renderHook(() => useViewSwitcher('kanban'));
|
|
135
|
+
|
|
136
|
+
act(() => {
|
|
137
|
+
result.current.setSelectedIds(['card-3', 'card-7']);
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
act(() => {
|
|
141
|
+
result.current.switchView('calendar');
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
expect(result.current.state.currentView).toBe('calendar');
|
|
145
|
+
expect(result.current.state.selectedIds).toEqual(['card-3', 'card-7']);
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
it('should switch from calendar to grid while preserving selection', () => {
|
|
149
|
+
const { result } = renderHook(() => useViewSwitcher('calendar'));
|
|
150
|
+
|
|
151
|
+
act(() => {
|
|
152
|
+
result.current.setSelectedIds(['event-1']);
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
act(() => {
|
|
156
|
+
result.current.switchView('grid');
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
expect(result.current.state.currentView).toBe('grid');
|
|
160
|
+
expect(result.current.state.selectedIds).toEqual(['event-1']);
|
|
161
|
+
});
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
describe('View Switching: filter preservation', () => {
|
|
165
|
+
it('should preserve filters when switching grid → kanban', () => {
|
|
166
|
+
const { result } = renderHook(() => useViewSwitcher('grid'));
|
|
167
|
+
|
|
168
|
+
act(() => {
|
|
169
|
+
result.current.setFilters({ status: 'active', department: 'engineering' });
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
act(() => {
|
|
173
|
+
result.current.switchView('kanban');
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
expect(result.current.state.currentView).toBe('kanban');
|
|
177
|
+
expect(result.current.state.filters).toEqual({
|
|
178
|
+
status: 'active',
|
|
179
|
+
department: 'engineering',
|
|
180
|
+
});
|
|
181
|
+
});
|
|
182
|
+
|
|
183
|
+
it('should preserve filters when switching kanban → calendar', () => {
|
|
184
|
+
const { result } = renderHook(() => useViewSwitcher('kanban'));
|
|
185
|
+
|
|
186
|
+
act(() => {
|
|
187
|
+
result.current.setFilters({ priority: 'high' });
|
|
188
|
+
});
|
|
189
|
+
|
|
190
|
+
act(() => {
|
|
191
|
+
result.current.switchView('calendar');
|
|
192
|
+
});
|
|
193
|
+
|
|
194
|
+
expect(result.current.state.filters).toEqual({ priority: 'high' });
|
|
195
|
+
});
|
|
196
|
+
|
|
197
|
+
it('should preserve filters across multiple view switches', () => {
|
|
198
|
+
const { result } = renderHook(() => useViewSwitcher('grid'));
|
|
199
|
+
|
|
200
|
+
act(() => {
|
|
201
|
+
result.current.setFilters({ type: 'bug', assignee: 'alice' });
|
|
202
|
+
});
|
|
203
|
+
|
|
204
|
+
// grid → kanban
|
|
205
|
+
act(() => {
|
|
206
|
+
result.current.switchView('kanban');
|
|
207
|
+
});
|
|
208
|
+
expect(result.current.state.filters).toEqual({ type: 'bug', assignee: 'alice' });
|
|
209
|
+
|
|
210
|
+
// kanban → calendar
|
|
211
|
+
act(() => {
|
|
212
|
+
result.current.switchView('calendar');
|
|
213
|
+
});
|
|
214
|
+
expect(result.current.state.filters).toEqual({ type: 'bug', assignee: 'alice' });
|
|
215
|
+
|
|
216
|
+
// calendar → grid
|
|
217
|
+
act(() => {
|
|
218
|
+
result.current.switchView('grid');
|
|
219
|
+
});
|
|
220
|
+
expect(result.current.state.filters).toEqual({ type: 'bug', assignee: 'alice' });
|
|
221
|
+
});
|
|
222
|
+
|
|
223
|
+
it('should allow clearing filters independently of view switch', () => {
|
|
224
|
+
const { result } = renderHook(() => useViewSwitcher('grid'));
|
|
225
|
+
|
|
226
|
+
act(() => {
|
|
227
|
+
result.current.setFilters({ status: 'open' });
|
|
228
|
+
});
|
|
229
|
+
|
|
230
|
+
act(() => {
|
|
231
|
+
result.current.switchView('kanban');
|
|
232
|
+
});
|
|
233
|
+
|
|
234
|
+
act(() => {
|
|
235
|
+
result.current.clearFilters();
|
|
236
|
+
});
|
|
237
|
+
|
|
238
|
+
expect(result.current.state.currentView).toBe('kanban');
|
|
239
|
+
expect(result.current.state.filters).toEqual({});
|
|
240
|
+
});
|
|
241
|
+
});
|
|
242
|
+
|
|
243
|
+
describe('View Switching: scroll position preservation', () => {
|
|
244
|
+
it('should preserve scroll position when switching views', () => {
|
|
245
|
+
const { result } = renderHook(() => useViewSwitcher('grid'));
|
|
246
|
+
|
|
247
|
+
act(() => {
|
|
248
|
+
result.current.setScrollPosition(1500);
|
|
249
|
+
});
|
|
250
|
+
|
|
251
|
+
act(() => {
|
|
252
|
+
result.current.switchView('kanban');
|
|
253
|
+
});
|
|
254
|
+
|
|
255
|
+
expect(result.current.state.scrollPosition).toBe(1500);
|
|
256
|
+
});
|
|
257
|
+
|
|
258
|
+
it('should preserve scroll position across full cycle: grid → kanban → calendar → grid', () => {
|
|
259
|
+
const { result } = renderHook(() => useViewSwitcher('grid'));
|
|
260
|
+
|
|
261
|
+
act(() => {
|
|
262
|
+
result.current.setScrollPosition(3200);
|
|
263
|
+
});
|
|
264
|
+
|
|
265
|
+
act(() => {
|
|
266
|
+
result.current.switchView('kanban');
|
|
267
|
+
});
|
|
268
|
+
expect(result.current.state.scrollPosition).toBe(3200);
|
|
269
|
+
|
|
270
|
+
act(() => {
|
|
271
|
+
result.current.switchView('calendar');
|
|
272
|
+
});
|
|
273
|
+
expect(result.current.state.scrollPosition).toBe(3200);
|
|
274
|
+
|
|
275
|
+
act(() => {
|
|
276
|
+
result.current.switchView('grid');
|
|
277
|
+
});
|
|
278
|
+
expect(result.current.state.scrollPosition).toBe(3200);
|
|
279
|
+
});
|
|
280
|
+
});
|
|
281
|
+
|
|
282
|
+
describe('View Switching: sort preservation', () => {
|
|
283
|
+
it('should preserve sort when switching views', () => {
|
|
284
|
+
const { result } = renderHook(() => useViewSwitcher('grid'));
|
|
285
|
+
|
|
286
|
+
act(() => {
|
|
287
|
+
result.current.setSort('name', 'asc');
|
|
288
|
+
});
|
|
289
|
+
|
|
290
|
+
act(() => {
|
|
291
|
+
result.current.switchView('kanban');
|
|
292
|
+
});
|
|
293
|
+
|
|
294
|
+
expect(result.current.state.sortField).toBe('name');
|
|
295
|
+
expect(result.current.state.sortDirection).toBe('asc');
|
|
296
|
+
});
|
|
297
|
+
|
|
298
|
+
it('should preserve sort across multiple switches', () => {
|
|
299
|
+
const { result } = renderHook(() => useViewSwitcher('grid'));
|
|
300
|
+
|
|
301
|
+
act(() => {
|
|
302
|
+
result.current.setSort('createdAt', 'desc');
|
|
303
|
+
});
|
|
304
|
+
|
|
305
|
+
act(() => {
|
|
306
|
+
result.current.switchView('calendar');
|
|
307
|
+
});
|
|
308
|
+
|
|
309
|
+
act(() => {
|
|
310
|
+
result.current.switchView('kanban');
|
|
311
|
+
});
|
|
312
|
+
|
|
313
|
+
expect(result.current.state.sortField).toBe('createdAt');
|
|
314
|
+
expect(result.current.state.sortDirection).toBe('desc');
|
|
315
|
+
});
|
|
316
|
+
});
|
|
317
|
+
|
|
318
|
+
describe('View Switching: combined state preservation', () => {
|
|
319
|
+
it('should preserve all state simultaneously when switching views', () => {
|
|
320
|
+
const { result } = renderHook(() => useViewSwitcher('grid'));
|
|
321
|
+
|
|
322
|
+
// Set up complex state
|
|
323
|
+
act(() => {
|
|
324
|
+
result.current.setSelectedIds(['item-1', 'item-2', 'item-3']);
|
|
325
|
+
result.current.setFilters({ status: 'active', priority: 'high' });
|
|
326
|
+
result.current.setScrollPosition(2500);
|
|
327
|
+
result.current.setSort('title', 'asc');
|
|
328
|
+
});
|
|
329
|
+
|
|
330
|
+
// Switch to kanban
|
|
331
|
+
act(() => {
|
|
332
|
+
result.current.switchView('kanban');
|
|
333
|
+
});
|
|
334
|
+
|
|
335
|
+
expect(result.current.state.currentView).toBe('kanban');
|
|
336
|
+
expect(result.current.state.selectedIds).toEqual(['item-1', 'item-2', 'item-3']);
|
|
337
|
+
expect(result.current.state.filters).toEqual({ status: 'active', priority: 'high' });
|
|
338
|
+
expect(result.current.state.scrollPosition).toBe(2500);
|
|
339
|
+
expect(result.current.state.sortField).toBe('title');
|
|
340
|
+
expect(result.current.state.sortDirection).toBe('asc');
|
|
341
|
+
|
|
342
|
+
// Switch to calendar
|
|
343
|
+
act(() => {
|
|
344
|
+
result.current.switchView('calendar');
|
|
345
|
+
});
|
|
346
|
+
|
|
347
|
+
expect(result.current.state.currentView).toBe('calendar');
|
|
348
|
+
expect(result.current.state.selectedIds).toEqual(['item-1', 'item-2', 'item-3']);
|
|
349
|
+
expect(result.current.state.filters).toEqual({ status: 'active', priority: 'high' });
|
|
350
|
+
expect(result.current.state.scrollPosition).toBe(2500);
|
|
351
|
+
expect(result.current.state.sortField).toBe('title');
|
|
352
|
+
expect(result.current.state.sortDirection).toBe('asc');
|
|
353
|
+
|
|
354
|
+
// Switch back to grid
|
|
355
|
+
act(() => {
|
|
356
|
+
result.current.switchView('grid');
|
|
357
|
+
});
|
|
358
|
+
|
|
359
|
+
expect(result.current.state.currentView).toBe('grid');
|
|
360
|
+
expect(result.current.state.selectedIds).toEqual(['item-1', 'item-2', 'item-3']);
|
|
361
|
+
expect(result.current.state.filters).toEqual({ status: 'active', priority: 'high' });
|
|
362
|
+
expect(result.current.state.scrollPosition).toBe(2500);
|
|
363
|
+
expect(result.current.state.sortField).toBe('title');
|
|
364
|
+
expect(result.current.state.sortDirection).toBe('asc');
|
|
365
|
+
});
|
|
366
|
+
|
|
367
|
+
it('should allow modifying state after view switch', () => {
|
|
368
|
+
const { result } = renderHook(() => useViewSwitcher('grid'));
|
|
369
|
+
|
|
370
|
+
act(() => {
|
|
371
|
+
result.current.setSelectedIds(['item-1']);
|
|
372
|
+
result.current.setFilters({ status: 'open' });
|
|
373
|
+
});
|
|
374
|
+
|
|
375
|
+
act(() => {
|
|
376
|
+
result.current.switchView('kanban');
|
|
377
|
+
});
|
|
378
|
+
|
|
379
|
+
// Modify state in kanban view
|
|
380
|
+
act(() => {
|
|
381
|
+
result.current.setSelectedIds(['item-1', 'item-4']);
|
|
382
|
+
result.current.setFilters({ status: 'open', type: 'feature' });
|
|
383
|
+
});
|
|
384
|
+
|
|
385
|
+
expect(result.current.state.selectedIds).toEqual(['item-1', 'item-4']);
|
|
386
|
+
expect(result.current.state.filters).toEqual({ status: 'open', type: 'feature' });
|
|
387
|
+
|
|
388
|
+
// Switch to calendar - should have updated state
|
|
389
|
+
act(() => {
|
|
390
|
+
result.current.switchView('calendar');
|
|
391
|
+
});
|
|
392
|
+
|
|
393
|
+
expect(result.current.state.selectedIds).toEqual(['item-1', 'item-4']);
|
|
394
|
+
expect(result.current.state.filters).toEqual({ status: 'open', type: 'feature' });
|
|
395
|
+
});
|
|
396
|
+
|
|
397
|
+
it('should handle clearing selection independently of other state', () => {
|
|
398
|
+
const { result } = renderHook(() => useViewSwitcher('grid'));
|
|
399
|
+
|
|
400
|
+
act(() => {
|
|
401
|
+
result.current.setSelectedIds(['a', 'b', 'c']);
|
|
402
|
+
result.current.setFilters({ status: 'done' });
|
|
403
|
+
result.current.setScrollPosition(800);
|
|
404
|
+
});
|
|
405
|
+
|
|
406
|
+
act(() => {
|
|
407
|
+
result.current.switchView('kanban');
|
|
408
|
+
});
|
|
409
|
+
|
|
410
|
+
act(() => {
|
|
411
|
+
result.current.clearSelection();
|
|
412
|
+
});
|
|
413
|
+
|
|
414
|
+
expect(result.current.state.selectedIds).toEqual([]);
|
|
415
|
+
expect(result.current.state.filters).toEqual({ status: 'done' });
|
|
416
|
+
expect(result.current.state.scrollPosition).toBe(800);
|
|
417
|
+
});
|
|
418
|
+
});
|
|
419
|
+
|
|
420
|
+
describe('View Switching: rapid switching', () => {
|
|
421
|
+
it('should handle rapid view switches without losing state', () => {
|
|
422
|
+
const { result } = renderHook(() => useViewSwitcher('grid'));
|
|
423
|
+
|
|
424
|
+
act(() => {
|
|
425
|
+
result.current.setSelectedIds(['item-1']);
|
|
426
|
+
result.current.setFilters({ query: 'test' });
|
|
427
|
+
});
|
|
428
|
+
|
|
429
|
+
// Rapid switches
|
|
430
|
+
act(() => {
|
|
431
|
+
result.current.switchView('kanban');
|
|
432
|
+
result.current.switchView('calendar');
|
|
433
|
+
result.current.switchView('grid');
|
|
434
|
+
result.current.switchView('kanban');
|
|
435
|
+
});
|
|
436
|
+
|
|
437
|
+
expect(result.current.state.currentView).toBe('kanban');
|
|
438
|
+
expect(result.current.state.selectedIds).toEqual(['item-1']);
|
|
439
|
+
expect(result.current.state.filters).toEqual({ query: 'test' });
|
|
440
|
+
});
|
|
441
|
+
|
|
442
|
+
it('should handle switching to the same view', () => {
|
|
443
|
+
const { result } = renderHook(() => useViewSwitcher('grid'));
|
|
444
|
+
|
|
445
|
+
act(() => {
|
|
446
|
+
result.current.setSelectedIds(['x']);
|
|
447
|
+
});
|
|
448
|
+
|
|
449
|
+
act(() => {
|
|
450
|
+
result.current.switchView('grid');
|
|
451
|
+
});
|
|
452
|
+
|
|
453
|
+
expect(result.current.state.currentView).toBe('grid');
|
|
454
|
+
expect(result.current.state.selectedIds).toEqual(['x']);
|
|
455
|
+
});
|
|
456
|
+
});
|
|
@@ -0,0 +1,260 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ObjectUI
|
|
3
|
+
* Copyright (c) 2024-present ObjectStack Inc.
|
|
4
|
+
*
|
|
5
|
+
* This source code is licensed under the MIT license found in the
|
|
6
|
+
* LICENSE file in the root directory of this source tree.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* @object-ui/react - Drag and Drop Context
|
|
11
|
+
*
|
|
12
|
+
* Provides a unified DnD configuration system to the component tree.
|
|
13
|
+
* Implements DndConfigSchema, DragItemSchema, DropZoneSchema,
|
|
14
|
+
* DragConstraintSchema, and DropEffectSchema from @objectstack/spec v2.0.7.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
import React, { createContext, useCallback, useContext, useMemo, useState } from 'react';
|
|
18
|
+
|
|
19
|
+
/** Drop effect types aligned with DropEffectSchema */
|
|
20
|
+
export type DropEffectType = 'move' | 'copy' | 'link' | 'none';
|
|
21
|
+
|
|
22
|
+
/** Drag constraint axis aligned with DragConstraintSchema */
|
|
23
|
+
export type DragAxis = 'x' | 'y' | 'both';
|
|
24
|
+
|
|
25
|
+
/** Drag item metadata */
|
|
26
|
+
export interface DragItemData {
|
|
27
|
+
/** Unique identifier for the dragged item */
|
|
28
|
+
id: string;
|
|
29
|
+
/** Type of the dragged item (for drop zone filtering) */
|
|
30
|
+
type: string;
|
|
31
|
+
/** Arbitrary data associated with the drag */
|
|
32
|
+
data?: Record<string, unknown>;
|
|
33
|
+
/** Source container/zone identifier */
|
|
34
|
+
sourceId?: string;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** Drop zone configuration */
|
|
38
|
+
export interface DropZoneConfig {
|
|
39
|
+
/** Unique identifier for the drop zone */
|
|
40
|
+
id: string;
|
|
41
|
+
/** Accepted drag item types */
|
|
42
|
+
acceptTypes: string[];
|
|
43
|
+
/** Drop effect to apply */
|
|
44
|
+
effect?: DropEffectType;
|
|
45
|
+
/** Whether the zone is currently disabled */
|
|
46
|
+
disabled?: boolean;
|
|
47
|
+
/** Maximum number of items this zone can hold */
|
|
48
|
+
maxItems?: number;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** Drag constraints configuration */
|
|
52
|
+
export interface DragConstraintConfig {
|
|
53
|
+
/** Constrain movement to an axis */
|
|
54
|
+
axis?: DragAxis;
|
|
55
|
+
/** Minimum distance before drag starts (in pixels) */
|
|
56
|
+
activationDistance?: number;
|
|
57
|
+
/** Whether to show a drag handle */
|
|
58
|
+
showHandle?: boolean;
|
|
59
|
+
/** Container bounds (CSS selector) */
|
|
60
|
+
containerId?: string;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/** Overall DnD configuration aligned with DndConfigSchema */
|
|
64
|
+
export interface DndSystemConfig {
|
|
65
|
+
/** Whether DnD is enabled */
|
|
66
|
+
enabled?: boolean;
|
|
67
|
+
/** Default drag constraints */
|
|
68
|
+
defaultConstraints?: DragConstraintConfig;
|
|
69
|
+
/** Whether to show drag overlay/preview */
|
|
70
|
+
showPreview?: boolean;
|
|
71
|
+
/** Auto-scroll when dragging near edges */
|
|
72
|
+
autoScroll?: boolean;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/** Drag event information */
|
|
76
|
+
export interface DragEvent {
|
|
77
|
+
/** The dragged item */
|
|
78
|
+
item: DragItemData;
|
|
79
|
+
/** Target drop zone (null if not over a zone) */
|
|
80
|
+
targetZoneId: string | null;
|
|
81
|
+
/** The applied drop effect */
|
|
82
|
+
effect: DropEffectType;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
export interface DndProviderProps {
|
|
86
|
+
children: React.ReactNode;
|
|
87
|
+
/** System-wide DnD configuration */
|
|
88
|
+
config?: DndSystemConfig;
|
|
89
|
+
/** Called when a drag starts */
|
|
90
|
+
onDragStart?: (event: DragEvent) => void;
|
|
91
|
+
/** Called when an item is dropped */
|
|
92
|
+
onDrop?: (event: DragEvent) => void;
|
|
93
|
+
/** Called when a drag is cancelled */
|
|
94
|
+
onDragCancel?: (event: DragEvent) => void;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
interface DndContextValue {
|
|
98
|
+
/** System-wide DnD configuration */
|
|
99
|
+
config: DndSystemConfig;
|
|
100
|
+
/** Whether DnD is enabled */
|
|
101
|
+
enabled: boolean;
|
|
102
|
+
/** Currently dragging item (null if not dragging) */
|
|
103
|
+
activeItem: DragItemData | null;
|
|
104
|
+
/** Registered drop zones */
|
|
105
|
+
dropZones: Map<string, DropZoneConfig>;
|
|
106
|
+
/** Start a drag operation */
|
|
107
|
+
startDrag: (item: DragItemData) => void;
|
|
108
|
+
/** End the current drag operation */
|
|
109
|
+
endDrag: (targetZoneId?: string) => void;
|
|
110
|
+
/** Cancel the current drag */
|
|
111
|
+
cancelDrag: () => void;
|
|
112
|
+
/** Register a drop zone */
|
|
113
|
+
registerDropZone: (zone: DropZoneConfig) => void;
|
|
114
|
+
/** Unregister a drop zone */
|
|
115
|
+
unregisterDropZone: (zoneId: string) => void;
|
|
116
|
+
/** Check if a drop zone accepts a drag item type */
|
|
117
|
+
canDrop: (zoneId: string, itemType: string) => boolean;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
const DndCtx = createContext<DndContextValue | null>(null);
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* DndProvider — Provides a unified DnD system aligned with @objectstack/spec v2.0.7.
|
|
124
|
+
*
|
|
125
|
+
* This is a coordination layer; actual drag mechanics are delegated to
|
|
126
|
+
* libraries like @dnd-kit/core or react-grid-layout in individual plugins.
|
|
127
|
+
*
|
|
128
|
+
* @example
|
|
129
|
+
* ```tsx
|
|
130
|
+
* <DndProvider
|
|
131
|
+
* config={{ enabled: true, showPreview: true, autoScroll: true }}
|
|
132
|
+
* onDrop={(event) => handleDrop(event.item, event.targetZoneId)}
|
|
133
|
+
* >
|
|
134
|
+
* <KanbanBoard />
|
|
135
|
+
* </DndProvider>
|
|
136
|
+
* ```
|
|
137
|
+
*/
|
|
138
|
+
export const DndProvider: React.FC<DndProviderProps> = ({
|
|
139
|
+
children,
|
|
140
|
+
config: userConfig = {},
|
|
141
|
+
onDragStart,
|
|
142
|
+
onDrop,
|
|
143
|
+
onDragCancel,
|
|
144
|
+
}) => {
|
|
145
|
+
const config = useMemo<DndSystemConfig>(
|
|
146
|
+
() => ({
|
|
147
|
+
enabled: true,
|
|
148
|
+
showPreview: true,
|
|
149
|
+
autoScroll: true,
|
|
150
|
+
...userConfig,
|
|
151
|
+
}),
|
|
152
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
153
|
+
[JSON.stringify(userConfig)],
|
|
154
|
+
);
|
|
155
|
+
|
|
156
|
+
const [activeItem, setActiveItem] = useState<DragItemData | null>(null);
|
|
157
|
+
const [dropZones] = useState<Map<string, DropZoneConfig>>(() => new Map());
|
|
158
|
+
|
|
159
|
+
const startDrag = useCallback(
|
|
160
|
+
(item: DragItemData) => {
|
|
161
|
+
if (!config.enabled) return;
|
|
162
|
+
setActiveItem(item);
|
|
163
|
+
onDragStart?.({ item, targetZoneId: null, effect: 'move' });
|
|
164
|
+
},
|
|
165
|
+
[config.enabled, onDragStart],
|
|
166
|
+
);
|
|
167
|
+
|
|
168
|
+
const endDrag = useCallback(
|
|
169
|
+
(targetZoneId?: string) => {
|
|
170
|
+
if (!activeItem) return;
|
|
171
|
+
const zone = targetZoneId ? dropZones.get(targetZoneId) : undefined;
|
|
172
|
+
const effect: DropEffectType = zone?.effect ?? 'move';
|
|
173
|
+
onDrop?.({ item: activeItem, targetZoneId: targetZoneId ?? null, effect });
|
|
174
|
+
setActiveItem(null);
|
|
175
|
+
},
|
|
176
|
+
[activeItem, dropZones, onDrop],
|
|
177
|
+
);
|
|
178
|
+
|
|
179
|
+
const cancelDrag = useCallback(() => {
|
|
180
|
+
if (!activeItem) return;
|
|
181
|
+
onDragCancel?.({ item: activeItem, targetZoneId: null, effect: 'none' });
|
|
182
|
+
setActiveItem(null);
|
|
183
|
+
}, [activeItem, onDragCancel]);
|
|
184
|
+
|
|
185
|
+
const registerDropZone = useCallback(
|
|
186
|
+
(zone: DropZoneConfig) => {
|
|
187
|
+
dropZones.set(zone.id, zone);
|
|
188
|
+
},
|
|
189
|
+
[dropZones],
|
|
190
|
+
);
|
|
191
|
+
|
|
192
|
+
const unregisterDropZone = useCallback(
|
|
193
|
+
(zoneId: string) => {
|
|
194
|
+
dropZones.delete(zoneId);
|
|
195
|
+
},
|
|
196
|
+
[dropZones],
|
|
197
|
+
);
|
|
198
|
+
|
|
199
|
+
const canDrop = useCallback(
|
|
200
|
+
(zoneId: string, itemType: string): boolean => {
|
|
201
|
+
const zone = dropZones.get(zoneId);
|
|
202
|
+
if (!zone || zone.disabled) return false;
|
|
203
|
+
return zone.acceptTypes.includes(itemType);
|
|
204
|
+
},
|
|
205
|
+
[dropZones],
|
|
206
|
+
);
|
|
207
|
+
|
|
208
|
+
const value = useMemo<DndContextValue>(
|
|
209
|
+
() => ({
|
|
210
|
+
config,
|
|
211
|
+
enabled: config.enabled ?? true,
|
|
212
|
+
activeItem,
|
|
213
|
+
dropZones,
|
|
214
|
+
startDrag,
|
|
215
|
+
endDrag,
|
|
216
|
+
cancelDrag,
|
|
217
|
+
registerDropZone,
|
|
218
|
+
unregisterDropZone,
|
|
219
|
+
canDrop,
|
|
220
|
+
}),
|
|
221
|
+
[
|
|
222
|
+
config,
|
|
223
|
+
activeItem,
|
|
224
|
+
dropZones,
|
|
225
|
+
startDrag,
|
|
226
|
+
endDrag,
|
|
227
|
+
cancelDrag,
|
|
228
|
+
registerDropZone,
|
|
229
|
+
unregisterDropZone,
|
|
230
|
+
canDrop,
|
|
231
|
+
],
|
|
232
|
+
);
|
|
233
|
+
|
|
234
|
+
return <DndCtx.Provider value={value}>{children}</DndCtx.Provider>;
|
|
235
|
+
};
|
|
236
|
+
|
|
237
|
+
DndProvider.displayName = 'DndProvider';
|
|
238
|
+
|
|
239
|
+
/**
|
|
240
|
+
* Hook to consume the DnD context.
|
|
241
|
+
*
|
|
242
|
+
* @throws Error if used outside a DndProvider
|
|
243
|
+
*/
|
|
244
|
+
export function useDnd(): DndContextValue {
|
|
245
|
+
const ctx = useContext(DndCtx);
|
|
246
|
+
if (!ctx) {
|
|
247
|
+
throw new Error(
|
|
248
|
+
'useDnd must be used within a <DndProvider>. ' +
|
|
249
|
+
'Wrap your app with <DndProvider> to use the drag and drop system.',
|
|
250
|
+
);
|
|
251
|
+
}
|
|
252
|
+
return ctx;
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
/**
|
|
256
|
+
* Hook to check if a DndProvider is available.
|
|
257
|
+
*/
|
|
258
|
+
export function useHasDndProvider(): boolean {
|
|
259
|
+
return useContext(DndCtx) !== null;
|
|
260
|
+
}
|