Skip to content

Commit 38e8d2f

Browse files
committed
Initial commit
1 parent 8a6bb28 commit 38e8d2f

5 files changed

Lines changed: 44 additions & 13 deletions

File tree

src/HeaderCell.tsx

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import { css } from '@linaria/core';
33

44
import { useRovingTabIndex } from './hooks';
55
import {
6-
clampColumnWidth,
76
getCellClassname,
87
getCellStyle,
98
getHeaderCellRowSpan,
@@ -124,8 +123,7 @@ export default function HeaderCell<R, SR>({
124123

125124
function onPointerMove(event: PointerEvent) {
126125
const { width, right, left } = headerCell.getBoundingClientRect();
127-
let newWidth = isRtl ? right + offset - event.clientX : event.clientX + offset - left;
128-
newWidth = clampColumnWidth(newWidth, column);
126+
const newWidth = isRtl ? right + offset - event.clientX : event.clientX + offset - left;
129127
if (width > 0 && newWidth !== width) {
130128
onColumnResize(column, newWidth);
131129
}

src/hooks/useColumnWidths.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { useLayoutEffect, useRef, useState } from 'react';
22

3+
import { clampColumnWidth, getColumnWidthForMeasurement } from '../utils';
34
import type { CalculatedColumn, StateSetter } from '../types';
45
import type { DataGridProps } from '../DataGrid';
56

@@ -25,16 +26,18 @@ export function useColumnWidths<R, SR>(
2526
const newTemplateColumns = [...templateColumns];
2627
const columnsToMeasure: string[] = [];
2728

28-
for (const { key, idx, width } of viewportColumns) {
29+
for (const column of viewportColumns) {
30+
const { key, idx, width } = column;
2931
if (key === columnToAutoResize) {
30-
newTemplateColumns[idx] = 'max-content';
32+
newTemplateColumns[idx] = getColumnWidthForMeasurement('max-content', column);
3133
columnsToMeasure.push(key);
3234
} else if (
3335
typeof width === 'string' &&
3436
(ignorePreviouslyMeasuredColumns || !measuredColumnWidths.has(key)) &&
37+
// If the column is resized by the user, we don't want to measure it again
3538
!resizedColumnWidths.has(key)
3639
) {
37-
newTemplateColumns[idx] = width;
40+
newTemplateColumns[idx] = getColumnWidthForMeasurement(width, column);
3841
columnsToMeasure.push(key);
3942
}
4043
}
@@ -109,12 +112,13 @@ export function useColumnWidths<R, SR>(
109112
}
110113

111114
if (typeof nextWidth === 'number') {
115+
const clampedNextWidth = clampColumnWidth(nextWidth, column);
112116
setResizedColumnWidths((resizedColumnWidths) => {
113117
const newResizedColumnWidths = new Map(resizedColumnWidths);
114-
newResizedColumnWidths.set(resizingKey, nextWidth);
118+
newResizedColumnWidths.set(resizingKey, clampedNextWidth);
115119
return newResizedColumnWidths;
116120
});
117-
onColumnResize?.(column, nextWidth);
121+
onColumnResize?.(column, clampedNextWidth);
118122
} else {
119123
setColumnToAutoResize(resizingKey);
120124
}

src/utils/index.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export function assertIsValidKeyGetter<R, K extends React.Key>(
2121
export function clampColumnWidth<R, SR>(
2222
width: number,
2323
{ minWidth, maxWidth }: CalculatedColumn<R, SR>
24-
): number {
24+
) {
2525
width = max(width, minWidth);
2626

2727
// ignore maxWidth if it less than minWidth
@@ -32,6 +32,24 @@ export function clampColumnWidth<R, SR>(
3232
return width;
3333
}
3434

35+
export function getColumnWidthForMeasurement<R, SR>(
36+
width: string,
37+
{ minWidth, maxWidth }: CalculatedColumn<R, SR>
38+
) {
39+
if (
40+
maxWidth != null &&
41+
// ignore maxWidth if it less than minWidth
42+
maxWidth >= minWidth
43+
) {
44+
// fit-content = min(max-width, max(auto, ${maxWidth}px))
45+
return width === 'max-content'
46+
? `fit-content(${maxWidth}px)`
47+
: `minmax(${width}, ${maxWidth}px)`;
48+
}
49+
50+
return width;
51+
}
52+
3553
export function getHeaderCellRowSpan<R, SR>(
3654
column: CalculatedColumnOrColumnGroup<R, SR>,
3755
rowIdx: number

website/routes/CommonFeatures.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ function getColumns(
117117
{
118118
key: 'country',
119119
name: 'Country',
120+
maxWidth: 150,
120121
renderEditCell: (p) => (
121122
<select
122123
autoFocus

website/routes/ContextMenu.tsx

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ function createRows(): Row[] {
3636
for (let i = 1; i < 1000; i++) {
3737
rows.push({
3838
id: i,
39+
col2: '111111111111111111111111111111111111ssssssssssssssssssssssssssssssssssssssss1',
3940
product: faker.commerce.productName(),
4041
price: faker.commerce.price()
4142
});
@@ -45,11 +46,20 @@ function createRows(): Row[] {
4546
}
4647

4748
const columns: readonly Column<Row>[] = [
48-
{ key: 'id', name: 'ID' },
49-
{ key: 'product', name: 'Product' },
50-
{ key: 'price', name: 'Price' }
49+
{
50+
key: 'col1',
51+
name: 'col1',
52+
width: 100
53+
},
54+
{
55+
key: 'col2',
56+
name: 'col2',
57+
minWidth: 100,
58+
width: 200,
59+
maxWidth: 400,
60+
resizable: true
61+
}
5162
];
52-
5363
function rowKeyGetter(row: Row) {
5464
return row.id;
5565
}

0 commit comments

Comments
 (0)