nntc-ui 0.0.59 → 0.0.60

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.
Files changed (3) hide show
  1. package/index.d.ts +1 -0
  2. package/index.js +37 -12
  3. package/package.json +1 -1
package/index.d.ts CHANGED
@@ -329,6 +329,7 @@ interface Props$8 {
329
329
  onFiltersChange?: (filters: FilterBy[]) => void;
330
330
  onSortsChange?: (sorts: SortBy[]) => void;
331
331
  additionalHeaderContent?: (headerName: string) => ReactNode;
332
+ showRowsNumbers?: boolean;
332
333
  }
333
334
  declare function VirtualTable(props: UiProps<Props$8>): react_jsx_runtime.JSX.Element;
334
335
 
package/index.js CHANGED
@@ -2536,6 +2536,7 @@ import { defaultRangeExtractor, useVirtualizer } from "@tanstack/react-virtual";
2536
2536
  import classnames22 from "classnames";
2537
2537
  import dayjs4 from "dayjs";
2538
2538
  import { Fragment as Fragment8, useCallback as useCallback9, useEffect as useEffect8, useMemo as useMemo8, useRef as useRef11, useState as useState15 } from "react";
2539
+ import { v4 as uuidv46 } from "uuid";
2539
2540
 
2540
2541
  // src/utils/toFirstLetterUpperCase.ts
2541
2542
  var toFirstLetterUpperCase = (name) => {
@@ -3295,12 +3296,29 @@ function VirtualTable(props) {
3295
3296
  globalSorts,
3296
3297
  onFiltersChange,
3297
3298
  onSortsChange,
3298
- additionalHeaderContent
3299
+ additionalHeaderContent,
3300
+ showRowsNumbers
3299
3301
  } = props;
3302
+ const fixedWithNumbersCount = fixedColumnsCount + (showRowsNumbers ? 1 : 0);
3300
3303
  const [filterBy, setFilterBy] = useState15(globalFilters ?? []);
3301
3304
  const [sortBy, setSortBy] = useState15([...globalSorts ?? [], { columnName: "isNew", direction: "desc" }]);
3302
3305
  const tableContainerRef = useRef11(null);
3303
- const memoizedColumns = useMemo8(() => getColumns(columns), [columns]);
3306
+ const rowNumberColumnId = useMemo8(() => showRowsNumbers ? `rowNumber_${uuidv46()}` : null, [showRowsNumbers]);
3307
+ const memoizedColumns = useMemo8(() => {
3308
+ if (!rowNumberColumnId) {
3309
+ return getColumns(columns);
3310
+ }
3311
+ const buildRowsNumbersColumn = (template, isRoot = false) => ({
3312
+ id: rowNumberColumnId,
3313
+ ...isRoot ? { width: 50 } : void 0,
3314
+ ...!template?.columns?.length ? { accessorKey: rowNumberColumnId } : void 0,
3315
+ label: "#",
3316
+ columns: template?.columns?.map((child) => buildRowsNumbersColumn(child, false))
3317
+ });
3318
+ const nextColumns = [buildRowsNumbersColumn(columns[0], true), ...columns];
3319
+ const result = getColumns(nextColumns);
3320
+ return result;
3321
+ }, [columns, rowNumberColumnId]);
3304
3322
  const memoizedData = useMemo8(() => {
3305
3323
  const newRows = [...rows];
3306
3324
  const nextFilterBy = filterBy.filter((filterByItem) => {
@@ -3315,13 +3333,20 @@ function VirtualTable(props) {
3315
3333
  (fb) => fb.type === "numberRange" && fb.values.some((v) => v !== Infinity && v !== -Infinity) || fb.type === "dateRange" && fb.values.some((v) => !dayjs4(v).isSame(dayjs4(0)) && !dayjs4(v).isSame(dayjs4(Number.MAX_SAFE_INTEGER)))
3316
3334
  ) ? [] : newRows;
3317
3335
  const sorted = sortBy.length ? recursiveSort(filtered, sortBy) : filtered;
3336
+ if (rowNumberColumnId) {
3337
+ sorted.forEach((row, index) => {
3338
+ row[rowNumberColumnId] = {
3339
+ value: index + 1
3340
+ };
3341
+ });
3342
+ }
3318
3343
  window.dispatchEvent(
3319
3344
  new CustomEvent("filteringRows", {
3320
3345
  detail: sorted
3321
3346
  })
3322
3347
  );
3323
3348
  return sorted;
3324
- }, [rows, sortBy, filterBy]);
3349
+ }, [rows, sortBy, filterBy, rowNumberColumnId]);
3325
3350
  const columnsEstimateSize = useMemo8(
3326
3351
  () => cellWidth ?? ((index) => memoizedColumns[index]?.meta?.width ?? 200),
3327
3352
  [cellWidth, memoizedColumns]
@@ -3335,7 +3360,7 @@ function VirtualTable(props) {
3335
3360
  getCoreRowModel: getCoreRowModel(),
3336
3361
  initialState: {
3337
3362
  columnPinning: {
3338
- left: memoizedColumns.filter((_, index) => index < fixedColumnsCount).map((mc) => mc.id).filter((id) => id).map((id) => id) ?? []
3363
+ left: memoizedColumns.filter((_, index) => index < fixedWithNumbersCount).map((mc) => mc.id).filter((id) => id).map((id) => id) ?? []
3339
3364
  }
3340
3365
  },
3341
3366
  meta: {
@@ -3357,7 +3382,7 @@ function VirtualTable(props) {
3357
3382
  horizontal: true,
3358
3383
  count: headerGroups[0]?.headers.length ?? 1,
3359
3384
  rangeExtractor: useCallback9((range) => {
3360
- const stickyIndexes = [...Array(fixedColumnsCount)].map((_, index) => index);
3385
+ const stickyIndexes = [...Array(fixedWithNumbersCount)].map((_, index) => index);
3361
3386
  const next = /* @__PURE__ */ new Set([...stickyIndexes, ...defaultRangeExtractor(range)]);
3362
3387
  return [...next].sort((a, b) => a - b);
3363
3388
  }, []),
@@ -3366,8 +3391,8 @@ function VirtualTable(props) {
3366
3391
  overscan: 5
3367
3392
  });
3368
3393
  const fixedColumnsWidth = useMemo8(
3369
- () => [...Array(fixedColumnsCount)].reduce((sum, _, index) => sum + columnsEstimateSize(index), 0),
3370
- [fixedColumnsCount, columnsEstimateSize]
3394
+ () => [...Array(fixedWithNumbersCount)].reduce((sum, _, index) => sum + columnsEstimateSize(index), 0),
3395
+ [fixedWithNumbersCount, columnsEstimateSize]
3371
3396
  );
3372
3397
  const headerGroupHeight = useMemo8(() => {
3373
3398
  const delta = headerGroups.length - headerRowHeight.length;
@@ -3437,7 +3462,7 @@ function VirtualTable(props) {
3437
3462
  const virtualColumns = columnVirtualizer.getVirtualItems();
3438
3463
  const lastVirtualColumnIndex = virtualColumns[virtualColumns.length - 1]?.index;
3439
3464
  const [before, after] = virtualColumns.length > 0 ? [
3440
- fixedColumnsCount && virtualColumns[fixedColumnsCount].index !== fixedColumnsCount ? virtualColumns[fixedColumnsCount].start - fixedColumnsWidth : virtualColumns[0].start,
3465
+ fixedWithNumbersCount && virtualColumns[fixedWithNumbersCount].index !== fixedWithNumbersCount ? virtualColumns[fixedWithNumbersCount].start - fixedColumnsWidth : virtualColumns[0].start,
3441
3466
  columnsTotalSize - virtualColumns[virtualColumns.length - 1].end
3442
3467
  ] : [0, 0];
3443
3468
  useEffect8(() => {
@@ -3517,7 +3542,7 @@ function VirtualTable(props) {
3517
3542
  ) });
3518
3543
  };
3519
3544
  return /* @__PURE__ */ jsxs19(Fragment8, { children: [
3520
- [...Array(fixedColumnsCount)].map((_, index) => {
3545
+ [...Array(fixedWithNumbersCount)].map((_, index) => {
3521
3546
  const header2 = headerGroup.headers[index];
3522
3547
  const nextHorizontalHeaders = getNextHorizontalHeaders(
3523
3548
  headerGroup.headers,
@@ -3548,7 +3573,7 @@ function VirtualTable(props) {
3548
3573
  );
3549
3574
  }),
3550
3575
  virtualColumns.map((column) => {
3551
- if (column.index > fixedColumnsCount - 1) {
3576
+ if (column.index > fixedWithNumbersCount - 1) {
3552
3577
  const header2 = headerGroup.headers[column.index];
3553
3578
  const nextHorizontalHeaders = getNextHorizontalHeaders(
3554
3579
  headerGroup.headers,
@@ -3585,7 +3610,7 @@ function VirtualTable(props) {
3585
3610
  width: `${thWidth}px`,
3586
3611
  zIndex,
3587
3612
  height: `${thHeight}px`,
3588
- transform: `translateX(${column.start + (!!fixedColumnsCount ? -2 : 0)}px) translateY(${translateY}px)`,
3613
+ transform: `translateX(${column.start + (!!fixedWithNumbersCount ? -2 : 0)}px) translateY(${translateY}px)`,
3589
3614
  borderTop: borders !== "none" && translateY === 0 ? "1px solid var(--border-color)" : void 0
3590
3615
  },
3591
3616
  children: innerCell(header2)
@@ -3602,7 +3627,7 @@ function VirtualTable(props) {
3602
3627
  style: {
3603
3628
  width: `${rowActionWidth}px`,
3604
3629
  height: `${headerGroupHeight[hgi]}px`,
3605
- transform: `translateX(${columnsTotalSize + (!!fixedColumnsCount ? -2 : 0)}px) translateY(${headerGroupY(hgi)}px)`
3630
+ transform: `translateX(${columnsTotalSize + (!!fixedWithNumbersCount ? -2 : 0)}px) translateY(${headerGroupY(hgi)}px)`
3606
3631
  }
3607
3632
  }
3608
3633
  )
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nntc-ui",
3
- "version": "0.0.59",
3
+ "version": "0.0.60",
4
4
  "author": "NNTC",
5
5
  "description": "React UI-kit for NNTC",
6
6
  "type": "module",