Skip to content

Commit e4e2be9

Browse files
committed
feat: Remove logic to calculate colIndex/rowIndex
1 parent 3c08546 commit e4e2be9

File tree

4 files changed

+27
-77
lines changed

4 files changed

+27
-77
lines changed

src/grid/GridRenderer.tsx

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { FunctionComponent } from 'react';
22
import { CSSGrid } from './CSSGrid';
3-
import { getGridDimensions, wrapCellsInRows } from './grid-renderer-utils';
3+
import { getGridDimensions } from './grid-renderer-utils';
44
import { RowCol } from './RowCol';
55
import { Table } from './Table';
6-
import { GridCell, GridRendererProps, GridRenderingType } from './types';
6+
import { GridRow, GridCell, GridRendererProps, GridRenderingType } from './types';
77

88
export const GridRenderer: FunctionComponent<GridRendererProps> = ({
99
cellComponent,
@@ -19,13 +19,12 @@ export const GridRenderer: FunctionComponent<GridRendererProps> = ({
1919
}
2020
if (!grid.rows.length) return null;
2121
const dimensions = getGridDimensions(grid.rows);
22-
const rows = wrapCellsInRows(grid.rows);
2322

2423
if (type === GridRenderingType.Table) {
2524
return (
2625
<Table
2726
cellComponent={cellComponent}
28-
grid={rows}
27+
grid={grid.rows}
2928
dimensions={dimensions}
3029
styleForCell={styleForCell}
3130
{...props}
@@ -38,7 +37,7 @@ export const GridRenderer: FunctionComponent<GridRendererProps> = ({
3837
return (
3938
<RowCol
4039
cellComponent={cellComponent}
41-
grid={rows}
40+
grid={grid.rows}
4241
dimensions={dimensions}
4342
styleForCell={styleForCell}
4443
{...props}
@@ -48,12 +47,8 @@ export const GridRenderer: FunctionComponent<GridRendererProps> = ({
4847
);
4948
}
5049

51-
const cells = rows.reduce((accumulator: GridCell[], row: GridCell[], rowIndex: number) => {
52-
row.forEach((cell: GridCell) => {
53-
accumulator.push(cell);
54-
});
55-
return accumulator;
56-
}, []);
50+
const cells = grid.rows.reduce<GridCell[]>((memo, row) => memo.concat(row.columns), []);
51+
5752
return (
5853
<CSSGrid
5954
cellComponent={cellComponent}

src/grid/grid-renderer-utils.ts

Lines changed: 3 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { GridCell, GridDimensions, GridPosition } from './types';
1+
import { GridCell, GridDimensions } from './types';
22

33
export const getGridDimensions = (rows: any[]): GridDimensions => {
44
return {
@@ -8,8 +8,7 @@ export const getGridDimensions = (rows: any[]): GridDimensions => {
88
};
99

1010
export function getPositionnableCellClassNames(cell: GridCell, { rows, cols }: GridDimensions): string {
11-
const { rowIndex, colIndex } = cell.position;
12-
const { colspan, rowspan } = cell.layout;
11+
const { colspan, rowspan, rowIndex, colIndex } = cell.layout;
1312
const isLastRow = rowIndex + rowspan === rows;
1413
const isLastCol = colIndex + colspan === cols;
1514

@@ -18,47 +17,9 @@ export function getPositionnableCellClassNames(cell: GridCell, { rows, cols }: G
1817
} ${isLastCol ? 'last-col' : ''}`.replace(/\s+/g, ' ');
1918
}
2019

21-
export function getPositionnablRowClassNames(
22-
{ rowIndex }: Omit<GridPosition, 'colIndex'>,
23-
{ rows }: GridDimensions,
24-
): string {
20+
export function getPositionnablRowClassNames({ rowIndex }: { rowIndex: number }, { rows }: GridDimensions): string {
2521
return `row-${rowIndex} ${rowIndex == 0 ? 'first-row' : ''} ${rowIndex == rows - 1 ? 'last-row' : ''}`.replace(
2622
/\s+/g,
2723
' ',
2824
);
2925
}
30-
31-
export function wrapCellsInRows(rows: any[]): GridCell[][] {
32-
const dimensions = getGridDimensions(rows);
33-
let memoryMap: { colspan: number; rowspan: number }[] = new Array(dimensions.cols).fill({ colspan: 1, rowspan: 1 });
34-
35-
const grid = rows.map((row: any, rowIndex: number) => {
36-
let colspanOffset = 0;
37-
let rowspanOffset = 0;
38-
return row.columns.map((cell: any) => {
39-
if (memoryMap[colspanOffset]?.rowspan > 1) {
40-
rowspanOffset = memoryMap[colspanOffset].colspan;
41-
memoryMap[colspanOffset].rowspan--;
42-
} else {
43-
rowspanOffset = 0;
44-
}
45-
const positionInfos: GridPosition = {
46-
rowIndex,
47-
colIndex: colspanOffset + rowspanOffset,
48-
};
49-
memoryMap[positionInfos.colIndex] = {
50-
...cell.layout,
51-
};
52-
colspanOffset += cell.layout.colspan + rowspanOffset;
53-
return {
54-
...cell,
55-
position: positionInfos,
56-
layout: {
57-
colspan: cell.layout.colspan,
58-
rowspan: cell.layout.rowspan,
59-
},
60-
};
61-
});
62-
});
63-
return grid;
64-
}

src/grid/types.ts

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,25 +13,23 @@ export type GridDimensions = {
1313
};
1414

1515
export type GridCell = {
16-
position: GridPosition;
17-
rowIndex: number;
18-
colIndex: number;
1916
layout: {
2017
rowspan: number;
2118
colspan: number;
19+
rowIndex: number;
20+
colIndex: number;
2221
};
23-
} & any;
22+
};
2423

25-
export type GridPosition = {
26-
rowIndex: number;
27-
colIndex: number;
24+
export type GridRow = {
25+
columns: GridCell[];
2826
};
2927

3028
export interface GridRendererProps {
3129
cellComponent: React.FunctionComponent<{ cell: any; dimensions: GridDimensions }>;
3230
type: GridRenderingType;
3331
grid: {
34-
rows: any;
32+
rows: GridRow[];
3533
};
3634
style?: React.CSSProperties;
3735
children?: FunctionComponent<any>;
@@ -49,7 +47,7 @@ export interface CSSGridProps {
4947

5048
export interface TableGridProps {
5149
cellComponent: React.FunctionComponent<{ cell: any; dimensions: GridDimensions }>;
52-
grid: GridCell[][];
50+
grid: GridRow[];
5351
children?: FunctionComponent<any>;
5452
dimensions: GridDimensions;
5553
style?: React.CSSProperties;
@@ -58,7 +56,7 @@ export interface TableGridProps {
5856

5957
export interface RowColGridProps {
6058
cellComponent: React.FunctionComponent<{ cell: any; dimensions: GridDimensions }>;
61-
grid: GridCell[][];
59+
grid: GridRow[];
6260
children?: FunctionComponent<any>;
6361
dimensions: GridDimensions;
6462
style?: React.CSSProperties;

tests/gridcellposition.test.js

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
const { createCatalogueFetcher, createClient, catalogueFetcherGraphqlBuilder } = require('@crystallize/js-api-client');
2-
const { wrapCellsInRows } = require('../dist/grid/grid-renderer-utils');
32

43
test('Get a Grid and check the position', async () => {
54
const CrystallizeClient = createClient({
@@ -20,6 +19,8 @@ test('Get a Grid and check the position', async () => {
2019
layout: {
2120
rowspan: true,
2221
colspan: true,
22+
rowIndex: true,
23+
colIndex: true,
2324
},
2425
item: {
2526
__on: [builder.onItem()],
@@ -30,17 +31,12 @@ test('Get a Grid and check the position', async () => {
3031
};
3132
const grid = (await fetch(query)).grid;
3233

33-
expect(grid.rows[0].columns[0].layout.rowspan).toBe(1);
34-
expect(grid.rows[1].columns[0].layout.rowspan).toBe(1);
35-
expect(grid.rows[5].columns[0].layout.rowspan).toBe(2);
36-
const gridCells = wrapCellsInRows(grid.rows);
37-
38-
const check = ([cellRowIndex, cellColIndex], [rowIndex, colIndex]) => {
39-
expect(gridCells[cellRowIndex][cellColIndex].position.rowIndex).toBe(rowIndex);
40-
expect(gridCells[cellRowIndex][cellColIndex].position.colIndex).toBe(colIndex);
41-
};
42-
43-
check([1, 0], [1, 0]);
44-
check([2, 0], [2, 0]);
45-
check([6, 0], [6, 2]);
34+
for (const row of grid.rows) {
35+
for (const cell of row.columns) {
36+
expect(typeof cell.layout.colspan).toBe('number');
37+
expect(typeof cell.layout.rowspan).toBe('number');
38+
expect(typeof cell.layout.colIndex).toBe('number');
39+
expect(typeof cell.layout.rowIndex).toBe('number');
40+
}
41+
}
4642
});

0 commit comments

Comments
 (0)