@xeplr/ui-table 1.1.6 → 1.1.8

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/README.md CHANGED
@@ -231,6 +231,9 @@ Numeric strings compare as numbers. A host `cellStyle` prop result is merged ove
231
231
  | `statusBadge` | `map: { value: { bg, color, border?, label? } }`, `default` | text |
232
232
  | `dateDisplay` | `format`: `'date'` (`15 Sep 2026`), `'datetime'`, `'relative'` (`3h ago`, dates after 30 days) | Date or ISO string |
233
233
  | `link` | `hrefKey` (renders `<a href={row[hrefKey]}>`) and `target`, or `onClick(row)` | text |
234
+ | `trend` | `format(value, row)` (how the number reads: a percent, a currency …), `absolute` (true: the arrow says the sign, so the number is shown without it), `color(value, row)` (a rule's colour, which wins), `colors` (`{ up, down }`) | number — ▲ green above zero, ▼ red below, nothing for zero or a blank |
235
+
236
+ **A trend** is one look everywhere: `trendOf(value)` (`'up'`, `'down'` or null), `trendLook(value, ruledColor?, colors?)` → `{ arrow, color }` and `TREND_COLORS` are exported, so a host that shows a single value as a trend (a KPI, a tile) draws it exactly as the `trend` cell does. A rule's colour wins over green/red — that is how "down is good" is said (a refund rate).
234
237
 
235
238
  Each renderer is `(config) => (ctx) => node`. The `renderers` object is the registry the table reads, so `renderers.myType = (config) => (ctx) => …` adds one.
236
239
 
@@ -253,6 +256,7 @@ The scrollbar, the scroll position and the layout are the full table's. Scrollin
253
256
  - Everything fits → each column gets its natural width; leftover space stays empty.
254
257
  - It does not fit → no empty space; every column drops to its floor (its longest word) and the remainder is shared in proportion to what each wanted.
255
258
  - Not even the floors fit → floors, and the table scrolls horizontally.
259
+ - **A header never breaks mid-word for want of a pixel.** Its room is its text plus everything else on its line, read off the real cells: the header cell's padding, the `<th>`'s own padding and borders, the gap, and the filter trigger (`headerChrome`). A sorted column is measured with its arrow (`sortedHeaderText`), and re-measured when the sort moves. Why: leaving the `<th>`'s 1px border out made every column 1px short of its header, so "Orders" broke as "Order / s".
256
260
 
257
261
  `columnWidths: { [leafIndex]: percent }` pins columns to a share of the width the columns divide (after checkbox, expand and actions columns). **By index**, because a pivot's column keys are data and change on refresh. `columnText(row, column)` supplies the displayed text when cells are formatted; `sizingKey` forces a re-measure (a font change cannot be observed); `onColumnSizing` receives each column's natural width, floor, assigned width and reason (`natural`, `shrunk`, `floor`, `pinned`).
258
262
 
@@ -291,7 +295,7 @@ There is no margin property: a `<td>` ignores margin.
291
295
 
292
296
  ## Exports
293
297
 
294
- `XeplrTable`, `CHILD_DISPLAY`, `useTableController`, `useActionsController`, `detectTypes`, `TYPES`, `StringFilter` / `stringFilterFn`, `NumberFilter` / `numberFilterFn`, `DateFilter` / `dateFilterFn`, `BooleanFilter` / `booleanFilterFn`, `FilterWrapper`, `ActionsCell`, `RecordModal`, `RecordDetail`, `ChildTable`, `CellZoom`, `buildChangeSet`, `resolveOperator`, `resolveString`, `resolveNumber`, `resolveDate`, `isNullish`, `resolveCellStyle`, `STYLE_GROUPS`, `STYLE_FIELDS`, `BORDER_SIDES`, `styleField`, `cssLength`, `flattenStyle`, `resolveStyle`, `toCssProperties`, `resolveCellCss`, `useColumnWidths`, `measureColumnDemand`, `allocateColumnWidths`, `resolvePercentWidths`, `longestToken`, `WIDTH_NATURAL`, `WIDTH_SHRUNK`, `WIDTH_FLOOR`, `renderers`, `avatarName`, `tags`, `currency`, `memberChips`, `twoLine`, `statusBadge`, `dateDisplay`, `link`.
298
+ `XeplrTable`, `CHILD_DISPLAY`, `useTableController`, `useActionsController`, `detectTypes`, `TYPES`, `StringFilter` / `stringFilterFn`, `NumberFilter` / `numberFilterFn`, `DateFilter` / `dateFilterFn`, `BooleanFilter` / `booleanFilterFn`, `FilterWrapper`, `ActionsCell`, `RecordModal`, `RecordDetail`, `ChildTable`, `CellZoom`, `buildChangeSet`, `resolveOperator`, `resolveString`, `resolveNumber`, `resolveDate`, `isNullish`, `resolveCellStyle`, `STYLE_GROUPS`, `STYLE_FIELDS`, `BORDER_SIDES`, `styleField`, `cssLength`, `flattenStyle`, `resolveStyle`, `toCssProperties`, `resolveCellCss`, `useColumnWidths`, `measureColumnDemand`, `allocateColumnWidths`, `resolvePercentWidths`, `longestToken`, `headerChrome`, `sortedHeaderText`, `WIDTH_NATURAL`, `WIDTH_SHRUNK`, `WIDTH_FLOOR`, `renderers`, `avatarName`, `tags`, `currency`, `memberChips`, `twoLine`, `statusBadge`, `dateDisplay`, `link`.
295
299
 
296
300
  ## Tests
297
301
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xeplr/ui-table",
3
- "version": "1.1.6",
3
+ "version": "1.1.8",
4
4
  "description": "Controlled TanStack Table wrapper with auto-detected column types and smart filters",
5
5
  "type": "module",
6
6
  "main": "src/index.js",
@@ -239,7 +239,14 @@ export default function XeplrTable(props) {
239
239
  // Content sizing measures the whole dataset's first N rows, not the current
240
240
  // page — otherwise every page turn would re-measure and the columns would
241
241
  // visibly jump.
242
+ // The columns whose header shows a sort arrow, by accessor: their header
243
+ // needs the arrow's room too, or "Revenue ▼" wraps its arrow.
244
+ var sortedAccessors = (table.getState().sorting || []).map(function(entry) {
245
+ var column = table.getColumn(entry.id);
246
+ return column && column.columnDef.accessorKey;
247
+ }).filter(Boolean);
242
248
  var widths = useColumnWidths({
249
+ sortedColumns: sortedAccessors,
243
250
  enabled: props.columnSizing === 'content',
244
251
  data: props.data,
245
252
  columns: columns,
@@ -99,6 +99,43 @@ function pinnedWidth(overrides, index) {
99
99
  return Math.ceil(width);
100
100
  }
101
101
 
102
+ /**
103
+ * The room a header's label does NOT get — everything else on its line.
104
+ *
105
+ * Every part counts, read off the real cells: the header cell's padding, the
106
+ * <th>'s own padding and borders (its 1px divider sits inside the column's
107
+ * width), the gap between the label and the filter trigger, and the trigger.
108
+ * Leaving the <th> border out made each column exactly 1px short of its
109
+ * header, so "Orders" broke as "Order / s".
110
+ *
111
+ * @param {object} box
112
+ * @param {number} box.cell - header cell padding + borders
113
+ * @param {number} box.th - the <th>'s padding + borders
114
+ * @param {number} box.gap - gap between the label and the trigger
115
+ * @param {number|null} box.trigger - the filter trigger's width; null when
116
+ * the table has none, and the sort caret's room is kept instead
117
+ */
118
+ export function headerChrome(box) {
119
+ var fixed = (box.cell || 0) + (box.th || 0);
120
+ if (box.trigger === null || box.trigger === undefined) return fixed + SORT_CARET_ROOM;
121
+ return fixed + (box.gap || 0) + box.trigger;
122
+ }
123
+
124
+ /** Room for the sort caret on a table without filter triggers. */
125
+ export var SORT_CARET_ROOM = 16;
126
+
127
+ /** The arrow a sorted column's header shows (XeplrTable's sort icon). */
128
+ export var SORT_ARROW = ' \u25BC';
129
+
130
+ /**
131
+ * A header's text as it is drawn: a sorted column's carries its arrow, so its
132
+ * width is measured with it — without, "Revenue ▼" wrapped its arrow.
133
+ */
134
+ export function sortedHeaderText(column, sortedColumns) {
135
+ var text = headerTextOf(column);
136
+ return column && sortedColumns && sortedColumns.indexOf(column.accessor) !== -1 ? text + SORT_ARROW : text;
137
+ }
138
+
102
139
  /**
103
140
  * The longest run of characters with no break opportunity in it.
104
141
  *
package/src/index.js CHANGED
@@ -41,10 +41,14 @@ export {
41
41
  // Content-based column widths
42
42
  export { default as useColumnWidths } from './useColumnWidths.js';
43
43
  export {
44
- measureColumnDemand, allocateColumnWidths, resolvePercentWidths, longestToken,
44
+ measureColumnDemand, allocateColumnWidths, resolvePercentWidths, longestToken, headerChrome, sortedHeaderText,
45
45
  WIDTH_NATURAL, WIDTH_SHRUNK, WIDTH_FLOOR
46
46
  } from './columnWidths.js';
47
47
 
48
48
  // Cell renderers (config-driven)
49
49
  export { default as renderers } from './renderers/index.js';
50
- export { avatarName, tags, currency, memberChips, twoLine, statusBadge, dateDisplay, link } from './renderers/index.js';
50
+ export { avatarName, tags, currency, memberChips, twoLine, statusBadge, dateDisplay, link, trend } from './renderers/index.js';
51
+
52
+ // A number shown as a trend (▲ green up, ▼ red down) — the `trend` renderer's,
53
+ // and any host's that shows one value as a trend, so every trend looks alike.
54
+ export { trendOf, trendLook, TREND_COLORS } from './trend.js';
@@ -6,6 +6,7 @@ import twoLine from './twoLine.jsx';
6
6
  import statusBadge from './statusBadge.jsx';
7
7
  import dateDisplay from './dateDisplay.jsx';
8
8
  import link from './link.jsx';
9
+ import trend from './trend.jsx';
9
10
 
10
11
  var renderers = {
11
12
  avatarName: avatarName,
@@ -15,8 +16,9 @@ var renderers = {
15
16
  twoLine: twoLine,
16
17
  statusBadge: statusBadge,
17
18
  dateDisplay: dateDisplay,
18
- link: link
19
+ link: link,
20
+ trend: trend
19
21
  };
20
22
 
21
23
  export default renderers;
22
- export { avatarName, tags, currency, memberChips, twoLine, statusBadge, dateDisplay, link };
24
+ export { avatarName, tags, currency, memberChips, twoLine, statusBadge, dateDisplay, link, trend };
@@ -0,0 +1,33 @@
1
+ import { trendLook } from '../trend.js';
2
+
3
+ /**
4
+ * A number with its direction: ▲ green above zero, ▼ red below (trend.js).
5
+ *
6
+ * config: {
7
+ * format?: (value, row) => string how the number reads (a percent, a
8
+ * currency …); default: up to 2 decimals
9
+ * absolute?: boolean the arrow says the sign, so the number
10
+ * is shown without it (default true)
11
+ * color?: (value, row) => string a rule's colour, which wins
12
+ * colors?: { up, down } the defaults' replacement
13
+ * }
14
+ */
15
+ export default function trend(config) {
16
+ var c = config || {};
17
+ var absolute = c.absolute !== false;
18
+ return function cell(ctx) {
19
+ var v = ctx.getValue();
20
+ if (v === null || v === undefined || v === '') return null;
21
+ var row = ctx.row && ctx.row.original;
22
+ var n = Number(v);
23
+ var shown = absolute && Number.isFinite(n) ? Math.abs(n) : v;
24
+ var text = c.format ? c.format(shown, row) : (Number.isFinite(Number(shown)) ? Number(shown).toLocaleString(undefined, { maximumFractionDigits: 2 }) : String(shown));
25
+ var look = trendLook(v, c.color ? c.color(v, row) : null, c.colors);
26
+ return (
27
+ <span className="xeplr-r-trend" style={look.color ? { color: look.color } : undefined}>
28
+ {look.arrow && <span className="xeplr-r-trend-arrow" aria-hidden="true">{look.arrow}</span>}
29
+ {text}
30
+ </span>
31
+ );
32
+ };
33
+ }
package/src/trend.js ADDED
@@ -0,0 +1,41 @@
1
+ // A NUMBER SHOWN AS A TREND — its own sign says which way it went: ▲ above
2
+ // zero, ▼ below. Zero, a blank or something that is not a number has no
3
+ // direction, so it gets no arrow and no colour.
4
+ //
5
+ // The colour: green up, red down — unless the caller's rule coloured the value
6
+ // (`ruledColor`), which wins. That is how "green when negative" is said: for a
7
+ // refund rate, down is good.
8
+ //
9
+ // Used by the `trend` cell renderer, and by any host that shows one value as a
10
+ // trend (a KPI, a tile) so every trend looks the same.
11
+
12
+ export var TREND_COLORS = { up: '#1a7f37', down: '#c2372e' };
13
+ var ARROWS = { up: '▲', down: '▼' };
14
+
15
+ function asNumber(value) {
16
+ if (typeof value === 'number') return Number.isFinite(value) ? value : null;
17
+ if (typeof value === 'string' && value.trim() !== '') {
18
+ var n = Number(value);
19
+ return Number.isFinite(n) ? n : null;
20
+ }
21
+ return null;
22
+ }
23
+
24
+ /** 'up', 'down', or null when the value has no direction. */
25
+ export function trendOf(value) {
26
+ var n = asNumber(value);
27
+ if (n === null || n === 0) return null;
28
+ return n > 0 ? 'up' : 'down';
29
+ }
30
+
31
+ /**
32
+ * What a value shown as a trend looks like: { arrow, color } — the rule's
33
+ * colour when one coloured it, the default for its direction otherwise;
34
+ * { arrow: null, color: ruledColor } when it has no direction.
35
+ */
36
+ export function trendLook(value, ruledColor, colors) {
37
+ var dir = trendOf(value);
38
+ var palette = colors || TREND_COLORS;
39
+ if (!dir) return { arrow: null, color: ruledColor || null };
40
+ return { arrow: ARROWS[dir], color: ruledColor || palette[dir] };
41
+ }
@@ -1,5 +1,5 @@
1
1
  import { useState, useRef, useLayoutEffect, useCallback } from 'react';
2
- import { measureColumnDemand, allocateColumnWidths, resolvePercentWidths, headerTextOf } from './columnWidths.js';
2
+ import { measureColumnDemand, allocateColumnWidths, resolvePercentWidths, sortedHeaderText, headerChrome } from './columnWidths.js';
3
3
 
4
4
  /**
5
5
  * Content-based column widths — the DOM half. See columnWidths.js for the
@@ -117,6 +117,9 @@ export default function useColumnWidths(opts) {
117
117
  var data = opts.data;
118
118
  var columns = opts.columns;
119
119
  var sizingKey = opts.sizingKey;
120
+ // Sorted columns' headers carry an arrow; re-measured when the sort moves.
121
+ var sortedColumns = opts.sortedColumns || [];
122
+ var sortedSignature = sortedColumns.join('\u0001');
120
123
  var onSizing = opts.onSizing;
121
124
  var sampleRows = opts.sampleRows || DEFAULTS.sampleRows;
122
125
  var overrides = opts.columnWidths;
@@ -173,7 +176,7 @@ export default function useColumnWidths(opts) {
173
176
 
174
177
  // Cheap bail-out before the expensive part: measuring 200 rows across
175
178
  // every column on a render that changed nothing is pure waste.
176
- var signature = signatureOf(columns, containerWidth, sizingKey, overrides);
179
+ var signature = signatureOf(columns, containerWidth, sizingKey, overrides) + '\u0002' + sortedSignature;
177
180
  if (signature === signatureRef.current && data === dataRef.current && sizingRef.current) return;
178
181
  signatureRef.current = signature;
179
182
  dataRef.current = data;
@@ -213,10 +216,14 @@ export default function useColumnWidths(opts) {
213
216
 
214
217
  // Header chrome: the label shares its row with the sort caret and the
215
218
  // filter trigger, so the text needs less room than the cell does.
216
- var headerPadding = horizontalPadding(headCell);
217
219
  var trigger = table.querySelector('thead .xeplr-table-filter-trigger');
218
- if (trigger) headerPadding += trigger.offsetWidth + 4;
219
- else headerPadding += 16; // sort caret
220
+ var triggerBox = trigger && (trigger.closest('.xeplr-table-filter-wrapper') || trigger);
221
+ var headerPadding = headerChrome({
222
+ cell: horizontalPadding(headCell),
223
+ th: horizontalPadding(headCell && headCell.closest('th')),
224
+ gap: headCell ? parseFloat(window.getComputedStyle(headCell).columnGap) || 0 : 0,
225
+ trigger: triggerBox ? triggerBox.getBoundingClientRect().width : null
226
+ });
220
227
 
221
228
  // The table's own structural columns — expand arrow, select checkbox, row
222
229
  // actions — aren't content columns and don't compete for space. Their
@@ -250,7 +257,7 @@ export default function useColumnWidths(opts) {
250
257
  columns: columns,
251
258
  rows: rows,
252
259
  textOf: textOf,
253
- headerOf: headerTextOf,
260
+ headerOf: function(column) { return sortedHeaderText(column, sortedColumns); },
254
261
  keyOf: function(column) { return column.accessor; },
255
262
  measure: measureWidest,
256
263
  bodyFont: BODY_FONTS,
@@ -273,7 +280,7 @@ export default function useColumnWidths(opts) {
273
280
  if (onSizing) onSizing(next);
274
281
  // State is tracked through refs above, so it is deliberately absent here.
275
282
  // eslint-disable-next-line react-hooks/exhaustive-deps
276
- }, [enabled, containerWidth, data, columns, textOf, sampleRows, sizingKey, overrides,
283
+ }, [enabled, containerWidth, data, columns, textOf, sampleRows, sizingKey, sortedSignature, overrides,
277
284
  opts.minColumnWidth, opts.maxColumnWidth, opts.maxFloorWidth]);
278
285
 
279
286
  return { scrollRef: scrollRef, tableRef: tableRef, sizing: enabled ? sizing : null };
@@ -1025,6 +1025,8 @@
1025
1025
  .xeplr-r-tag-more { color: var(--xeplr-text-secondary, #8a93b8); font-size: 11px; align-self: center; }
1026
1026
 
1027
1027
  /* currency */
1028
+ .xeplr-r-trend { display: inline-flex; align-items: baseline; gap: 4px; font-weight: 600; font-variant-numeric: tabular-nums; white-space: nowrap; }
1029
+ .xeplr-r-trend-arrow { font-size: 0.8em; }
1028
1030
  .xeplr-r-currency { display: inline-flex; align-items: baseline; gap: 2px; }
1029
1031
  .xeplr-r-currency-bold { font-weight: 700; color: var(--xeplr-text-primary, #e6e9f5); }
1030
1032
  .xeplr-r-currency-sym { color: var(--xeplr-text-secondary, #8a93b8); font-size: 0.92em; }