@aircall/ds 0.38.0 → 0.40.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/{data-table-DdK-5YI9.d.ts → data-table-AHgs5bxe.d.ts} +57 -25
- package/dist/{data-table-fPncHG6B.js → data-table-DD7m3puW.js} +308 -15
- package/dist/data-table.d.ts +1 -1
- package/dist/data-table.js +1 -1
- package/dist/globals.css +1 -1
- package/dist/index.d.ts +11 -7
- package/dist/index.js +18 -4
- package/package.json +5 -5
|
@@ -2,12 +2,20 @@ import * as React from "react";
|
|
|
2
2
|
import { ColumnDef, ColumnPinningState, OnChangeFn, RowData, SortingState, VisibilityState } from "@tanstack/react-table";
|
|
3
3
|
//#region src/components/data-table.d.ts
|
|
4
4
|
/**
|
|
5
|
-
* Column definition for `DataTable`.
|
|
6
|
-
* `
|
|
5
|
+
* Column definition for `DataTable`. TanStack Table's own `ColumnDef` plus the `flex` field
|
|
6
|
+
* `DataTable` adds, owned by `@aircall/ds` — author against this type (not
|
|
7
7
|
* `@tanstack/react-table` directly), since `@tanstack/react-table` is a
|
|
8
8
|
* regular `@aircall/ds` dependency and not part of your own install.
|
|
9
9
|
*/
|
|
10
|
-
type ColumnDef$1<TData extends RowData, TValue = unknown> = ColumnDef<TData, TValue
|
|
10
|
+
type ColumnDef$1<TData extends RowData, TValue = unknown> = ColumnDef<TData, TValue> & {
|
|
11
|
+
/**
|
|
12
|
+
* Share of the free space this column takes, relative to the other flexible columns.
|
|
13
|
+
* `2` grows twice as fast as the default of `1`. React Spectrum spells this `width="2fr"`.
|
|
14
|
+
*
|
|
15
|
+
* Ignored on a column that declares a `size`, which is static by definition.
|
|
16
|
+
*/
|
|
17
|
+
flex?: number;
|
|
18
|
+
};
|
|
11
19
|
/** Sorting state for `DataTable`'s `sorting` / `onSortingChange` props. Bound alias of TanStack Table's `SortingState`. */
|
|
12
20
|
type SortingState$1 = SortingState;
|
|
13
21
|
/** Change handler used by `DataTable`'s controlled props (e.g. `onSortingChange`). Bound alias of TanStack Table's `OnChangeFn`. */
|
|
@@ -25,6 +33,29 @@ interface DataTableProps<TData> {
|
|
|
25
33
|
* footer cells aligned under their columns.
|
|
26
34
|
*
|
|
27
35
|
* Pin columns to the left/right edge with the `columnPinning` prop.
|
|
36
|
+
*
|
|
37
|
+
* ### Width
|
|
38
|
+
*
|
|
39
|
+
* A column is either static or flexible, and `size` is what decides which
|
|
40
|
+
* (mirroring React Spectrum's `TableView`):
|
|
41
|
+
*
|
|
42
|
+
* - **`size: 40`** — static. Exactly 40px, at every container width. It never
|
|
43
|
+
* grows to fill space and never shrinks to make room. Use it for columns
|
|
44
|
+
* whose content has a known width: a checkbox, an icon, a row-actions menu.
|
|
45
|
+
* - **no `size`** — flexible. Takes an equal share of whatever the static
|
|
46
|
+
* columns leave over (Spectrum's `1fr`). This is the default, so leave a
|
|
47
|
+
* column undeclared unless you mean a fixed width.
|
|
48
|
+
* - **`flex: 2`** — flexible with twice the share of a default column.
|
|
49
|
+
* - **`minSize`** — floor. On a flexible column it defaults to 75, and when the
|
|
50
|
+
* floors sum past the container the table keeps every column at its minimum
|
|
51
|
+
* and scrolls horizontally rather than crushing them. It also bounds a
|
|
52
|
+
* static column, so `{ size: 40, minSize: 100 }` renders at 100.
|
|
53
|
+
* - **`maxSize`** — cap; a flexible column's surplus goes to the others. This
|
|
54
|
+
* bounds a static column too, so `{ size: 400, maxSize: 300 }` renders at 300.
|
|
55
|
+
*
|
|
56
|
+
* Declaring `size` on *every* column leaves nothing to absorb the difference,
|
|
57
|
+
* so a table narrower than its container ends short of the right edge. Keep at
|
|
58
|
+
* least one column flexible.
|
|
28
59
|
*/
|
|
29
60
|
columns: ColumnDef$1<TData, unknown>[];
|
|
30
61
|
/** Data array to render. With server-side sorting/pagination, this is the already-prepared page. */
|
|
@@ -55,9 +86,19 @@ interface DataTableProps<TData> {
|
|
|
55
86
|
onSortingChange?: OnChangeFn$1<SortingState$1>;
|
|
56
87
|
/**
|
|
57
88
|
* Enables interactive column resizing. Each resizable header gets a drag
|
|
58
|
-
* handle on its right edge
|
|
59
|
-
*
|
|
60
|
-
*
|
|
89
|
+
* handle on its right edge, bounded by the column's `minSize`/`maxSize`. Set
|
|
90
|
+
* `enableResizing: false` on a column def to pin it (e.g. an actions column).
|
|
91
|
+
*
|
|
92
|
+
* Dragging turns that column static at its new width. The columns before it
|
|
93
|
+
* freeze at their current widths and the ones after it stay flexible and
|
|
94
|
+
* absorb the change, so the drag reads as moving a divider rather than
|
|
95
|
+
* reflowing the whole table (as in React Spectrum's `resizeColumnWidth`).
|
|
96
|
+
*
|
|
97
|
+
* Because each drag freezes what is behind the handle, repeated resizing turns
|
|
98
|
+
* the table progressively static, and once nothing is left flexible it stops
|
|
99
|
+
* filling its container. That is inherent to the model rather than a bug:
|
|
100
|
+
* Spectrum notes the same, that resizing "will convert all columns to the left
|
|
101
|
+
* to static widths, so it will cut down on the number of FR columns".
|
|
61
102
|
*/
|
|
62
103
|
enableColumnResizing?: boolean;
|
|
63
104
|
/**
|
|
@@ -74,10 +115,10 @@ interface DataTableProps<TData> {
|
|
|
74
115
|
/**
|
|
75
116
|
* Controlled column pinning state — arrays of column ids stuck to the left
|
|
76
117
|
* or right edge while the table scrolls horizontally, using the same
|
|
77
|
-
* column ids passed via `columns`.
|
|
78
|
-
*
|
|
79
|
-
*
|
|
80
|
-
*
|
|
118
|
+
* column ids passed via `columns`. Pin offsets are derived from the resolved
|
|
119
|
+
* column widths, so no special sizing setup is needed. When any column is
|
|
120
|
+
* pinned and selection is enabled, the built-in selection column is also
|
|
121
|
+
* auto-pinned to the left.
|
|
81
122
|
*
|
|
82
123
|
* Set once from wherever pinning is decided (e.g. hardcoded in the column
|
|
83
124
|
* schema, or computed once from screen size) — `DataTable` has no
|
|
@@ -150,25 +191,16 @@ interface DataTableProps<TData> {
|
|
|
150
191
|
* virtualizer can track scroll position. Without it, `scrollTop` stays 0 and
|
|
151
192
|
* virtualization has no effect.
|
|
152
193
|
*
|
|
153
|
-
*
|
|
154
|
-
*
|
|
155
|
-
* content. Columns with no `size` fall back to TanStack's 150px default — so
|
|
156
|
-
* a content-sized table (e.g. a narrow icon column) will visibly snap to
|
|
157
|
-
* 150px when you enable this. Set an explicit `size` on each column def to
|
|
158
|
-
* preserve the intended widths.
|
|
194
|
+
* Needs no sizing setup: the virtualizer requires stable column widths, and
|
|
195
|
+
* widths are already resolved from the column defs in every mode.
|
|
159
196
|
*/
|
|
160
197
|
virtualizeRows?: boolean;
|
|
161
198
|
/**
|
|
162
|
-
*
|
|
163
|
-
*
|
|
164
|
-
*
|
|
165
|
-
* should be truncated by the cell (e.g. a `truncate` wrapper).
|
|
199
|
+
* @deprecated No longer does anything. Column widths are resolved from the column defs in every
|
|
200
|
+
* mode, so the layout is always stable — which is all this prop ever bought. Safe to remove; it
|
|
201
|
+
* is still accepted so existing call sites keep compiling.
|
|
166
202
|
*
|
|
167
|
-
*
|
|
168
|
-
* it adds no drag handles, and unlike `virtualizeRows` it needs no
|
|
169
|
-
* `fillHeight`. Use it for content-stable lists where columns shouldn't
|
|
170
|
-
* reflow. (`enableColumnResizing`, `virtualizeRows`, and `columnPinning`
|
|
171
|
-
* imply this too.)
|
|
203
|
+
* See the `columns` prop for how widths are declared.
|
|
172
204
|
*/
|
|
173
205
|
fixedLayout?: boolean;
|
|
174
206
|
className?: string;
|
|
@@ -856,6 +856,39 @@ const TableRow = React.forwardRef((componentProps, forwardRef) => {
|
|
|
856
856
|
}, props));
|
|
857
857
|
});
|
|
858
858
|
TableRow.displayName = "TableRow";
|
|
859
|
+
/**
|
|
860
|
+
* Truncation for DATA cells only. Header cells deliberately do not clip - see below.
|
|
861
|
+
*
|
|
862
|
+
* Column widths are authoritative - `DataTable` solves them and renders the table at exactly their
|
|
863
|
+
* total under `table-layout: fixed` - so content that does not fit has to clip. Without
|
|
864
|
+
* `overflow-hidden` it spills across the next column instead of being cut off.
|
|
865
|
+
*
|
|
866
|
+
* `min-w-0` on the direct child is what lets a flex wrapper (`<div className="flex gap-2">`, the
|
|
867
|
+
* common cell shape) shrink below its content width; a flex item's `min-width: auto` would
|
|
868
|
+
* otherwise refuse to and push the overflow out.
|
|
869
|
+
*
|
|
870
|
+
* WHY NOT ON `TableHead`: the header row is where the interactive chrome lives, and clipping it
|
|
871
|
+
* cuts focus rings. `DataTable`'s sortable header renders a `Button` with `-ml-2` against this
|
|
872
|
+
* cell's `px-2`, and the DS focus ring is `focus-visible:ring-3`, drawn 3px OUTSIDE the border box.
|
|
873
|
+
* Measured headroom between the button's left edge and the clip boundary:
|
|
874
|
+
*
|
|
875
|
+
* - first column: 8px, because `first:pl-4` gives it 16px of padding. The ring clears.
|
|
876
|
+
* - every other column: 0px. The ring is cut.
|
|
877
|
+
*
|
|
878
|
+
* So it was every sortable column except the first, for keyboard users only. Stated precisely
|
|
879
|
+
* because the 0px figure was measured on a non-first column and does not generalise.
|
|
880
|
+
*
|
|
881
|
+
* React Spectrum solves this with `overflow: hidden` plus `padding: 4px; margin: -4px` so the ring
|
|
882
|
+
* escapes the clip, but that is not portable to a `display: table-cell`, where `margin` is inert.
|
|
883
|
+
* The alternatives all cost something visible: shrinking the negative margin misaligns sortable
|
|
884
|
+
* against non-sortable headers, dropping the button's padding changes the ghost hover rect, and an
|
|
885
|
+
* inset ring would make these the only focus rings in the DS drawn inwards.
|
|
886
|
+
*
|
|
887
|
+
* So headers keep the behaviour they had before column sizing existed: no clip. Header labels are
|
|
888
|
+
* short by nature, and a long one in a narrow column spills exactly as it always has. Data cells
|
|
889
|
+
* are where long content actually appears, and none of them carry a negative margin.
|
|
890
|
+
*/
|
|
891
|
+
const truncatingCell = "overflow-hidden text-ellipsis [&>*]:min-w-0";
|
|
859
892
|
/** A header cell. Renders a `<th>` with medium-weight, left-aligned text. */
|
|
860
893
|
const TableHead = React.forwardRef((componentProps, forwardRef) => {
|
|
861
894
|
const { className } = componentProps, props = _objectWithoutProperties(componentProps, _excluded6);
|
|
@@ -872,10 +905,130 @@ const TableCell = React.forwardRef((componentProps, forwardRef) => {
|
|
|
872
905
|
return /* @__PURE__ */ jsx("td", _objectSpread2({
|
|
873
906
|
ref: forwardRef,
|
|
874
907
|
"data-slot": "table-cell",
|
|
875
|
-
className: cn("p-2 first:pl-4 align-middle whitespace-nowrap [&:has([role=checkbox])]:pr-0", className)
|
|
908
|
+
className: cn("p-2 first:pl-4 align-middle whitespace-nowrap [&:has([role=checkbox])]:pr-0", truncatingCell, className)
|
|
876
909
|
}, props));
|
|
877
910
|
});
|
|
878
911
|
TableCell.displayName = "TableCell";
|
|
912
|
+
/**
|
|
913
|
+
* Resolves every column to a whole-pixel width.
|
|
914
|
+
*
|
|
915
|
+
* Static columns are frozen at their `size`. The remaining space is distributed over the flexible
|
|
916
|
+
* columns in proportion to their weight, clamped to each column's `minSize` / `maxSize`; a column
|
|
917
|
+
* that hits a bound freezes there and the loop redistributes what is left over the rest.
|
|
918
|
+
*
|
|
919
|
+
* When the floors sum past `availableWidth` the total simply exceeds it - the caller renders a
|
|
920
|
+
* wider table inside a scrolling container. There is no separate overflow mode; it falls out of
|
|
921
|
+
* the min clamping, exactly as in React Spectrum.
|
|
922
|
+
*
|
|
923
|
+
* @param availableWidth Visible width the columns divide up, in px. Values `<= 0` (an unmeasured
|
|
924
|
+
* container) resolve every column to its floor, so the first paint is sane rather than collapsed.
|
|
925
|
+
* @param columns Declared sizing per column, in render order.
|
|
926
|
+
* @returns Widths in the same order as `columns`, summing to `max(availableWidth, sum of floors)`.
|
|
927
|
+
*/
|
|
928
|
+
function resolveColumnWidths(availableWidth, columns) {
|
|
929
|
+
if (columns.length === 0) return [];
|
|
930
|
+
const available = Math.max(0, Math.floor(availableWidth));
|
|
931
|
+
let hasUnfrozen = false;
|
|
932
|
+
const items = columns.map((column) => {
|
|
933
|
+
var _column$minSize, _column$maxSize, _column$flex;
|
|
934
|
+
const isStatic = column.size != null;
|
|
935
|
+
const min = (_column$minSize = column.minSize) !== null && _column$minSize !== void 0 ? _column$minSize : isStatic ? 0 : 75;
|
|
936
|
+
const max = (_column$maxSize = column.maxSize) !== null && _column$maxSize !== void 0 ? _column$maxSize : Number.MAX_SAFE_INTEGER;
|
|
937
|
+
const baseSize = isStatic ? column.size : 0;
|
|
938
|
+
const flex = isStatic ? 0 : Math.max(0, (_column$flex = column.flex) !== null && _column$flex !== void 0 ? _column$flex : 1);
|
|
939
|
+
const frozen = isStatic || flex <= 0;
|
|
940
|
+
const item = {
|
|
941
|
+
frozen,
|
|
942
|
+
baseSize,
|
|
943
|
+
min,
|
|
944
|
+
max,
|
|
945
|
+
flex,
|
|
946
|
+
targetSize: frozen ? Math.max(min, Math.min(baseSize, max)) : 0,
|
|
947
|
+
violation: 0
|
|
948
|
+
};
|
|
949
|
+
if (!item.frozen) hasUnfrozen = true;
|
|
950
|
+
return item;
|
|
951
|
+
});
|
|
952
|
+
while (hasUnfrozen) {
|
|
953
|
+
let usedWidth = 0;
|
|
954
|
+
let flexTotal = 0;
|
|
955
|
+
for (const item of items) if (item.frozen) usedWidth += item.targetSize;
|
|
956
|
+
else {
|
|
957
|
+
usedWidth += item.baseSize;
|
|
958
|
+
flexTotal += item.flex;
|
|
959
|
+
}
|
|
960
|
+
const freeSpace = available - usedWidth;
|
|
961
|
+
if (freeSpace > 0 && flexTotal > 0) {
|
|
962
|
+
for (const item of items) if (!item.frozen) item.targetSize = item.baseSize + item.flex / flexTotal * freeSpace;
|
|
963
|
+
} else for (const item of items) if (!item.frozen) item.targetSize = item.baseSize;
|
|
964
|
+
let totalViolation = 0;
|
|
965
|
+
for (const item of items) {
|
|
966
|
+
item.violation = 0;
|
|
967
|
+
if (!item.frozen) {
|
|
968
|
+
const clamped = Math.max(item.min, Math.min(item.targetSize, item.max));
|
|
969
|
+
item.violation = clamped - item.targetSize;
|
|
970
|
+
item.targetSize = clamped;
|
|
971
|
+
totalViolation += item.violation;
|
|
972
|
+
}
|
|
973
|
+
}
|
|
974
|
+
hasUnfrozen = false;
|
|
975
|
+
for (const item of items) {
|
|
976
|
+
if (item.frozen) continue;
|
|
977
|
+
if (totalViolation === 0 || Math.sign(totalViolation) === Math.sign(item.violation)) item.frozen = true;
|
|
978
|
+
else hasUnfrozen = true;
|
|
979
|
+
}
|
|
980
|
+
}
|
|
981
|
+
return cascadeRound(items.map((item) => item.targetSize));
|
|
982
|
+
}
|
|
983
|
+
/**
|
|
984
|
+
* Rounds a list of fractional widths to integers whose sum equals the rounded sum of the input.
|
|
985
|
+
*
|
|
986
|
+
* Rounding each width on its own would lose or gain up to half a pixel per column, which shows up
|
|
987
|
+
* as a hairline gap at the right edge of the table. Carrying the running error into the next
|
|
988
|
+
* column keeps the total exact.
|
|
989
|
+
*/
|
|
990
|
+
function cascadeRound(values) {
|
|
991
|
+
let floatTotal = 0;
|
|
992
|
+
let intTotal = 0;
|
|
993
|
+
return values.map((value) => {
|
|
994
|
+
const rounded = Math.round(value + floatTotal) - intTotal;
|
|
995
|
+
floatTotal += value;
|
|
996
|
+
intTotal += rounded;
|
|
997
|
+
return rounded;
|
|
998
|
+
});
|
|
999
|
+
}
|
|
1000
|
+
//#endregion
|
|
1001
|
+
//#region src/lib/use-available-width.ts
|
|
1002
|
+
/**
|
|
1003
|
+
* Tracks the inner width available to a container's content, in px.
|
|
1004
|
+
*
|
|
1005
|
+
* `clientWidth` rather than `getBoundingClientRect().width` on purpose: it excludes the border and
|
|
1006
|
+
* any scrollbar gutter, which is exactly the space child content can occupy. `DataTable` divides
|
|
1007
|
+
* this number up between its columns, so counting a vertical scrollbar's width would make the
|
|
1008
|
+
* table overflow horizontally by the width of that scrollbar.
|
|
1009
|
+
*
|
|
1010
|
+
* Measured in a layout effect so the first paint already has a real number - a `useEffect` here
|
|
1011
|
+
* would show one frame of collapsed columns before correcting itself.
|
|
1012
|
+
*
|
|
1013
|
+
* @returns The container's `clientWidth`, or `0` before the first measurement.
|
|
1014
|
+
*/
|
|
1015
|
+
function useAvailableWidth(ref) {
|
|
1016
|
+
const [width, setWidth] = React.useState(0);
|
|
1017
|
+
React.useLayoutEffect(() => {
|
|
1018
|
+
const el = ref.current;
|
|
1019
|
+
if (!el) return;
|
|
1020
|
+
const measure = () => setWidth((previous) => previous === el.clientWidth ? previous : el.clientWidth);
|
|
1021
|
+
measure();
|
|
1022
|
+
if (typeof ResizeObserver === "undefined") {
|
|
1023
|
+
window.addEventListener("resize", measure);
|
|
1024
|
+
return () => window.removeEventListener("resize", measure);
|
|
1025
|
+
}
|
|
1026
|
+
const observer = new ResizeObserver(measure);
|
|
1027
|
+
observer.observe(el);
|
|
1028
|
+
return () => observer.disconnect();
|
|
1029
|
+
}, [ref]);
|
|
1030
|
+
return width;
|
|
1031
|
+
}
|
|
879
1032
|
//#endregion
|
|
880
1033
|
//#region src/lib/use-horizontal-scroll-edges.ts
|
|
881
1034
|
/**
|
|
@@ -929,6 +1082,18 @@ function useHorizontalScrollEdges(containerRef, enabled, contentSizeKey) {
|
|
|
929
1082
|
//#endregion
|
|
930
1083
|
//#region src/components/data-table.tsx
|
|
931
1084
|
const SELECT_COLUMN_ID = "__select";
|
|
1085
|
+
/**
|
|
1086
|
+
* Narrowest a column can be dragged to, in px, when it declares a `size` but no `minSize`.
|
|
1087
|
+
*
|
|
1088
|
+
* Separate from the solver's floor on purpose. For LAYOUT a static column has no minimum, because a
|
|
1089
|
+
* declared `size: 16` has to render as 16. For DRAGGING it needs one, or the column can be pulled to
|
|
1090
|
+
* nothing and vanish. TanStack's own default of 20 used to provide this by accident, until
|
|
1091
|
+
* `defaultColumn: { minSize: 0 }` removed it so that our solved widths stop being second-guessed.
|
|
1092
|
+
*
|
|
1093
|
+
* Applied as `min(declared size, this)`, so a column declaring less than 20 is floored at its own
|
|
1094
|
+
* width rather than jumping up to 20 the moment it is touched.
|
|
1095
|
+
*/
|
|
1096
|
+
const MIN_RESIZE_WIDTH = 20;
|
|
932
1097
|
const DEFAULT_SKELETON_COUNT = 5;
|
|
933
1098
|
const EMPTY_KEYS = /* @__PURE__ */ new Set();
|
|
934
1099
|
const EMPTY_COLUMN_PINNING = {
|
|
@@ -936,6 +1101,62 @@ const EMPTY_COLUMN_PINNING = {
|
|
|
936
1101
|
right: []
|
|
937
1102
|
};
|
|
938
1103
|
/**
|
|
1104
|
+
* Resolves the id TanStack will give a leaf column, from the consumer's own def.
|
|
1105
|
+
*
|
|
1106
|
+
* Must stay identical to `createColumn` in `@tanstack/table-core`, which resolves
|
|
1107
|
+
* `id ?? accessorKey.replaceAll('.', '_') ?? (typeof header === 'string' ? header : undefined)`.
|
|
1108
|
+
* The dot replacement and the string-`header` fallback are both easy to miss and both matter: get
|
|
1109
|
+
* either wrong and the id here disagrees with the id on the table instance, so the column gets no
|
|
1110
|
+
* resolved width and every pin offset after it is computed from the wrong number.
|
|
1111
|
+
*
|
|
1112
|
+
* `assertEveryColumnSized` below is the backstop for exactly that.
|
|
1113
|
+
*/
|
|
1114
|
+
function resolveLeafColumnId(def) {
|
|
1115
|
+
const accessorKey = def.accessorKey;
|
|
1116
|
+
if (def.id != null) return def.id;
|
|
1117
|
+
if (accessorKey != null) return accessorKey.replace(/\./g, "_");
|
|
1118
|
+
return typeof def.header === "string" ? def.header : void 0;
|
|
1119
|
+
}
|
|
1120
|
+
/**
|
|
1121
|
+
* Flattens column defs to their leaves and reads the declared sizing off each one.
|
|
1122
|
+
*
|
|
1123
|
+
* Reads the consumer's own defs rather than `column.columnDef` from the table instance, because
|
|
1124
|
+
* TanStack merges its defaults (`size: 150`, `minSize: 20`) into every column def - so by the time
|
|
1125
|
+
* a def reaches the table there is no way left to tell "the consumer asked for 150" from "the
|
|
1126
|
+
* consumer declared nothing". That distinction is the whole model: a declared `size` is static, an
|
|
1127
|
+
* absent one is flexible.
|
|
1128
|
+
*/
|
|
1129
|
+
function collectSizingSpecs(defs, into = /* @__PURE__ */ new Map()) {
|
|
1130
|
+
for (const def of defs) {
|
|
1131
|
+
const groupChildren = def.columns;
|
|
1132
|
+
if (groupChildren === null || groupChildren === void 0 ? void 0 : groupChildren.length) {
|
|
1133
|
+
collectSizingSpecs(groupChildren, into);
|
|
1134
|
+
continue;
|
|
1135
|
+
}
|
|
1136
|
+
const id = resolveLeafColumnId(def);
|
|
1137
|
+
if (id == null) continue;
|
|
1138
|
+
into.set(id, {
|
|
1139
|
+
id,
|
|
1140
|
+
size: def.size,
|
|
1141
|
+
minSize: def.minSize,
|
|
1142
|
+
maxSize: def.maxSize,
|
|
1143
|
+
flex: def.flex
|
|
1144
|
+
});
|
|
1145
|
+
}
|
|
1146
|
+
return into;
|
|
1147
|
+
}
|
|
1148
|
+
/**
|
|
1149
|
+
* Dev-only check that the solve covered every column the table actually renders.
|
|
1150
|
+
*
|
|
1151
|
+
* A column missing from `columnSizing` silently falls back to TanStack's 150px default and throws
|
|
1152
|
+
* off the pin offsets of everything after it - a failure that is invisible in a passing test suite
|
|
1153
|
+
* and easy to introduce by drifting from `resolveLeafColumnId`.
|
|
1154
|
+
*/
|
|
1155
|
+
function assertEveryColumnSized(table, columnSizing) {
|
|
1156
|
+
const diverged = table.getVisibleLeafColumns().filter((column) => columnSizing[column.id] !== column.getSize()).map((column) => `${column.id} (solved ${columnSizing[column.id]}, getSize ${column.getSize()})`);
|
|
1157
|
+
if (diverged.length > 0) console.warn(`DataTable: resolved width disagrees with getSize() for ${diverged.join(", ")}. The colgroup uses the resolved width and getTotalSize() uses getSize(), so the table renders wider or narrower than its own column tracks and every width gets rescaled. Usual causes: a column id derived from your \`columns\` prop that disagrees with the one on the table instance (set an explicit \`id\`), or a \`columnDef\` min/max clamping a width we own.`);
|
|
1158
|
+
}
|
|
1159
|
+
/**
|
|
939
1160
|
* Data grid built on TanStack Table. Renders a `<div>` wrapper around a DS
|
|
940
1161
|
* `<Table>` (`<table>`), driven by `columns` + `data`, with opt-in sorting,
|
|
941
1162
|
* row selection, column resizing, column pinning (`columnPinning`), virtualization,
|
|
@@ -952,7 +1173,7 @@ const EMPTY_COLUMN_PINNING = {
|
|
|
952
1173
|
*/
|
|
953
1174
|
function DataTable(componentProps) {
|
|
954
1175
|
var _columnPinning$left$l, _columnPinning$left, _columnPinning$right$, _columnPinning$right, _resolvedColumnPinnin, _resolvedColumnPinnin2, _resolvedColumnPinnin3, _resolvedColumnPinnin4, _componentProps$virtu;
|
|
955
|
-
const { columns, data, getRowId, loadingState = "idle", skeletonCount = DEFAULT_SKELETON_COUNT, emptyState, sorting, onSortingChange, columnVisibility, columnPinning, selectionMode = "none", selectedKeys, defaultSelectedKeys, onSelectionChange, disabledKeys, disabledBehavior = "all", actionBar, onFetchMore, hasMore = false, enableColumnResizing = false, fillHeight = false,
|
|
1176
|
+
const { columns, data, getRowId, loadingState = "idle", skeletonCount = DEFAULT_SKELETON_COUNT, emptyState, sorting, onSortingChange, columnVisibility, columnPinning, selectionMode = "none", selectedKeys, defaultSelectedKeys, onSelectionChange, disabledKeys, disabledBehavior = "all", actionBar, onFetchMore, hasMore = false, enableColumnResizing = false, fillHeight = false, onRowClick, className } = componentProps;
|
|
956
1177
|
const sortingEnabled = !!onSortingChange;
|
|
957
1178
|
const selectionEnabled = selectionMode !== "none";
|
|
958
1179
|
const multiSelect = selectionMode === "multiple";
|
|
@@ -992,6 +1213,70 @@ function DataTable(componentProps) {
|
|
|
992
1213
|
const next = typeof updater === "function" ? updater(rowSelection) : updater;
|
|
993
1214
|
setSelectionSet(new Set(Object.keys(next).filter((key) => next[key])));
|
|
994
1215
|
}, [rowSelection, setSelectionSet]);
|
|
1216
|
+
const containerRef = React.useRef(null);
|
|
1217
|
+
const availableWidth = useAvailableWidth(containerRef);
|
|
1218
|
+
const sizingSpecs = React.useMemo(() => collectSizingSpecs(finalColumns), [finalColumns]);
|
|
1219
|
+
const [userSizing, setUserSizing] = React.useState({});
|
|
1220
|
+
const visibleLeafIds = React.useMemo(() => {
|
|
1221
|
+
var _resolvedColumnPinnin5, _resolvedColumnPinnin6;
|
|
1222
|
+
const all = [...sizingSpecs.keys()];
|
|
1223
|
+
const ids = columnVisibility ? all.filter((id) => columnVisibility[id] !== false) : all;
|
|
1224
|
+
const left = (_resolvedColumnPinnin5 = resolvedColumnPinning.left) !== null && _resolvedColumnPinnin5 !== void 0 ? _resolvedColumnPinnin5 : [];
|
|
1225
|
+
const right = (_resolvedColumnPinnin6 = resolvedColumnPinning.right) !== null && _resolvedColumnPinnin6 !== void 0 ? _resolvedColumnPinnin6 : [];
|
|
1226
|
+
if (left.length === 0 && right.length === 0) return ids;
|
|
1227
|
+
const isPinned = /* @__PURE__ */ new Set([...left, ...right]);
|
|
1228
|
+
return [
|
|
1229
|
+
...left.filter((id) => ids.includes(id)),
|
|
1230
|
+
...ids.filter((id) => !isPinned.has(id)),
|
|
1231
|
+
...right.filter((id) => ids.includes(id))
|
|
1232
|
+
];
|
|
1233
|
+
}, [
|
|
1234
|
+
sizingSpecs,
|
|
1235
|
+
columnVisibility,
|
|
1236
|
+
resolvedColumnPinning
|
|
1237
|
+
]);
|
|
1238
|
+
const columnSizing = React.useMemo(() => {
|
|
1239
|
+
const specs = visibleLeafIds.map((id) => {
|
|
1240
|
+
var _sizingSpecs$get;
|
|
1241
|
+
const spec = (_sizingSpecs$get = sizingSpecs.get(id)) !== null && _sizingSpecs$get !== void 0 ? _sizingSpecs$get : { id };
|
|
1242
|
+
const dragged = userSizing[id];
|
|
1243
|
+
return dragged != null ? _objectSpread2(_objectSpread2({}, spec), {}, { size: dragged }) : spec;
|
|
1244
|
+
});
|
|
1245
|
+
const widths = resolveColumnWidths(availableWidth, specs);
|
|
1246
|
+
const resolved = {};
|
|
1247
|
+
visibleLeafIds.forEach((id, index) => {
|
|
1248
|
+
resolved[id] = widths[index];
|
|
1249
|
+
});
|
|
1250
|
+
return resolved;
|
|
1251
|
+
}, [
|
|
1252
|
+
visibleLeafIds,
|
|
1253
|
+
sizingSpecs,
|
|
1254
|
+
userSizing,
|
|
1255
|
+
availableWidth
|
|
1256
|
+
]);
|
|
1257
|
+
const handleColumnSizingChange = React.useCallback((updater) => {
|
|
1258
|
+
const next = typeof updater === "function" ? updater(columnSizing) : updater;
|
|
1259
|
+
const changedIds = visibleLeafIds.filter((id) => next[id] != null && next[id] !== columnSizing[id]);
|
|
1260
|
+
if (changedIds.length === 0) return;
|
|
1261
|
+
const frozen = {};
|
|
1262
|
+
const firstChangedIndex = visibleLeafIds.indexOf(changedIds[0]);
|
|
1263
|
+
for (let index = 0; index < firstChangedIndex; index++) {
|
|
1264
|
+
const id = visibleLeafIds[index];
|
|
1265
|
+
frozen[id] = columnSizing[id];
|
|
1266
|
+
}
|
|
1267
|
+
for (const id of changedIds) {
|
|
1268
|
+
var _spec$minSize, _spec$maxSize;
|
|
1269
|
+
const spec = sizingSpecs.get(id);
|
|
1270
|
+
const floor = (_spec$minSize = spec === null || spec === void 0 ? void 0 : spec.minSize) !== null && _spec$minSize !== void 0 ? _spec$minSize : (spec === null || spec === void 0 ? void 0 : spec.size) != null ? Math.min(spec.size, MIN_RESIZE_WIDTH) : 75;
|
|
1271
|
+
const cap = (_spec$maxSize = spec === null || spec === void 0 ? void 0 : spec.maxSize) !== null && _spec$maxSize !== void 0 ? _spec$maxSize : Number.MAX_SAFE_INTEGER;
|
|
1272
|
+
frozen[id] = Math.max(floor, Math.min(next[id], cap));
|
|
1273
|
+
}
|
|
1274
|
+
setUserSizing((previous) => _objectSpread2(_objectSpread2({}, previous), frozen));
|
|
1275
|
+
}, [
|
|
1276
|
+
columnSizing,
|
|
1277
|
+
visibleLeafIds,
|
|
1278
|
+
sizingSpecs
|
|
1279
|
+
]);
|
|
995
1280
|
const table = useReactTable({
|
|
996
1281
|
data,
|
|
997
1282
|
columns: finalColumns,
|
|
@@ -1004,9 +1289,14 @@ function DataTable(componentProps) {
|
|
|
1004
1289
|
enableColumnResizing,
|
|
1005
1290
|
enableColumnPinning: hasPinnedColumns,
|
|
1006
1291
|
columnResizeMode: "onChange",
|
|
1292
|
+
defaultColumn: { minSize: 0 },
|
|
1007
1293
|
onSortingChange,
|
|
1008
1294
|
onRowSelectionChange: handleRowSelectionChange,
|
|
1009
|
-
|
|
1295
|
+
onColumnSizingChange: handleColumnSizingChange,
|
|
1296
|
+
state: _objectSpread2(_objectSpread2(_objectSpread2({}, sorting !== void 0 && { sorting }), {}, {
|
|
1297
|
+
rowSelection,
|
|
1298
|
+
columnSizing
|
|
1299
|
+
}, hasPinnedColumns && { columnPinning: resolvedColumnPinning }), columnVisibility !== void 0 && { columnVisibility })
|
|
1010
1300
|
});
|
|
1011
1301
|
const rows = table.getRowModel().rows;
|
|
1012
1302
|
const selectedCount = table.getSelectedRowModel().rows.length;
|
|
@@ -1026,15 +1316,15 @@ function DataTable(componentProps) {
|
|
|
1026
1316
|
} else if (onRowClick) onRowClick(row.original);
|
|
1027
1317
|
};
|
|
1028
1318
|
const virtualizeRows = ((_componentProps$virtu = componentProps.virtualizeRows) !== null && _componentProps$virtu !== void 0 ? _componentProps$virtu : false) && fillHeight;
|
|
1029
|
-
const
|
|
1030
|
-
|
|
1031
|
-
const { atLeftEdge, atRightEdge } = useHorizontalScrollEdges(containerRef, hasPinnedColumns,
|
|
1032
|
-
const getCellPinProps = (column, variant) => getPinnedCellProps(column, {
|
|
1033
|
-
pinWidths,
|
|
1319
|
+
const totalSize = table.getTotalSize();
|
|
1320
|
+
assertEveryColumnSized(table, columnSizing);
|
|
1321
|
+
const { atLeftEdge, atRightEdge } = useHorizontalScrollEdges(containerRef, hasPinnedColumns, totalSize);
|
|
1322
|
+
const getCellPinProps = (column, variant, size) => getPinnedCellProps(column, {
|
|
1034
1323
|
variant,
|
|
1035
1324
|
fillHeight,
|
|
1036
1325
|
showLeftEdgeShadow: !atLeftEdge,
|
|
1037
|
-
showRightEdgeShadow: !atRightEdge
|
|
1326
|
+
showRightEdgeShadow: !atRightEdge,
|
|
1327
|
+
size
|
|
1038
1328
|
});
|
|
1039
1329
|
const rowVirtualizer = useVirtualizer({
|
|
1040
1330
|
count: virtualizeRows ? rows.length : 0,
|
|
@@ -1056,19 +1346,21 @@ function DataTable(componentProps) {
|
|
|
1056
1346
|
children: [
|
|
1057
1347
|
/* @__PURE__ */ jsxs(Table, {
|
|
1058
1348
|
containerRef,
|
|
1059
|
-
className: cn(
|
|
1349
|
+
className: cn("table-fixed", hasPinnedColumns && cn("border-separate border-spacing-0", "[&_[data-slot=table-cell]]:border-b [&_[data-slot=table-cell]]:border-border", "[&_tbody_tr:last-child_[data-slot=table-cell]]:border-b-0", "[&_tfoot_[data-slot=table-cell]]:border-t [&_tfoot_tr:last-child_[data-slot=table-cell]]:border-b-0", !fillHeight && "[&_[data-slot=table-head]]:border-b [&_[data-slot=table-head]]:border-border"), fillHeight && "[&_[data-slot=table-head]]:sticky [&_[data-slot=table-head]]:top-0 [&_[data-slot=table-head]]:z-10 [&_[data-slot=table-head]]:bg-background [&_thead_tr]:border-b-0 [&_[data-slot=table-head]]:shadow-[inset_0_-1px_0_0_var(--border)]", fillHeight && showEmptyState && "h-full"),
|
|
1060
1350
|
containerClassName: fillHeight ? "min-h-0 flex-1 overflow-y-auto" : void 0,
|
|
1061
1351
|
containerStyle: reservedBottom ? {
|
|
1062
1352
|
paddingBottom: reservedBottom,
|
|
1063
1353
|
scrollPaddingBottom: reservedBottom
|
|
1064
1354
|
} : void 0,
|
|
1065
|
-
style:
|
|
1355
|
+
style: { width: `${totalSize}px` },
|
|
1066
1356
|
children: [
|
|
1357
|
+
/* @__PURE__ */ jsx("colgroup", { children: visibleLeafIds.map((id) => /* @__PURE__ */ jsx("col", { style: { width: `${columnSizing[id]}px` } }, id)) }),
|
|
1067
1358
|
/* @__PURE__ */ jsx(TableHeader, { children: table.getHeaderGroups().map((headerGroup) => /* @__PURE__ */ jsx(TableRow, {
|
|
1068
1359
|
className: enableColumnResizing ? "group/header-row" : void 0,
|
|
1069
1360
|
children: headerGroup.headers.map((header) => {
|
|
1070
|
-
const pinProps = getCellPinProps(header.column, "header");
|
|
1361
|
+
const pinProps = getCellPinProps(header.column, "header", header.getSize());
|
|
1071
1362
|
return /* @__PURE__ */ jsxs(TableHead, {
|
|
1363
|
+
colSpan: header.colSpan,
|
|
1072
1364
|
style: pinProps.style,
|
|
1073
1365
|
className: cn(enableColumnResizing && "relative", pinProps.className),
|
|
1074
1366
|
children: [header.isPlaceholder ? null : header.column.getCanSort() ? /* @__PURE__ */ jsx(SortableHeader, { header }) : flexRender(header.column.columnDef.header, header.getContext()), enableColumnResizing && header.column.getCanResize() ? /* @__PURE__ */ jsx(ResizeHandle, { header }) : null]
|
|
@@ -1166,8 +1458,9 @@ function DataTable(componentProps) {
|
|
|
1166
1458
|
})
|
|
1167
1459
|
}) : null }),
|
|
1168
1460
|
hasFooter && !isInitialLoading ? /* @__PURE__ */ jsx(TableFooter, { children: table.getFooterGroups().map((footerGroup) => /* @__PURE__ */ jsx(TableRow, { children: footerGroup.headers.map((header) => {
|
|
1169
|
-
const pinProps = getCellPinProps(header.column, "footer");
|
|
1461
|
+
const pinProps = getCellPinProps(header.column, "footer", header.getSize());
|
|
1170
1462
|
return /* @__PURE__ */ jsx(TableCell, {
|
|
1463
|
+
colSpan: header.colSpan,
|
|
1171
1464
|
style: pinProps.style,
|
|
1172
1465
|
className: pinProps.className,
|
|
1173
1466
|
children: header.isPlaceholder ? null : flexRender(header.column.columnDef.footer, header.getContext())
|
|
@@ -1265,8 +1558,8 @@ function SortableHeader({ header }) {
|
|
|
1265
1558
|
* while horizontal scroll has moved away from that edge.
|
|
1266
1559
|
*/
|
|
1267
1560
|
function getPinnedCellProps(column, options) {
|
|
1268
|
-
const {
|
|
1269
|
-
const style =
|
|
1561
|
+
const { variant, fillHeight, showLeftEdgeShadow, showRightEdgeShadow, size } = options;
|
|
1562
|
+
const style = { width: `${size !== null && size !== void 0 ? size : column.getSize()}px` };
|
|
1270
1563
|
const isPinned = column.getIsPinned();
|
|
1271
1564
|
if (!isPinned) return { style };
|
|
1272
1565
|
style.position = "sticky";
|
package/dist/data-table.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { a as OnChangeFn, i as DataTableLoadingState, n as ColumnPinningState, o as SortingState, r as DataTable, s as VisibilityState, t as ColumnDef } from "./data-table-
|
|
1
|
+
import { a as OnChangeFn, i as DataTableLoadingState, n as ColumnPinningState, o as SortingState, r as DataTable, s as VisibilityState, t as ColumnDef } from "./data-table-AHgs5bxe.js";
|
|
2
2
|
export { type ColumnDef, type ColumnPinningState, DataTable, type DataTableLoadingState, type OnChangeFn, type SortingState, type VisibilityState };
|
package/dist/data-table.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { t as DataTable } from "./data-table-
|
|
1
|
+
import { t as DataTable } from "./data-table-DD7m3puW.js";
|
|
2
2
|
export { DataTable };
|