@@ -2,7 +2,7 @@ export namespace Microsoft.FluentUI.Blazor.DataGrid {
22
33 interface Grid {
44 id : string ;
5- columns : any [ ] ; // or a more specific type if you have one
5+ columns : Column [ ] ; // or a more specific type if you have one
66 initialWidths : string ;
77 }
88
@@ -12,8 +12,7 @@ export namespace Microsoft.FluentUI.Blazor.DataGrid {
1212 }
1313
1414 // Use a dictionary for grids for id-based access
15- let grids : { [ id : string ] : Grid } = { } ;
16- const minWidth = 100 ;
15+ let grids : Grid [ ] = [ ] ; // { [id: string]: Grid } = {};
1716
1817 export function Initialize ( gridElement : HTMLElement , autoFocus : boolean ) {
1918 if ( gridElement === undefined || gridElement === null ) {
@@ -150,7 +149,8 @@ export namespace Microsoft.FluentUI.Blazor.DataGrid {
150149 document . body . removeEventListener ( 'click' , bodyClickHandler ) ;
151150 document . body . removeEventListener ( 'mousedown' , bodyClickHandler ) ;
152151 gridElement . removeEventListener ( 'keydown' , keyDownHandler ) ;
153- delete grids [ gridElement . id ] ;
152+ grids = grids . filter ( grid => grid . id !== gridElement . id ) ;
153+
154154 }
155155 } ;
156156 }
@@ -236,11 +236,13 @@ export namespace Microsoft.FluentUI.Blazor.DataGrid {
236236 }
237237
238238 const id = gridElement . id ;
239- grids [ id ] = {
240- id,
241- columns,
242- initialWidths,
243- } ;
239+ if ( ! grids . find ( ( grid : Grid ) => grid . id === id ) ) {
240+ grids . push ( {
241+ id,
242+ columns,
243+ initialWidths,
244+ } ) ;
245+ }
244246
245247 function setListeners ( div : HTMLElement , isRTL : boolean ) {
246248 let pageX : number | undefined , curCol : HTMLElement | undefined , curColWidth : number | undefined ;
@@ -273,7 +275,7 @@ export namespace Microsoft.FluentUI.Blazor.DataGrid {
273275 const diffX = isRTL ? ( pageX ! - e . pageX ) : ( e . pageX - pageX ! ) ;
274276 const column : Column = columns . find ( ( { header } ) => header === curCol ) ! ;
275277
276- column . size = parseInt ( Math . max ( minWidth , curColWidth ! + diffX ) as any , 10 ) + 'px' ;
278+ column . size = parseInt ( Math . max ( parseInt ( ( column . header as HTMLElement ) . style . minWidth ) , curColWidth ! + diffX ) as any , 10 ) + 'px' ;
277279
278280 columns . forEach ( ( col ) => {
279281 if ( col . size . startsWith ( 'minmax' ) ) {
@@ -345,7 +347,7 @@ export namespace Microsoft.FluentUI.Blazor.DataGrid {
345347
346348 export function ResetColumnWidths ( gridElement : HTMLElement ) {
347349 const isGrid = gridElement . classList . contains ( 'grid' ) ;
348- const grid = grids [ gridElement . id ] ;
350+ const grid = grids . find ( grid => grid . id = gridElement . id ) ;
349351 if ( ! grid ) {
350352 return ;
351353 }
@@ -370,6 +372,7 @@ export namespace Microsoft.FluentUI.Blazor.DataGrid {
370372 }
371373
372374 export function ResizeColumnDiscrete ( gridElement : HTMLElement , column : string | undefined , change : number ) {
375+ const isGrid = gridElement . classList . contains ( 'grid' ) ;
373376 const columns : any [ ] = [ ] ;
374377 let headerBeingResized : HTMLElement | null | undefined ;
375378
@@ -383,50 +386,62 @@ export namespace Microsoft.FluentUI.Blazor.DataGrid {
383386 else {
384387 headerBeingResized = gridElement . querySelector ( '.column-header[col-index="' + column + '"]' ) as HTMLElement | null ;
385388 }
386-
387- grids [ gridElement . id ] . columns . forEach ( ( column : any ) => {
389+ grids . find ( grid => grid . id = gridElement . id ) ! . columns . forEach ( ( column : any ) => {
388390 if ( column . header === headerBeingResized ) {
389- const width = headerBeingResized ! . getBoundingClientRect ( ) . width + change ;
391+ const width = headerBeingResized ! . offsetWidth + change ;
392+ //const width = headerBeingResized!.getBoundingClientRect().width + change;
390393
391394 if ( change < 0 ) {
392- column . size = Math . max ( minWidth , width ) + 'px' ;
395+ column . size = Math . max ( parseInt ( column . header . style . minWidth ) , width ) + 'px' ;
393396 }
394397 else {
395398 column . size = width + 'px' ;
396399 }
400+ column . header . style . width = column . size ;
397401 }
398- else {
402+
403+ if ( isGrid ) {
404+ // for grid we need to recalculate all columns that are minmax
399405 if ( column . size . startsWith ( 'minmax' ) ) {
400406 column . size = parseInt ( column . header . clientWidth , 10 ) + 'px' ;
401407 }
408+ columns . push ( column . size ) ;
402409 }
403- columns . push ( column . size ) ;
404410 } ) ;
405411
406- gridElement . style . gridTemplateColumns = columns . join ( ' ' ) ;
412+ if ( isGrid ) {
413+ gridElement . style . gridTemplateColumns = columns . join ( ' ' ) ;
414+ }
407415 }
408416
409417 export function ResizeColumnExact ( gridElement : HTMLElement , column : string , width : number ) {
418+ const isGrid = gridElement . classList . contains ( 'grid' ) ;
410419 const columns : any [ ] = [ ] ;
411420 let headerBeingResized = gridElement . querySelector ( '.column-header[col-index="' + column + '"]' ) as HTMLElement | null ;
412421
413422 if ( ! headerBeingResized ) {
414423 return ;
415424 }
416425
417- grids [ gridElement . id ] . columns . forEach ( ( column : any ) => {
426+ grids . find ( grid => grid . id = gridElement . id ) ! . columns . forEach ( ( column : any ) => {
418427 if ( column . header === headerBeingResized ) {
419- column . size = Math . max ( minWidth , width ) + 'px' ;
428+ column . size = Math . max ( parseInt ( column . header . style . minWidth ) , width ) + 'px' ;
429+ column . header . style . width = column . size ;
420430 }
421- else {
431+
432+ if ( isGrid ) {
433+ // for grid we need to recalculate all columns that are minmax
422434 if ( column . size . startsWith ( 'minmax' ) ) {
423435 column . size = parseInt ( column . header . clientWidth , 10 ) + 'px' ;
424436 }
437+ column . header . style . width = column . size ;
438+ columns . push ( column . size ) ;
425439 }
426- columns . push ( column . size ) ;
427440 } ) ;
428441
429- gridElement . style . gridTemplateColumns = columns . join ( ' ' ) ;
442+ if ( isGrid ) {
443+ gridElement . style . gridTemplateColumns = columns . join ( ' ' ) ;
444+ }
430445
431446 gridElement . dispatchEvent ( new CustomEvent ( 'closecolumnresize' , { bubbles : true } ) ) ;
432447 gridElement . focus ( ) ;
@@ -448,8 +463,9 @@ export namespace Microsoft.FluentUI.Blazor.DataGrid {
448463 gridElement . style . gridTemplateColumns = gridTemplateColumns ;
449464 gridElement . classList . remove ( 'auto-fit' ) ;
450465
451- if ( grids [ gridElement . id ] ) {
452- grids [ gridElement . id ] . initialWidths = gridTemplateColumns ;
466+ const grid = grids . find ( grid => grid . id = gridElement . id ) ;
467+ if ( grid ) {
468+ grid . initialWidths = gridTemplateColumns ;
453469 }
454470 }
455471
0 commit comments