11import { FunctionComponent } from 'react' ;
22import { CSSGrid } from './CSSGrid' ;
3+ import { getGridDimensions , wrapCellsInRows } from './grid-renderer-utils' ;
34import { RowCol } from './RowCol' ;
45import { Table } from './Table' ;
5- import { GridPositionnable , GridRendererProps , GridRenderingType } from './types' ;
6-
7- const getTotalGridDimensions = ( rows : any [ ] ) : number => {
8- const totalColSpan = rows [ 0 ] . columns . reduce ( ( acc : number , col : any ) => acc + col . layout . colspan , 0 ) ;
9- return totalColSpan ;
10- } ;
11-
12- export function getPositionnableCellClassNames ( {
13- rowIndex,
14- colIndex,
15- rowLength,
16- colLength,
17- } : GridPositionnable ) : string {
18- return `cell-${ rowIndex } -${ colIndex } ${ rowIndex == 0 ? 'first-row' : '' } ${ colIndex == 0 ? 'first-col' : '' } ${
19- rowIndex === rowLength - 1 ? 'last-row' : ''
20- } ${ colIndex === colLength - 1 ? 'last-col' : '' } `. replace ( / \s + / g, ' ' ) ;
21- }
22-
23- export function getPositionnablRowClassNames ( {
24- rowIndex,
25- rowLength,
26- } : Omit < GridPositionnable , 'colIndex' | 'colLength' > ) : string {
27- return `row-${ rowIndex } ${ rowIndex == 0 ? 'first-row' : '' } ${ rowIndex == rowLength - 1 ? 'last-row' : '' } ` . replace (
28- / \s + / g,
29- ' ' ,
30- ) ;
31- }
6+ import { GridCell , GridRendererProps , GridRenderingType } from './types' ;
327
338export const GridRenderer : FunctionComponent < GridRendererProps > = ( {
349 cellComponent,
@@ -42,16 +17,16 @@ export const GridRenderer: FunctionComponent<GridRendererProps> = ({
4217 console . error ( '@crystallize/grid-renderer: missing ´cellComponent` or children function' ) ;
4318 return null ;
4419 }
45- const { rows } = grid ;
20+ if ( ! grid . rows . length ) return null ;
21+ const dimensions = getGridDimensions ( grid . rows ) ;
22+ const rows = wrapCellsInRows ( grid . rows ) ;
4623
47- if ( ! rows . length ) return null ;
48- const totalColSpan = getTotalGridDimensions ( rows ) ;
4924 if ( type === GridRenderingType . Table ) {
5025 return (
5126 < Table
5227 cellComponent = { cellComponent }
53- rows = { rows }
54- totalColSpan = { totalColSpan }
28+ grid = { rows }
29+ dimensions = { dimensions }
5530 styleForCell = { styleForCell }
5631 { ...props }
5732 >
@@ -63,29 +38,27 @@ export const GridRenderer: FunctionComponent<GridRendererProps> = ({
6338 return (
6439 < RowCol
6540 cellComponent = { cellComponent }
66- rows = { rows }
67- totalColSpan = { totalColSpan }
41+ grid = { rows }
42+ dimensions = { dimensions }
6843 styleForCell = { styleForCell }
6944 { ...props }
7045 >
7146 { children }
7247 </ RowCol >
7348 ) ;
7449 }
75- const cells = rows . reduce ( ( accumulator : any [ ] , row : { columns : any } , rowIndex : number ) => {
76- row . columns . forEach ( ( col : any , colIndex : number ) => {
77- accumulator . push ( {
78- ...col ,
79- position : [ rowIndex , colIndex , rows . length , row . columns . length ] ,
80- } ) ;
50+
51+ const cells = rows . reduce ( ( accumulator : GridCell [ ] , row : GridCell [ ] , rowIndex : number ) => {
52+ row . forEach ( ( cell : GridCell ) => {
53+ accumulator . push ( cell ) ;
8154 } ) ;
8255 return accumulator ;
8356 } , [ ] ) ;
8457 return (
8558 < CSSGrid
8659 cellComponent = { cellComponent }
8760 cells = { cells }
88- totalColSpan = { totalColSpan }
61+ dimensions = { dimensions }
8962 styleForCell = { styleForCell }
9063 { ...props }
9164 >
0 commit comments