nntc-ui 0.0.59 → 0.0.61

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 +38 -14
  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,19 @@ 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
+ const result = rowNumberColumnId ? sorted.map((row, index) => ({
3337
+ ...row,
3338
+ [rowNumberColumnId]: {
3339
+ value: index + 1
3340
+ }
3341
+ })) : sorted;
3318
3342
  window.dispatchEvent(
3319
3343
  new CustomEvent("filteringRows", {
3320
- detail: sorted
3344
+ detail: result
3321
3345
  })
3322
3346
  );
3323
- return sorted;
3324
- }, [rows, sortBy, filterBy]);
3347
+ return result;
3348
+ }, [rows, sortBy, filterBy, rowNumberColumnId]);
3325
3349
  const columnsEstimateSize = useMemo8(
3326
3350
  () => cellWidth ?? ((index) => memoizedColumns[index]?.meta?.width ?? 200),
3327
3351
  [cellWidth, memoizedColumns]
@@ -3335,7 +3359,7 @@ function VirtualTable(props) {
3335
3359
  getCoreRowModel: getCoreRowModel(),
3336
3360
  initialState: {
3337
3361
  columnPinning: {
3338
- left: memoizedColumns.filter((_, index) => index < fixedColumnsCount).map((mc) => mc.id).filter((id) => id).map((id) => id) ?? []
3362
+ left: memoizedColumns.filter((_, index) => index < fixedWithNumbersCount).map((mc) => mc.id).filter((id) => id).map((id) => id) ?? []
3339
3363
  }
3340
3364
  },
3341
3365
  meta: {
@@ -3357,7 +3381,7 @@ function VirtualTable(props) {
3357
3381
  horizontal: true,
3358
3382
  count: headerGroups[0]?.headers.length ?? 1,
3359
3383
  rangeExtractor: useCallback9((range) => {
3360
- const stickyIndexes = [...Array(fixedColumnsCount)].map((_, index) => index);
3384
+ const stickyIndexes = [...Array(fixedWithNumbersCount)].map((_, index) => index);
3361
3385
  const next = /* @__PURE__ */ new Set([...stickyIndexes, ...defaultRangeExtractor(range)]);
3362
3386
  return [...next].sort((a, b) => a - b);
3363
3387
  }, []),
@@ -3366,8 +3390,8 @@ function VirtualTable(props) {
3366
3390
  overscan: 5
3367
3391
  });
3368
3392
  const fixedColumnsWidth = useMemo8(
3369
- () => [...Array(fixedColumnsCount)].reduce((sum, _, index) => sum + columnsEstimateSize(index), 0),
3370
- [fixedColumnsCount, columnsEstimateSize]
3393
+ () => [...Array(fixedWithNumbersCount)].reduce((sum, _, index) => sum + columnsEstimateSize(index), 0),
3394
+ [fixedWithNumbersCount, columnsEstimateSize]
3371
3395
  );
3372
3396
  const headerGroupHeight = useMemo8(() => {
3373
3397
  const delta = headerGroups.length - headerRowHeight.length;
@@ -3437,7 +3461,7 @@ function VirtualTable(props) {
3437
3461
  const virtualColumns = columnVirtualizer.getVirtualItems();
3438
3462
  const lastVirtualColumnIndex = virtualColumns[virtualColumns.length - 1]?.index;
3439
3463
  const [before, after] = virtualColumns.length > 0 ? [
3440
- fixedColumnsCount && virtualColumns[fixedColumnsCount].index !== fixedColumnsCount ? virtualColumns[fixedColumnsCount].start - fixedColumnsWidth : virtualColumns[0].start,
3464
+ fixedWithNumbersCount && virtualColumns[fixedWithNumbersCount].index !== fixedWithNumbersCount ? virtualColumns[fixedWithNumbersCount].start - fixedColumnsWidth : virtualColumns[0].start,
3441
3465
  columnsTotalSize - virtualColumns[virtualColumns.length - 1].end
3442
3466
  ] : [0, 0];
3443
3467
  useEffect8(() => {
@@ -3517,7 +3541,7 @@ function VirtualTable(props) {
3517
3541
  ) });
3518
3542
  };
3519
3543
  return /* @__PURE__ */ jsxs19(Fragment8, { children: [
3520
- [...Array(fixedColumnsCount)].map((_, index) => {
3544
+ [...Array(fixedWithNumbersCount)].map((_, index) => {
3521
3545
  const header2 = headerGroup.headers[index];
3522
3546
  const nextHorizontalHeaders = getNextHorizontalHeaders(
3523
3547
  headerGroup.headers,
@@ -3548,7 +3572,7 @@ function VirtualTable(props) {
3548
3572
  );
3549
3573
  }),
3550
3574
  virtualColumns.map((column) => {
3551
- if (column.index > fixedColumnsCount - 1) {
3575
+ if (column.index > fixedWithNumbersCount - 1) {
3552
3576
  const header2 = headerGroup.headers[column.index];
3553
3577
  const nextHorizontalHeaders = getNextHorizontalHeaders(
3554
3578
  headerGroup.headers,
@@ -3585,7 +3609,7 @@ function VirtualTable(props) {
3585
3609
  width: `${thWidth}px`,
3586
3610
  zIndex,
3587
3611
  height: `${thHeight}px`,
3588
- transform: `translateX(${column.start + (!!fixedColumnsCount ? -2 : 0)}px) translateY(${translateY}px)`,
3612
+ transform: `translateX(${column.start + (!!fixedWithNumbersCount ? -2 : 0)}px) translateY(${translateY}px)`,
3589
3613
  borderTop: borders !== "none" && translateY === 0 ? "1px solid var(--border-color)" : void 0
3590
3614
  },
3591
3615
  children: innerCell(header2)
@@ -3602,7 +3626,7 @@ function VirtualTable(props) {
3602
3626
  style: {
3603
3627
  width: `${rowActionWidth}px`,
3604
3628
  height: `${headerGroupHeight[hgi]}px`,
3605
- transform: `translateX(${columnsTotalSize + (!!fixedColumnsCount ? -2 : 0)}px) translateY(${headerGroupY(hgi)}px)`
3629
+ transform: `translateX(${columnsTotalSize + (!!fixedWithNumbersCount ? -2 : 0)}px) translateY(${headerGroupY(hgi)}px)`
3606
3630
  }
3607
3631
  }
3608
3632
  )
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nntc-ui",
3
- "version": "0.0.59",
3
+ "version": "0.0.61",
4
4
  "author": "NNTC",
5
5
  "description": "React UI-kit for NNTC",
6
6
  "type": "module",