|
1
|
import { ColumnDef, Row } from "@tanstack/react-table" |
|
2
|
import { FieldValues } from "react-hook-form" |
|
3
|
import { |
|
4
|
DataGridColumnType, |
|
5
|
DataGridCoordinates, |
|
6
|
Grid, |
|
7
|
GridCell, |
|
8
|
InternalColumnMeta, |
|
9
|
} from "../types" |
|
10
|
|
|
11
|
export class DataGridMatrix<TData, TFieldValues extends FieldValues> { |
|
12
|
private multiColumnSelection: boolean |
|
13
|
private cells: Grid<TFieldValues> |
|
14
|
public rowAccessors: (string | null)[] = [] |
|
15
|
public columnAccessors: (string | null)[] = [] |
|
16
|
|
|
17
|
constructor( |
|
18
|
data: Row<TData>[], |
|
19
|
columns: ColumnDef<TData>[], |
|
20
|
multiColumnSelection: boolean = false |
|
21
|
) { |
|
22
|
this.multiColumnSelection = multiColumnSelection |
|
23
|
this.cells = this._populateCells(data, columns) |
|
24
|
|
|
25
|
this.rowAccessors = this._computeRowAccessors() |
|
26
|
this.columnAccessors = this._computeColumnAccessors() |
|
27
|
} |
|
28
|
|
|
29
|
private _computeRowAccessors(): (string | null)[] { |
|
30
|
return this.cells.map((_, rowIndex) => this.getRowAccessor(rowIndex)) |
|
31
|
} |
|
32
|
|
|
33
|
private _computeColumnAccessors(): (string | null)[] { |
|
34
|
if (this.cells.length === 0) { |
|
35
|
return [] |
|
36
|
} |
|
37
|
|
|
38
|
return this.cells[0].map((_, colIndex) => this.getColumnAccessor(colIndex)) |
|
39
|
} |
|
40
|
|
|
41
|
getFirstNavigableCell(): DataGridCoordinates | null { |
|
42
|
for (let row = 0; row < this.cells.length; row++) { |
|
43
|
for (let col = 0; col < this.cells[0].length; col++) { |
|
44
|
if (this.cells[row][col] !== null) { |
|
45
|
return { row, col } |
|
46
|
} |
|
47
|
} |
|
48
|
} |
|
49
|
|
|
50
|
return null |
|
51
|
} |
|
52
|
|
|
53
|
getFieldsInRow(row: number): string[] { |
|
54
|
const keys: string[] = [] |
|
55
|
|
|
56
|
if (row < 0 || row >= this.cells.length) { |
|
57
|
return keys |
|
58
|
} |
|
59
|
|
|
60
|
this.cells[row].forEach((cell) => { |
|
61
|
if (cell !== null) { |
|
62
|
keys.push(cell.field) |
|
63
|
} |
|
64
|
}) |
|
65
|
|
|
66
|
return keys |
|
67
|
} |
|
68
|
|
|
69
|
getFieldsInSelection( |
|
70
|
start: DataGridCoordinates | null, |
|
71
|
end: DataGridCoordinates | null |
|
72
|
): string[] { |
|
73
|
const keys: string[] = [] |
|
74
|
|
|
75
|
if (!start || !end) { |
|
76
|
return keys |
|
77
|
} |
|
78
|
|
|
79
|
if (!this.multiColumnSelection && start.col !== end.col) { |
|
80
|
throw new Error( |
|
81
|
"Selection must be in the same column when multiColumnSelection is disabled" |
|
82
|
) |
|
83
|
} |
|
84
|
|
|
85
|
const startRow = Math.min(start.row, end.row) |
|
86
|
const endRow = Math.max(start.row, end.row) |
|
87
|
const startCol = this.multiColumnSelection |
|
88
|
? Math.min(start.col, end.col) |
|
89
|
: start.col |
|
90
|
const endCol = this.multiColumnSelection |
|
91
|
? Math.max(start.col, end.col) |
|
92
|
: start.col |
|
93
|
|
|
94
|
for (let row = startRow; row <= endRow; row++) { |
|
95
|
for (let col = startCol; col <= endCol; col++) { |
|
96
|
if (this._isValidPosition(row, col) && this.cells[row][col] !== null) { |
|
97
|
keys.push(this.cells[row][col]?.field as string) |
|
98
|
} |
|
99
|
} |
|
100
|
} |
|
101
|
|
|
102
|
return keys |
|
103
|
} |
|
104
|
|
|
105
|
getCellField(cell: DataGridCoordinates): string | null { |
|
106
|
if (this._isValidPosition(cell.row, cell.col)) { |
|
107
|
return this.cells[cell.row][cell.col]?.field || null |
|
108
|
} |
|
109
|
|
|
110
|
return null |
|
111
|
} |
|
112
|
|
|
113
|
getCellType(cell: DataGridCoordinates): DataGridColumnType | null { |
|
114
|
if (this._isValidPosition(cell.row, cell.col)) { |
|
115
|
return this.cells[cell.row][cell.col]?.type || null |
|
116
|
} |
|
117
|
|
|
118
|
return null |
|
119
|
} |
|
120
|
|
|
121
|
getIsCellSelected( |
|
122
|
cell: DataGridCoordinates | null, |
|
123
|
start: DataGridCoordinates | null, |
|
124
|
end: DataGridCoordinates | null |
|
125
|
): boolean { |
|
126
|
if (!cell || !start || !end) { |
|
127
|
return false |
|
128
|
} |
|
129
|
|
|
130
|
if (!this.multiColumnSelection && start.col !== end.col) { |
|
131
|
throw new Error( |
|
132
|
"Selection must be in the same column when multiColumnSelection is disabled" |
|
133
|
) |
|
134
|
} |
|
135
|
|
|
136
|
const startRow = Math.min(start.row, end.row) |
|
137
|
const endRow = Math.max(start.row, end.row) |
|
138
|
const startCol = this.multiColumnSelection |
|
139
|
? Math.min(start.col, end.col) |
|
140
|
: start.col |
|
141
|
const endCol = this.multiColumnSelection |
|
142
|
? Math.max(start.col, end.col) |
|
143
|
: start.col |
|
144
|
|
|
145
|
return ( |
|
146
|
cell.row >= startRow && |
|
147
|
cell.row <= endRow && |
|
148
|
cell.col >= startCol && |
|
149
|
cell.col <= endCol |
|
150
|
) |
|
151
|
} |
|
152
|
|
|
153
|
toggleColumn(col: number, enabled: boolean) { |
|
154
|
if (col < 0 || col >= this.cells[0].length) { |
|
155
|
return |
|
156
|
} |
|
157
|
|
|
158
|
this.cells.forEach((row, index) => { |
|
159
|
const cell = row[col] |
|
160
|
|
|
161
|
if (cell) { |
|
162
|
this.cells[index][col] = { |
|
163
|
...cell, |
|
164
|
enabled, |
|
165
|
} |
|
166
|
} |
|
167
|
}) |
|
168
|
} |
|
169
|
|
|
170
|
toggleRow(row: number, enabled: boolean) { |
|
171
|
if (row < 0 || row >= this.cells.length) { |
|
172
|
return |
|
173
|
} |
|
174
|
|
|
175
|
this.cells[row].forEach((cell, index) => { |
|
176
|
if (cell) { |
|
177
|
this.cells[row][index] = { |
|
178
|
...cell, |
|
179
|
enabled, |
|
180
|
} |
|
181
|
} |
|
182
|
}) |
|
183
|
} |
|
184
|
|
|
185
|
getCoordinatesByField(field: string): DataGridCoordinates | null { |
|
186
|
if (this.rowAccessors.length === 1) { |
|
187
|
const col = this.columnAccessors.indexOf(field) |
|
188
|
|
|
189
|
if (col === -1) { |
|
190
|
return null |
|
191
|
} |
|
192
|
|
|
193
|
return { row: 0, col } |
|
194
|
} |
|
195
|
|
|
196
|
for (let row = 0; row < this.rowAccessors.length; row++) { |
|
197
|
const rowAccessor = this.rowAccessors[row] |
|
198
|
|
|
199
|
if (rowAccessor === null) { |
|
200
|
continue |
|
201
|
} |
|
202
|
|
|
203
|
if (!field.startsWith(rowAccessor)) { |
|
204
|
continue |
|
205
|
} |
|
206
|
|
|
207
|
for (let column = 0; column < this.columnAccessors.length; column++) { |
|
208
|
const columnAccessor = this.columnAccessors[column] |
|
209
|
|
|
210
|
if (columnAccessor === null) { |
|
211
|
continue |
|
212
|
} |
|
213
|
|
|
214
|
const fullFieldPath = `${rowAccessor}.${columnAccessor}` |
|
215
|
|
|
216
|
if (fullFieldPath === field) { |
|
217
|
return { row, col: column } |
|
218
|
} |
|
219
|
} |
|
220
|
} |
|
221
|
|
|
222
|
return null |
|
223
|
} |
|
224
|
|
|
225
|
getRowAccessor(row: number): string | null { |
|
226
|
if (row < 0 || row >= this.cells.length) { |
|
227
|
return null |
|
228
|
} |
|
229
|
|
|
230
|
const cells = this.cells[row] |
|
231
|
|
|
232
|
const nonNullFields = cells |
|
233
|
.filter((cell): cell is GridCell<TFieldValues> => cell !== null) |
|
234
|
.map((cell) => cell.field.split(".")) |
|
235
|
|
|
236
|
if (nonNullFields.length === 0) { |
|
237
|
return null |
|
238
|
} |
|
239
|
|
|
240
|
let commonParts = nonNullFields[0] |
|
241
|
|
|
242
|
for (const segments of nonNullFields) { |
|
243
|
commonParts = commonParts.filter( |
|
244
|
(part, index) => segments[index] === part |
|
245
|
) |
|
246
|
|
|
247
|
if (commonParts.length === 0) { |
|
248
|
break |
|
249
|
} |
|
250
|
} |
|
251
|
|
|
252
|
const accessor = commonParts.join(".") |
|
253
|
|
|
254
|
if (!accessor) { |
|
255
|
return null |
|
256
|
} |
|
257
|
|
|
258
|
return accessor |
|
259
|
} |
|
260
|
|
|
261
|
public getColumnAccessor(column: number): string | null { |
|
262
|
if (column < 0 || column >= this.cells[0].length) { |
|
263
|
return null |
|
264
|
} |
|
265
|
|
|
266
|
// Extract the unique part of the field name for each row in the specified column |
|
267
|
const uniqueParts = this.cells |
|
268
|
.map((row, rowIndex) => { |
|
269
|
const cell = row[column] |
|
270
|
if (!cell) { |
|
271
|
return null |
|
272
|
} |
|
273
|
|
|
274
|
// Get the row accessor for the current row |
|
275
|
const rowAccessor = this.getRowAccessor(rowIndex) |
|
276
|
|
|
277
|
// Remove the row accessor part from the field name |
|
278
|
if (rowAccessor && cell.field.startsWith(rowAccessor + ".")) { |
|
279
|
return cell.field.slice(rowAccessor.length + 1) // Extract the part after the row accessor |
|
280
|
} |
|
281
|
|
|
282
|
return null |
|
283
|
}) |
|
284
|
.filter((part) => part !== null) // Filter out null values |
|
285
|
|
|
286
|
if (uniqueParts.length === 0) { |
|
287
|
return null |
|
288
|
} |
|
289
|
|
|
290
|
// Ensure all unique parts are the same (this should be true for well-formed data) |
|
291
|
const firstPart = uniqueParts[0] |
|
292
|
const isConsistent = uniqueParts.every((part) => part === firstPart) |
|
293
|
|
|
294
|
return isConsistent ? firstPart : null |
|
295
|
} |
|
296
|
|
|
297
|
getValidMovement( |
|
298
|
row: number, |
|
299
|
col: number, |
|
300
|
direction: string, |
|
301
|
metaKey: boolean = false |
|
302
|
): DataGridCoordinates { |
|
303
|
const [dRow, dCol] = this._getDirectionDeltas(direction) |
|
304
|
|
|
305
|
if (metaKey) { |
|
306
|
return this._getLastValidCellInDirection(row, col, dRow, dCol) |
|
307
|
} else { |
|
308
|
let newRow = row + dRow |
|
309
|
let newCol = col + dCol |
|
310
|
|
|
311
|
while (this._isValidPosition(newRow, newCol)) { |
|
312
|
if ( |
|
313
|
this.cells[newRow][newCol] !== null && |
|
314
|
this.cells[newRow][newCol]?.enabled !== false |
|
315
|
) { |
|
316
|
return { row: newRow, col: newCol } |
|
317
|
} |
|
318
|
newRow += dRow |
|
319
|
newCol += dCol |
|
320
|
} |
|
321
|
|
|
322
|
return { row, col } |
|
323
|
} |
|
324
|
} |
|
325
|
|
|
326
|
private _isValidPosition( |
|
327
|
row: number, |
|
328
|
col: number, |
|
329
|
cells?: Grid<TFieldValues> |
|
330
|
): boolean { |
|
331
|
if (!cells) { |
|
332
|
cells = this.cells |
|
333
|
} |
|
334
|
|
|
335
|
return row >= 0 && row < cells.length && col >= 0 && col < cells[0].length |
|
336
|
} |
|
337
|
|
|
338
|
private _getDirectionDeltas(direction: string): [number, number] { |
|
339
|
switch (direction) { |
|
340
|
case "ArrowUp": |
|
341
|
return [-1, 0] |
|
342
|
case "ArrowDown": |
|
343
|
return [1, 0] |
|
344
|
case "ArrowLeft": |
|
345
|
return [0, -1] |
|
346
|
case "ArrowRight": |
|
347
|
return [0, 1] |
|
348
|
default: |
|
349
|
return [0, 0] |
|
350
|
} |
|
351
|
} |
|
352
|
|
|
353
|
private _getLastValidCellInDirection( |
|
354
|
row: number, |
|
355
|
col: number, |
|
356
|
dRow: number, |
|
357
|
dCol: number |
|
358
|
): DataGridCoordinates { |
|
359
|
let newRow = row |
|
360
|
let newCol = col |
|
361
|
let lastValidRow = row |
|
362
|
let lastValidCol = col |
|
363
|
|
|
364
|
while (this._isValidPosition(newRow + dRow, newCol + dCol)) { |
|
365
|
newRow += dRow |
|
366
|
newCol += dCol |
|
367
|
if (this.cells[newRow][newCol] !== null) { |
|
368
|
lastValidRow = newRow |
|
369
|
lastValidCol = newCol |
|
370
|
} |
|
371
|
} |
|
372
|
|
|
373
|
return { |
|
374
|
row: lastValidRow, |
|
375
|
col: lastValidCol, |
|
376
|
} |
|
377
|
} |
|
378
|
|
|
379
|
private _populateCells(rows: Row<TData>[], columns: ColumnDef<TData>[]) { |
|
380
|
const cells = Array.from({ length: rows.length }, () => |
|
381
|
Array(columns.length).fill(null) |
|
382
|
) as Grid<TFieldValues> |
|
383
|
|
|
384
|
rows.forEach((row, rowIndex) => { |
|
385
|
columns.forEach((column, colIndex) => { |
|
386
|
if (!this._isValidPosition(rowIndex, colIndex, cells)) { |
|
387
|
return |
|
388
|
} |
|
389
|
|
|
390
|
const { |
|
391
|
name: _, |
|
392
|
field, |
|
393
|
type, |
|
394
|
...rest |
|
395
|
} = column.meta as InternalColumnMeta<TData, TFieldValues> |
|
396
|
|
|
397
|
const context = { |
|
398
|
row, |
|
399
|
column: { |
|
400
|
...column, |
|
401
|
meta: rest, |
|
402
|
}, |
|
403
|
} |
|
404
|
|
|
405
|
const fieldValue = field ? field(context) : null |
|
406
|
|
|
407
|
if (!fieldValue || !type) { |
|
408
|
return |
|
409
|
} |
|
410
|
|
|
411
|
cells[rowIndex][colIndex] = { |
|
412
|
field: fieldValue, |
|
413
|
type, |
|
414
|
enabled: true, |
|
415
|
} |
|
416
|
}) |
|
417
|
}) |
|
418
|
|
|
419
|
return cells |
|
420
|
} |
|
421
|
} |