Skip to content

Commit e696f5d

Browse files
authored
[DataGrid] Add a feature to fit multiple column's width to their content (#731)
* Enable to fit multiple column width * applied prettier
1 parent 53f937e commit e696f5d

File tree

1 file changed

+26
-2
lines changed

1 file changed

+26
-2
lines changed

packages/datagrid/src/basicmousehandler.ts

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -641,8 +641,32 @@ export class BasicMouseHandler implements DataGrid.IMouseHandler {
641641
return;
642642
}
643643
}
644-
645-
grid.resizeColumn(colRegion, colIndex, null);
644+
const cs = grid.selectionModel?.currentSelection();
645+
const cv = grid.currentViewport;
646+
const rowCount = grid.selectionModel?.dataModel.rowCount('body') ?? 0;
647+
if (
648+
colRegion == 'body' &&
649+
cs != null &&
650+
cv != null &&
651+
cs.r1 == 0 &&
652+
cs.r2 == rowCount - 1
653+
) {
654+
// One or more columns are selected
655+
let c1 = Math.max(Math.min(cs.c1, cs.c2), cv.firstColumn);
656+
let c2 = Math.min(Math.max(cs.c1, cs.c2), cv.lastColumn);
657+
if (c1 <= colIndex && colIndex <= c2) {
658+
// When we double-click one of the selected column headers, resize all visible selected columns.
659+
for (let ci = c1; ci <= c2; ci++) {
660+
grid.resizeColumn(colRegion, ci, null);
661+
}
662+
} else {
663+
// When we double-click the column header outside the selection, resize only the clicked column.
664+
grid.resizeColumn(colRegion, colIndex, null);
665+
}
666+
} else {
667+
// When no columns are selected, resize only the clicked column.
668+
grid.resizeColumn(colRegion, colIndex, null);
669+
}
646670
}
647671
}
648672

0 commit comments

Comments
 (0)