-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Expand file tree
/
Copy pathindex.ts
More file actions
69 lines (59 loc) · 2.23 KB
/
index.ts
File metadata and controls
69 lines (59 loc) · 2.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import type { CalculatedColumn, CalculatedColumnOrColumnGroup, Maybe } from '../types';
export * from './colSpanUtils';
export * from './domUtils';
export * from './eventUtils';
export * from './keyboardUtils';
export * from './renderMeasuringCells';
export * from './selectedCellUtils';
export * from './styleUtils';
export const { min, max, floor, sign, abs } = Math;
export function assertIsValidKeyGetter<R, K extends React.Key>(
keyGetter: Maybe<(row: NoInfer<R>) => K>
): asserts keyGetter is (row: R) => K {
if (typeof keyGetter !== 'function') {
throw new Error('Please specify the rowKeyGetter prop to use selection');
}
}
export function getColumnWidthForMeasurement<R, SR>(
width: number | string,
{ minWidth, maxWidth }: CalculatedColumn<R, SR>
) {
const widthWithUnit = typeof width === 'number' ? `${width}px` : width;
// don't break in Node.js (SSR) and jsdom
if (typeof CSS === 'undefined') {
return widthWithUnit;
}
const hasMaxWidth = maxWidth != null;
const clampedWidth = hasMaxWidth
? `clamp(${minWidth}px, ${widthWithUnit}, ${maxWidth}px)`
: `max(${minWidth}px, ${widthWithUnit})`;
// clamp() and max() do not handle all the css grid column width values
if (isValidCSSGridColumnWidth(clampedWidth)) {
return clampedWidth;
}
if (
hasMaxWidth &&
// ignore maxWidth if it less than minWidth
maxWidth >= minWidth &&
// we do not want to use minmax with max-content as it
// can result in width being larger than max-content
widthWithUnit !== 'max-content'
) {
// We are setting maxWidth on the measuring cell but the browser only applies
// it after all the widths are calculated. This results in left over space in some cases.
const minMaxWidth = `minmax(${widthWithUnit}, ${maxWidth}px)`;
if (isValidCSSGridColumnWidth(minMaxWidth)) {
return minMaxWidth;
}
}
return isValidCSSGridColumnWidth(widthWithUnit) ? widthWithUnit : 'auto';
}
export function getHeaderCellRowSpan<R, SR>(
column: CalculatedColumnOrColumnGroup<R, SR>,
rowIdx: number
) {
return column.parent === undefined ? rowIdx : column.level - column.parent.level;
}
function isValidCSSGridColumnWidth(width: string) {
return CSS.supports('grid-template-columns', width);
}