@@ -30,39 +30,130 @@ export class B2bGridRowComponent {
30
30
this . adjustColumnFlex ( ) ;
31
31
}
32
32
33
+ private calculateRowsAndsSpaceForColumnsWithSpan (
34
+ columns : HTMLElement [ ] ,
35
+ currentRowTotal : number ,
36
+ rows : HTMLElement [ ] [ ] ,
37
+ currentRow : HTMLElement [ ] ,
38
+ ) {
39
+ columns . forEach ( column => {
40
+ let span = parseInt ( column . getAttribute ( 'span' ) , 10 ) ;
41
+
42
+ if ( currentRowTotal + span > 12 ) {
43
+ rows . push ( currentRow ) ;
44
+ currentRow = [ ] ;
45
+ currentRowTotal = 0 ;
46
+ }
47
+
48
+ currentRow . push ( column ) ;
49
+ currentRowTotal += span ;
50
+ } ) ;
51
+
52
+ if ( currentRow . length > 0 ) {
53
+ rows . push ( currentRow ) ;
54
+ }
55
+ }
56
+
33
57
private adjustColumnFlex ( ) {
34
58
const columns = this . hostElement . querySelectorAll ( ':scope > b2b-grid-col' ) ;
35
59
let totalSpan = 0 ;
36
60
let columnsWithoutSpan : HTMLElement [ ] = [ ] ;
61
+ let columnsWithSpan : HTMLElement [ ] = [ ] ;
37
62
38
63
columns . forEach ( ( column : any ) => {
39
64
const span = column . getAttribute ( 'span' ) ;
40
65
if ( span ) {
41
66
totalSpan += parseInt ( span , 10 ) ;
67
+ columnsWithSpan . push ( column ) ;
42
68
} else {
43
69
columnsWithoutSpan . push ( column ) ;
44
70
}
45
71
} ) ;
46
72
47
- const remainingSpan = 12 - totalSpan ;
48
-
49
- if ( remainingSpan == 0 ) {
50
- columnsWithoutSpan . forEach ( column => {
51
- column . style . width = '100%' ;
52
- } ) ;
73
+ if ( totalSpan > 12 && columnsWithoutSpan . length === 0 ) {
74
+ this . handleOverflowingColumns ( columnsWithSpan ) ;
53
75
return ;
54
76
}
55
77
56
78
if ( columnsWithoutSpan . length > 0 ) {
57
- const spanPerColumn = Math . floor (
58
- remainingSpan / columnsWithoutSpan . length ,
79
+ this . handleRowsWithColumnsWithoutSpan (
80
+ columnsWithoutSpan ,
81
+ columnsWithSpan ,
82
+ totalSpan ,
59
83
) ;
84
+ return ;
85
+ }
86
+ }
87
+
88
+ private handleOverflowingColumns ( columns : HTMLElement [ ] ) {
89
+ let rows : HTMLElement [ ] [ ] = [ ] ;
90
+ let currentRow : HTMLElement [ ] = [ ] ;
91
+ let currentRowTotal = 0 ;
92
+ this . calculateRowsAndsSpaceForColumnsWithSpan (
93
+ columns ,
94
+ currentRowTotal ,
95
+ rows ,
96
+ currentRow ,
97
+ ) ;
98
+
99
+ rows . forEach ( row => {
100
+ row . forEach ( column => {
101
+ let span = parseInt ( column . getAttribute ( 'span' ) , 10 ) ;
102
+ let widthPercentage = ( span / 12 ) * 100 ;
60
103
61
- columnsWithoutSpan . forEach ( column => {
62
- column . setAttribute ( 'span' , spanPerColumn . toString ( ) ) ;
63
- column . style . width = `${ ( spanPerColumn / 12 ) * 100 } %` ;
104
+ column . style . flex = `0 0 calc(${ widthPercentage } % - ${ this . columnGap } px)` ;
105
+ column . style . maxWidth = `calc(${ widthPercentage } % - ${ this . columnGap } px)` ;
64
106
} ) ;
107
+ } ) ;
108
+ }
109
+
110
+ private handleRowsWithColumnsWithoutSpan (
111
+ columnsWithoutSpan : HTMLElement [ ] ,
112
+ columnsWithSpan : HTMLElement [ ] ,
113
+ totalSpan : number ,
114
+ ) {
115
+ let remainingSpan = 12 - totalSpan <= 0 ? 12 : 12 - totalSpan ;
116
+ let rows : HTMLElement [ ] [ ] = [ ] ;
117
+ let currentRow : HTMLElement [ ] = [ ] ;
118
+ let currentRowTotal = 0 ;
119
+
120
+ this . calculateRowsAndsSpaceForColumnsWithSpan (
121
+ columnsWithSpan ,
122
+ currentRowTotal ,
123
+ rows ,
124
+ currentRow ,
125
+ ) ;
126
+
127
+ columnsWithoutSpan . forEach ( column => {
128
+ let spanPerColumn = Math . max (
129
+ 1 ,
130
+ Math . floor ( remainingSpan / columnsWithoutSpan . length ) ,
131
+ ) ;
132
+
133
+ if ( currentRowTotal + spanPerColumn > 12 ) {
134
+ rows . push ( currentRow ) ;
135
+ currentRow = [ ] ;
136
+ currentRowTotal = 0 ;
137
+ }
138
+
139
+ column . setAttribute ( 'span' , spanPerColumn . toString ( ) ) ;
140
+ currentRow . push ( column ) ;
141
+ currentRowTotal += spanPerColumn ;
142
+ } ) ;
143
+
144
+ if ( currentRow . length > 0 ) {
145
+ rows . push ( currentRow ) ;
65
146
}
147
+
148
+ rows . forEach ( row => {
149
+ row . forEach ( column => {
150
+ let span = parseInt ( column . getAttribute ( 'span' ) , 10 ) || 1 ;
151
+ let widthPercentage = ( span / 12 ) * 100 ;
152
+
153
+ column . style . flex = `0 0 calc(${ widthPercentage } % - ${ this . columnGap } px)` ;
154
+ column . style . maxWidth = `calc(${ widthPercentage } % - ${ this . columnGap } px)` ;
155
+ } ) ;
156
+ } ) ;
66
157
}
67
158
68
159
render ( ) {
0 commit comments