11import { FunctionComponent } from 'react' ;
22import { CSSGrid } from './CSSGrid' ;
3+ import { RowCol } from './RowCol' ;
34import { Table } from './Table' ;
45import { GridRendererProps , GridRenderingType } from './types' ;
56
@@ -8,6 +9,24 @@ const getTotalGridDimensions = (rows: any[]): number => {
89 return totalColSpan ;
910} ;
1011
12+ export function getPositionnableCellClassNames (
13+ rowIndex : number ,
14+ colIndex : number ,
15+ rowLength : number ,
16+ colLength : number ,
17+ ) : 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 ( rowIndex : number , rowLength : number ) : string {
24+ return `row-${ rowIndex } ${ rowIndex == 0 ? 'first-row' : '' } ${ rowIndex == rowLength - 1 ? 'last-row' : '' } ` . replace (
25+ / \s + / g,
26+ ' ' ,
27+ ) ;
28+ }
29+
1130export const GridRenderer : FunctionComponent < GridRendererProps > = ( {
1231 cellComponent,
1332 children,
@@ -23,19 +42,29 @@ export const GridRenderer: FunctionComponent<GridRendererProps> = ({
2342
2443 if ( ! rows . length ) return null ;
2544 const totalColSpan = getTotalGridDimensions ( rows ) ;
26- if ( type === 'table' ) {
45+ if ( type === GridRenderingType . Table ) {
2746 return (
2847 < Table cellComponent = { cellComponent } rows = { rows } totalColSpan = { totalColSpan } { ...props } >
2948 { children }
3049 </ Table >
3150 ) ;
3251 }
33- // Currently the data is only returned in a nested array of rows and
34- // columns. To make use of CSS Grid we need a flat array of all of the
35- // individual cells.
36- const columns = rows . map ( ( row : { columns : any } ) => row . columns ) ;
37- const cells = [ ] . concat . apply ( [ ] , columns ) ;
38-
52+ if ( type === GridRenderingType . RowCol ) {
53+ return (
54+ < RowCol cellComponent = { cellComponent } rows = { rows } totalColSpan = { totalColSpan } { ...props } >
55+ { children }
56+ </ RowCol >
57+ ) ;
58+ }
59+ const cells = rows . reduce ( ( accumulator : any [ ] , row : { columns : any } , rowIndex : number ) => {
60+ row . columns . forEach ( ( col : any , colIndex : number ) => {
61+ accumulator . push ( {
62+ ...col ,
63+ position : [ rowIndex , colIndex , rows . length , row . columns . length ] ,
64+ } ) ;
65+ } ) ;
66+ return accumulator ;
67+ } , [ ] ) ;
3968 return (
4069 < CSSGrid cellComponent = { cellComponent } cells = { cells } totalColSpan = { totalColSpan } { ...props } >
4170 { children }
0 commit comments