@@ -146,79 +146,96 @@ const Form: FC<FormPropTypes> = forwardRef((props: FormPropTypes, ref: Ref<HTMLD
146
146
147
147
const classes = useStyles ( ) ;
148
148
149
+ const currentNumberOfColumns = columnsMap . get ( currentRange ) ;
150
+ const currentLabelSpan = labelSpanMap . get ( currentRange ) ;
151
+
149
152
const [ formGroups , updatedTitle ] = useMemo ( ( ) => {
150
153
const computedFormGroups : any [ ] = [ ] ;
151
154
152
155
if ( Children . count ( children ) === 1 && ! title && ( children as ReactElement ) . props . title ?. length > 0 ) {
153
156
return [ cloneElement ( children as ReactElement , { title : null } ) , ( children as ReactElement ) . props . title ] ;
154
157
}
155
158
156
- const currentColumnCount = columnsMap . get ( currentRange ) ;
159
+ const currentColumnCount = currentNumberOfColumns ;
157
160
if ( currentColumnCount === 1 ) {
158
161
return [ children , title ] ;
159
162
}
160
163
161
- // if we are running on a large desktop device, we need some special logic for splitting up the form groups
162
- const columns = createArrayOfLength ( currentColumnCount ) ;
163
- Children . toArray ( children ) . forEach ( ( child , index ) => {
164
- columns [ index % currentColumnCount ] . push ( child ) ;
165
- } ) ;
164
+ const rows = [ ] ;
165
+ const childrenArray = Children . toArray ( children ) ;
166
+ const estimatedNumberOfGroupRows = childrenArray . length / currentColumnCount ;
167
+ for ( let i = 0 ; i < estimatedNumberOfGroupRows ; i ++ ) {
168
+ rows [ i ] = childrenArray . slice ( i * currentColumnCount , i * currentColumnCount + currentColumnCount ) ;
169
+ }
166
170
167
- // no column can have more rows than the first column
168
- let totalRowCount = 1 ;
169
- for ( let i = 0 ; i < columns [ 0 ] . length ; i ++ ) {
170
- const formGroupsForRow = columns . map ( ( groups ) => groups [ i ] ) ;
171
- const childrenOfFormGroups = createArrayOfLength ( currentColumnCount ) ;
172
-
173
- formGroupsForRow . forEach ( ( formGroup , index ) => {
174
- if ( formGroup ) {
175
- computedFormGroups . push (
176
- < Title
177
- level = { TitleLevel . H5 }
178
- style = { { paddingBottom : '0.75rem' , gridColumn : 'span 12' } }
179
- key = { `title-col-${ index } -row-${ totalRowCount } ` }
180
- >
181
- { ( formGroup as ReactElement ) ?. props ?. title ?? '' }
182
- </ Title >
183
- ) ;
184
- childrenOfFormGroups [ index ] =
185
- ( ( formGroup as ReactElement ) . type as any ) . displayName === 'FormItem'
186
- ? [ formGroup ]
187
- : Children . toArray ( ( formGroup as ReactElement ) ?. props ?. children ) ;
171
+ const maxRowsPerRow : number [ ] = [ ] ;
172
+ rows . forEach ( ( rowGroup : ReactElement [ ] , rowIndex ) => {
173
+ const numberOfRowsOfEachForm = rowGroup . map ( ( row ) => {
174
+ if ( ( row . type as any ) . displayName === 'FormItem' ) {
175
+ return 1 ;
188
176
}
177
+ return Children . count ( row . props . children ) + ( row . props ?. title ?. length > 0 ? 1 : 0 ) ;
189
178
} ) ;
190
- totalRowCount ++ ;
191
179
192
- const maxChildCount = Math . max ( ...childrenOfFormGroups . map ( ( c ) => c . length ) ) ;
180
+ maxRowsPerRow [ rowIndex ] = Math . max ( ...numberOfRowsOfEachForm ) ;
181
+ } ) ;
182
+
183
+ let totalRowCount = 2 ;
184
+
185
+ rows . forEach ( ( column : ReactElement [ ] , rowIndex ) => {
186
+ const rowsForThisRow = maxRowsPerRow [ rowIndex ] ;
187
+ column . forEach ( ( cell , columnIndex ) => {
188
+ computedFormGroups . push (
189
+ < Title
190
+ level = { TitleLevel . H5 }
191
+ style = { {
192
+ paddingBottom : '0.75rem' ,
193
+ gridColumnEnd : 'span 12' ,
194
+ gridColumnStart : columnIndex * 12 + 1 ,
195
+ gridRowStart : totalRowCount
196
+ } }
197
+ key = { `title-col-${ columnIndex } -row-${ totalRowCount } ` }
198
+ >
199
+ { cell ?. props ?. title ?? '' }
200
+ </ Title >
201
+ ) ;
193
202
194
- for ( let childIndex = 0 ; childIndex < maxChildCount ; childIndex ++ ) {
195
- childrenOfFormGroups . forEach ( ( child , columnIndex ) => {
196
- if ( child [ childIndex ] ) {
197
- // @ts -ignore
203
+ for ( let i = 0 ; i < rowsForThisRow ; i ++ ) {
204
+ const itemToRender =
205
+ ( cell . type as any ) . displayName === 'FormGroup'
206
+ ? Children . toArray ( cell . props . children ) [ i ]
207
+ : ( cell . type as any ) . displayName === 'FormItem' && i === 0
208
+ ? cell
209
+ : null ;
210
+
211
+ if ( itemToRender ) {
198
212
computedFormGroups . push (
199
- cloneElement ( child [ childIndex ] as ReactElement , {
200
- key : `col-${ columnIndex } -row-${ totalRowCount } ` ,
201
- columnIndex
213
+ cloneElement ( itemToRender as ReactElement , {
214
+ key : `col-${ columnIndex } -row-${ totalRowCount + i } ` ,
215
+ columnIndex,
216
+ rowIndex : totalRowCount + i + 1 ,
217
+ labelSpan : currentLabelSpan
202
218
} )
203
219
) ;
204
220
}
205
- } ) ;
206
- totalRowCount ++ ;
221
+ }
222
+ } ) ;
223
+ totalRowCount += rowsForThisRow ;
224
+ if ( rowsForThisRow === 1 ) {
225
+ totalRowCount += 1 ;
207
226
}
208
- }
227
+ } ) ;
209
228
210
229
return [ computedFormGroups , title ] ;
211
- } , [ children , currentRange , title , columnsMap . get ( currentRange ) ] ) ;
230
+ } , [ children , currentRange , title , currentNumberOfColumns , currentLabelSpan ] ) ;
212
231
213
232
const passThroughProps = usePassThroughHtmlProps ( props ) ;
214
233
215
234
const formClassNames = StyleClassHelper . of ( classes . form ) . putIfPresent ( className ) ;
216
235
217
236
const gridStyles : CSSProperties = { } ;
218
- gridStyles [ '--ui5wcr_form_content_span' ] = 12 - labelSpanMap . get ( currentRange ) ;
219
- gridStyles [ '--ui5wcr_form_label_span' ] = labelSpanMap . get ( currentRange ) ;
220
- gridStyles [ '--ui5wcr_form_full_span' ] = `span ${ columnsMap . get ( currentRange ) * 12 } ` ;
221
- gridStyles . gridTemplateColumns = `repeat(${ columnsMap . get ( currentRange ) * 12 } , 1fr)` ;
237
+ gridStyles [ '--ui5wcr_form_content_span' ] = 12 - currentLabelSpan ;
238
+ gridStyles [ '--ui5wcr_form_label_span' ] = currentLabelSpan ;
222
239
223
240
// special case for phones or label span 12
224
241
if ( gridStyles [ '--ui5wcr_form_content_span' ] <= 0 ) {
@@ -236,6 +253,7 @@ const Form: FC<FormPropTypes> = forwardRef((props: FormPropTypes, ref: Ref<HTMLD
236
253
...gridStyles ,
237
254
...( style || { } )
238
255
} }
256
+ data-columns = { currentNumberOfColumns }
239
257
{ ...passThroughProps }
240
258
>
241
259
{ updatedTitle && (
0 commit comments