react-virtual-merged-list 1.0.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Nilesh Ingale
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,125 @@
1
+ # React Virtual Merged List
2
+
3
+ A high-performance React component for rendering large, virtualized lists with **merged cells** (rowspans).
4
+
5
+ Built on top of `react-window`, this library combines the rendering speed of virtualization with the complex layout capabilities of data grids, automatically calculating rowspans based on your data.
6
+
7
+ ![License](https://img.shields.io/npm/l/react-virtual-merged-list)
8
+ ![Version](https://img.shields.io/npm/v/react-virtual-merged-list)
9
+ ![Size](https://img.shields.io/bundlephobia/minzip/react-virtual-merged-list)
10
+
11
+ ## Features
12
+
13
+ * πŸš€ **Virtualized Rendering**: Efficiently handles thousands of rows without performance lag.
14
+ * 🧩 **Automatic Cell Merging**: Automatically calculates and renders rowspans based on duplicate data in your columns.
15
+ * 🎨 **Flexible Layout**: Define columns, widths, custom renderers, and styles easily.
16
+ * πŸ›‘οΈ **TypeScript Native**: Fully typed for a robust developer experience.
17
+ * πŸ“¦ **Lightweight**: Zero bloatβ€”it relies on `react-window` as a peer dependency.
18
+
19
+ ## Installation
20
+
21
+ ```bash
22
+ npm install react-virtual-merged-list
23
+ ```
24
+
25
+ ### Peer Dependencies
26
+
27
+ This library depends on `react` and `react-window`. Ensure they are installed in your project:
28
+
29
+ ```bash
30
+ npm install react-window
31
+ ```
32
+
33
+ ## Usage
34
+
35
+ Here is a simple example showing how to render a list with merged "Department" and "Team" cells.
36
+
37
+ ```tsx
38
+ import React from 'react';
39
+ import { MergedList } from 'react-virtual-merged-list';
40
+
41
+ const App = () => {
42
+ // 1. Prepare your data (Must be sorted by the keys you want to merge!)
43
+ const data = [
44
+ { id: 1, department: 'Engineering', team: 'Backend', name: 'Alice' },
45
+ { id: 2, department: 'Engineering', team: 'Backend', name: 'Bob' },
46
+ { id: 3, department: 'Engineering', team: 'Frontend', name: 'Charlie' },
47
+ { id: 4, department: 'Sales', team: 'Enterprise', name: 'David' },
48
+ { id: 5, department: 'Sales', team: 'Enterprise', name: 'Eve' },
49
+ ];
50
+
51
+ return (
52
+ <MergedList
53
+ data={data}
54
+ rowHeight={50}
55
+ height={600}
56
+ width="100%" // or a fixed number like 800
57
+ columns={[
58
+ {
59
+ key: 'department',
60
+ width: 150,
61
+ style: { fontWeight: 'bold', background: '#f5f5f5', color: '#333' }
62
+ },
63
+ {
64
+ key: 'team',
65
+ width: 150,
66
+ style: { background: '#fafafa' }
67
+ },
68
+ {
69
+ key: 'name',
70
+ width: 200,
71
+ // Custom render function for complex content
72
+ render: (row) => (
73
+ <span style={{ color: 'blue', paddingLeft: 10 }}>
74
+ πŸ‘€ {row.name}
75
+ </span>
76
+ )
77
+ }
78
+ ]}
79
+ />
80
+ );
81
+ };
82
+
83
+ export default App;
84
+ ```
85
+
86
+ ## Props
87
+
88
+ | Prop | Type | Required | Description |
89
+ | --- | --- | --- | --- |
90
+ | `data` | `any[]` | **Yes** | The array of data objects to render. **Note:** Data should be sorted by the keys you intend to merge for correct visualization. |
91
+ | `columns` | `ColumnDef[]` | **Yes** | Configuration for the grid columns (see below). |
92
+ | `rowHeight` | `number` | **Yes** | Fixed height of each row in pixels. |
93
+ | `height` | `number` | **Yes** | Height of the scrollable container. |
94
+ | `width` | `number\|string` | **Yes** | Width of the container. |
95
+
96
+ ### Column Configuration (`ColumnDef`)
97
+
98
+ The `columns` prop takes an array of objects with the following properties:
99
+
100
+ ```typescript
101
+ interface ColumnDef {
102
+ /** The key in your data object to bind to this column */
103
+ key: string;
104
+
105
+ /** Width of the column in pixels */
106
+ width: number;
107
+
108
+ /** Optional: Custom render function for cell content */
109
+ render?: (row: any) => React.ReactNode;
110
+
111
+ /** Optional: Inline styles applied to the cell container */
112
+ style?: React.CSSProperties;
113
+ }
114
+ ```
115
+
116
+ ## How It Works
117
+
118
+ 1. The component analyzes your `data` and the keys found in `columns`.
119
+ 2. It calculates `rowSpan` values for consecutive identical values in those columns.
120
+ 3. It uses `react-window` to render only the visible rows.
121
+ 4. It uses absolute positioning to render merged cells ("Phantom Spacers" are handled internally, so you don't have to worry about flexbox layouts breaking).
122
+
123
+ ## License
124
+
125
+ MIT Β© Nilesh Ingale
@@ -0,0 +1,25 @@
1
+ import React from 'react';
2
+
3
+ interface ColumnDef {
4
+ key: string;
5
+ width: number;
6
+ render?: (row: any) => React.ReactNode;
7
+ style?: React.CSSProperties;
8
+ }
9
+ interface MergedListProps {
10
+ data: any[];
11
+ columns: ColumnDef[];
12
+ rowHeight: number;
13
+ height: number;
14
+ width: number | string;
15
+ }
16
+ declare const MergedList: ({ data, columns, rowHeight, height, width, }: MergedListProps) => React.JSX.Element;
17
+
18
+ type SpanMap = Record<string, {
19
+ span: number;
20
+ isVisible: boolean;
21
+ refRow?: number;
22
+ }>;
23
+ declare const calculateSpans: (data: any[], groupKeys: string[]) => SpanMap;
24
+
25
+ export { MergedList, calculateSpans };
@@ -0,0 +1,25 @@
1
+ import React from 'react';
2
+
3
+ interface ColumnDef {
4
+ key: string;
5
+ width: number;
6
+ render?: (row: any) => React.ReactNode;
7
+ style?: React.CSSProperties;
8
+ }
9
+ interface MergedListProps {
10
+ data: any[];
11
+ columns: ColumnDef[];
12
+ rowHeight: number;
13
+ height: number;
14
+ width: number | string;
15
+ }
16
+ declare const MergedList: ({ data, columns, rowHeight, height, width, }: MergedListProps) => React.JSX.Element;
17
+
18
+ type SpanMap = Record<string, {
19
+ span: number;
20
+ isVisible: boolean;
21
+ refRow?: number;
22
+ }>;
23
+ declare const calculateSpans: (data: any[], groupKeys: string[]) => SpanMap;
24
+
25
+ export { MergedList, calculateSpans };
package/dist/index.js ADDED
@@ -0,0 +1,148 @@
1
+ var __create = Object.create;
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __getProtoOf = Object.getPrototypeOf;
6
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
7
+ var __export = (target, all) => {
8
+ for (var name in all)
9
+ __defProp(target, name, { get: all[name], enumerable: true });
10
+ };
11
+ var __copyProps = (to, from, except, desc) => {
12
+ if (from && typeof from === "object" || typeof from === "function") {
13
+ for (let key of __getOwnPropNames(from))
14
+ if (!__hasOwnProp.call(to, key) && key !== except)
15
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
16
+ }
17
+ return to;
18
+ };
19
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
20
+ // If the importer is in node compatibility mode or this is not an ESM
21
+ // file that has been converted to a CommonJS file using a Babel-
22
+ // compatible transform (i.e. "__esModule" has not been set), then set
23
+ // "default" to the CommonJS "module.exports" for node compatibility.
24
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
25
+ mod
26
+ ));
27
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
28
+
29
+ // src/index.ts
30
+ var index_exports = {};
31
+ __export(index_exports, {
32
+ MergedList: () => MergedList,
33
+ calculateSpans: () => calculateSpans
34
+ });
35
+ module.exports = __toCommonJS(index_exports);
36
+
37
+ // src/MergedList.tsx
38
+ var import_react = __toESM(require("react"));
39
+ var ReactWindowNamespace = __toESM(require("react-window"));
40
+
41
+ // src/utils/spanManager.ts
42
+ var calculateSpans = (data, groupKeys) => {
43
+ const spanMap = {};
44
+ const lastSeenValues = {};
45
+ const lastStartIndices = {};
46
+ data.forEach((row, rowIndex) => {
47
+ groupKeys.forEach((key) => {
48
+ const cellId = `${rowIndex}:${key}`;
49
+ const currentValue = row[key];
50
+ if (rowIndex > 0 && currentValue === lastSeenValues[key]) {
51
+ const parentRowIndex = lastStartIndices[key];
52
+ const parentId = `${parentRowIndex}:${key}`;
53
+ if (!spanMap[parentId])
54
+ spanMap[parentId] = { span: 1, isVisible: true };
55
+ spanMap[parentId].span += 1;
56
+ spanMap[cellId] = { span: 0, isVisible: false, refRow: parentRowIndex };
57
+ } else {
58
+ lastSeenValues[key] = currentValue;
59
+ lastStartIndices[key] = rowIndex;
60
+ spanMap[cellId] = { span: 1, isVisible: true };
61
+ }
62
+ });
63
+ });
64
+ return spanMap;
65
+ };
66
+
67
+ // src/MergedList.tsx
68
+ var ReactWindow = ReactWindowNamespace;
69
+ var _a, _b;
70
+ var ListComponent = ReactWindow.VariableSizeList || ReactWindow.List || ((_a = ReactWindow.default) == null ? void 0 : _a.VariableSizeList) || ((_b = ReactWindow.default) == null ? void 0 : _b.List);
71
+ if (!ListComponent) {
72
+ console.error(
73
+ "\u274C Critical Error: Could not find 'List' or 'VariableSizeList' in react-window exports.",
74
+ ReactWindowNamespace
75
+ );
76
+ }
77
+ var MergedList = ({
78
+ data,
79
+ columns,
80
+ rowHeight,
81
+ height,
82
+ width
83
+ }) => {
84
+ const mergeKeys = (0, import_react.useMemo)(() => columns.map((c) => c.key), [columns]);
85
+ const spanMap = (0, import_react.useMemo)(
86
+ () => calculateSpans(data, mergeKeys),
87
+ [data, mergeKeys]
88
+ );
89
+ const Row = ({ index, style }) => {
90
+ const row = data[index];
91
+ return /* @__PURE__ */ import_react.default.createElement("div", { style: { ...style, position: "absolute" } }, " ", columns.reduce(
92
+ (acc, col) => {
93
+ const { key, width: width2, render } = col;
94
+ const currentLeft = acc.leftOffset;
95
+ const spanData = spanMap[`${index}:${key}`];
96
+ const isVisible = spanData ? spanData.isVisible : true;
97
+ const spanCount = spanData ? spanData.span : 1;
98
+ if (isVisible) {
99
+ acc.elements.push(
100
+ /* @__PURE__ */ import_react.default.createElement(
101
+ "div",
102
+ {
103
+ key,
104
+ style: {
105
+ position: "absolute",
106
+ left: currentLeft,
107
+ // πŸ“ PINNED POSITION
108
+ width: width2,
109
+ height: spanCount * rowHeight,
110
+ // πŸ“ Grow downwards
111
+ zIndex: spanCount > 1 ? 10 : 1,
112
+ // Float on top if merged
113
+ background: "white",
114
+ // Important to cover rows below
115
+ borderRight: "1px solid #eee",
116
+ borderBottom: "1px solid #eee",
117
+ display: "flex",
118
+ alignItems: "center",
119
+ ...col.style
120
+ }
121
+ },
122
+ render ? render(row) : row[key]
123
+ )
124
+ );
125
+ }
126
+ acc.leftOffset += width2;
127
+ return acc;
128
+ },
129
+ { elements: [], leftOffset: 0 }
130
+ ).elements);
131
+ };
132
+ return /* @__PURE__ */ import_react.default.createElement(
133
+ ListComponent,
134
+ {
135
+ height,
136
+ itemCount: data.length,
137
+ itemSize: () => rowHeight,
138
+ width
139
+ },
140
+ Row
141
+ );
142
+ };
143
+ // Annotate the CommonJS export names for ESM import in node:
144
+ 0 && (module.exports = {
145
+ MergedList,
146
+ calculateSpans
147
+ });
148
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/index.ts","../src/MergedList.tsx","../src/utils/spanManager.ts"],"sourcesContent":["export { MergedList } from './MergedList';\r\nexport { calculateSpans } from './utils/spanManager';","import React, { useMemo } from \"react\";\r\n\r\nimport * as ReactWindowNamespace from \"react-window\";\r\n\r\n// πŸ› οΈ FIX: Handle different export names dynamically\r\nconst ReactWindow = ReactWindowNamespace as any;\r\n\r\n// Try to find the List component in this order:\r\n// 1. VariableSizeList (Standard v1.8)\r\n// 2. List (What you are seeing in your console)\r\n// 3. default.VariableSizeList (CommonJS fallback)\r\nconst ListComponent =\r\n ReactWindow.VariableSizeList ||\r\n ReactWindow.List ||\r\n ReactWindow.default?.VariableSizeList ||\r\n ReactWindow.default?.List;\r\n\r\nif (!ListComponent) {\r\n console.error(\r\n \"❌ Critical Error: Could not find 'List' or 'VariableSizeList' in react-window exports.\",\r\n ReactWindowNamespace\r\n );\r\n}\r\n\r\nimport { calculateSpans } from \"./utils/spanManager\";\r\n\r\ninterface ColumnDef {\r\n key: string;\r\n width: number;\r\n render?: (row: any) => React.ReactNode; // User can provide custom content\r\n style?: React.CSSProperties;\r\n}\r\n\r\ninterface MergedListProps {\r\n data: any[];\r\n columns: ColumnDef[]; // <--- New Prop\r\n rowHeight: number;\r\n height: number;\r\n width: number | string;\r\n}\r\n\r\nexport const MergedList = ({\r\n data,\r\n columns,\r\n rowHeight,\r\n height,\r\n width,\r\n}: MergedListProps) => {\r\n // 1. Identify which keys need span calculation\r\n const mergeKeys = useMemo(() => columns.map((c) => c.key), [columns]);\r\n const spanMap = useMemo(\r\n () => calculateSpans(data, mergeKeys),\r\n [data, mergeKeys]\r\n );\r\n\r\n // 2. The Internal Row Renderer\r\n const Row = ({ index, style }: any) => {\r\n const row = data[index];\r\n\r\n // We render a container, but the cells inside are ABSOLUTE positioned\r\n return (\r\n <div style={{ ...style, position: \"absolute\" }}>\r\n {\" \"}\r\n {/* Keep react-window's position */}\r\n {\r\n columns.reduce(\r\n (acc, col) => {\r\n const { key, width, render } = col;\r\n const currentLeft = acc.leftOffset; // Where does this column start?\r\n\r\n const spanData = spanMap[`${index}:${key}`];\r\n const isVisible = spanData ? spanData.isVisible : true; // Default true if not merged\r\n const spanCount = spanData ? spanData.span : 1;\r\n\r\n // LOGIC: Only render if it's the start of a span (or not a merge column)\r\n if (isVisible) {\r\n acc.elements.push(\r\n <div\r\n key={key}\r\n style={{\r\n position: \"absolute\",\r\n left: currentLeft, // πŸ“ PINNED POSITION\r\n width: width,\r\n height: spanCount * rowHeight, // πŸ“ Grow downwards\r\n zIndex: spanCount > 1 ? 10 : 1, // Float on top if merged\r\n background: \"white\", // Important to cover rows below\r\n borderRight: \"1px solid #eee\",\r\n borderBottom: \"1px solid #eee\",\r\n display: \"flex\",\r\n alignItems: \"center\",\r\n ...col.style,\r\n }}\r\n >\r\n {/* Render user content or default text */}\r\n {render ? render(row) : row[key]}\r\n </div>\r\n );\r\n }\r\n\r\n // Move the cursor to the right for the next column\r\n // We add width even if we didn't render anything!\r\n acc.leftOffset += width;\r\n\r\n return acc;\r\n },\r\n { elements: [] as React.ReactNode[], leftOffset: 0 }\r\n ).elements\r\n }\r\n </div>\r\n );\r\n };\r\n\r\n return (\r\n <ListComponent\r\n height={height}\r\n itemCount={data.length}\r\n itemSize={() => rowHeight}\r\n width={width}\r\n >\r\n {Row}\r\n </ListComponent>\r\n );\r\n};\r\n","export type SpanMap = Record<\r\n string,\r\n { span: number; isVisible: boolean; refRow?: number }\r\n>;\r\n\r\nexport const calculateSpans = (data: any[], groupKeys: string[]): SpanMap => {\r\n const spanMap: SpanMap = {};\r\n\r\n // Initialize pointers for each column (groupKey)\r\n const lastSeenValues: Record<string, any> = {};\r\n const lastStartIndices: Record<string, number> = {};\r\n\r\n data.forEach((row, rowIndex) => {\r\n groupKeys.forEach((key) => {\r\n const cellId = `${rowIndex}:${key}`;\r\n const currentValue = row[key];\r\n\r\n // If it matches the previous row's value, extend the span\r\n if (rowIndex > 0 && currentValue === lastSeenValues[key]) {\r\n const parentRowIndex = lastStartIndices[key];\r\n const parentId = `${parentRowIndex}:${key}`;\r\n\r\n // Increment parent span\r\n if (!spanMap[parentId])\r\n spanMap[parentId] = { span: 1, isVisible: true };\r\n spanMap[parentId].span += 1;\r\n\r\n // Mark current cell as hidden\r\n spanMap[cellId] = { span: 0, isVisible: false, refRow: parentRowIndex };\r\n } else {\r\n // New Value: Start a new span\r\n lastSeenValues[key] = currentValue;\r\n lastStartIndices[key] = rowIndex;\r\n spanMap[cellId] = { span: 1, isVisible: true };\r\n }\r\n });\r\n });\r\n\r\n return spanMap;\r\n};\r\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,mBAA+B;AAE/B,2BAAsC;;;ACG/B,IAAM,iBAAiB,CAAC,MAAa,cAAiC;AAC3E,QAAM,UAAmB,CAAC;AAG1B,QAAM,iBAAsC,CAAC;AAC7C,QAAM,mBAA2C,CAAC;AAElD,OAAK,QAAQ,CAAC,KAAK,aAAa;AAC9B,cAAU,QAAQ,CAAC,QAAQ;AACzB,YAAM,SAAS,GAAG,QAAQ,IAAI,GAAG;AACjC,YAAM,eAAe,IAAI,GAAG;AAG5B,UAAI,WAAW,KAAK,iBAAiB,eAAe,GAAG,GAAG;AACxD,cAAM,iBAAiB,iBAAiB,GAAG;AAC3C,cAAM,WAAW,GAAG,cAAc,IAAI,GAAG;AAGzC,YAAI,CAAC,QAAQ,QAAQ;AACnB,kBAAQ,QAAQ,IAAI,EAAE,MAAM,GAAG,WAAW,KAAK;AACjD,gBAAQ,QAAQ,EAAE,QAAQ;AAG1B,gBAAQ,MAAM,IAAI,EAAE,MAAM,GAAG,WAAW,OAAO,QAAQ,eAAe;AAAA,MACxE,OAAO;AAEL,uBAAe,GAAG,IAAI;AACtB,yBAAiB,GAAG,IAAI;AACxB,gBAAQ,MAAM,IAAI,EAAE,MAAM,GAAG,WAAW,KAAK;AAAA,MAC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Aa;AAAA,kBACb,cAAc;AAAA,kBACd,SAAS;AAAA,kBACT,YAAY;AAAA,kBACZ,GAAG,IAAI;AAAA,gBACT;AAAA;AAAA,cAGC,SAAS,OAAO,GAAG,IAAI,IAAI,GAAG;AAAA,YACjC;AAAA,UACF;AAAA,QACF;AAIA,YAAI,cAAcA;AAElB,eAAO;AAAA,MACT;AAAA,MACA,EAAE,UAAU,CAAC,GAAwB,YAAY,EAAE;AAAA,IACrD,EAAE,QAEN;AAAA,EAEJ;AAEA,SACE,6BAAAD,QAAA;AAAA,IAAC;AAAA;AAAA,MACC;AAAA,MACA,WAAW,KAAK;AAAA,MAChB,UAAU,MAAM;AAAA,MAChB;AAAA;AAAA,IAEC;AAAA,EACH;AAEJ;","names":["React","width"]}
package/dist/index.mjs ADDED
@@ -0,0 +1,111 @@
1
+ // src/MergedList.tsx
2
+ import React, { useMemo } from "react";
3
+ import * as ReactWindowNamespace from "react-window";
4
+
5
+ // src/utils/spanManager.ts
6
+ var calculateSpans = (data, groupKeys) => {
7
+ const spanMap = {};
8
+ const lastSeenValues = {};
9
+ const lastStartIndices = {};
10
+ data.forEach((row, rowIndex) => {
11
+ groupKeys.forEach((key) => {
12
+ const cellId = `${rowIndex}:${key}`;
13
+ const currentValue = row[key];
14
+ if (rowIndex > 0 && currentValue === lastSeenValues[key]) {
15
+ const parentRowIndex = lastStartIndices[key];
16
+ const parentId = `${parentRowIndex}:${key}`;
17
+ if (!spanMap[parentId])
18
+ spanMap[parentId] = { span: 1, isVisible: true };
19
+ spanMap[parentId].span += 1;
20
+ spanMap[cellId] = { span: 0, isVisible: false, refRow: parentRowIndex };
21
+ } else {
22
+ lastSeenValues[key] = currentValue;
23
+ lastStartIndices[key] = rowIndex;
24
+ spanMap[cellId] = { span: 1, isVisible: true };
25
+ }
26
+ });
27
+ });
28
+ return spanMap;
29
+ };
30
+
31
+ // src/MergedList.tsx
32
+ var ReactWindow = ReactWindowNamespace;
33
+ var _a, _b;
34
+ var ListComponent = ReactWindow.VariableSizeList || ReactWindow.List || ((_a = ReactWindow.default) == null ? void 0 : _a.VariableSizeList) || ((_b = ReactWindow.default) == null ? void 0 : _b.List);
35
+ if (!ListComponent) {
36
+ console.error(
37
+ "\u274C Critical Error: Could not find 'List' or 'VariableSizeList' in react-window exports.",
38
+ ReactWindowNamespace
39
+ );
40
+ }
41
+ var MergedList = ({
42
+ data,
43
+ columns,
44
+ rowHeight,
45
+ height,
46
+ width
47
+ }) => {
48
+ const mergeKeys = useMemo(() => columns.map((c) => c.key), [columns]);
49
+ const spanMap = useMemo(
50
+ () => calculateSpans(data, mergeKeys),
51
+ [data, mergeKeys]
52
+ );
53
+ const Row = ({ index, style }) => {
54
+ const row = data[index];
55
+ return /* @__PURE__ */ React.createElement("div", { style: { ...style, position: "absolute" } }, " ", columns.reduce(
56
+ (acc, col) => {
57
+ const { key, width: width2, render } = col;
58
+ const currentLeft = acc.leftOffset;
59
+ const spanData = spanMap[`${index}:${key}`];
60
+ const isVisible = spanData ? spanData.isVisible : true;
61
+ const spanCount = spanData ? spanData.span : 1;
62
+ if (isVisible) {
63
+ acc.elements.push(
64
+ /* @__PURE__ */ React.createElement(
65
+ "div",
66
+ {
67
+ key,
68
+ style: {
69
+ position: "absolute",
70
+ left: currentLeft,
71
+ // πŸ“ PINNED POSITION
72
+ width: width2,
73
+ height: spanCount * rowHeight,
74
+ // πŸ“ Grow downwards
75
+ zIndex: spanCount > 1 ? 10 : 1,
76
+ // Float on top if merged
77
+ background: "white",
78
+ // Important to cover rows below
79
+ borderRight: "1px solid #eee",
80
+ borderBottom: "1px solid #eee",
81
+ display: "flex",
82
+ alignItems: "center",
83
+ ...col.style
84
+ }
85
+ },
86
+ render ? render(row) : row[key]
87
+ )
88
+ );
89
+ }
90
+ acc.leftOffset += width2;
91
+ return acc;
92
+ },
93
+ { elements: [], leftOffset: 0 }
94
+ ).elements);
95
+ };
96
+ return /* @__PURE__ */ React.createElement(
97
+ ListComponent,
98
+ {
99
+ height,
100
+ itemCount: data.length,
101
+ itemSize: () => rowHeight,
102
+ width
103
+ },
104
+ Row
105
+ );
106
+ };
107
+ export {
108
+ MergedList,
109
+ calculateSpans
110
+ };
111
+ //# sourceMappingURL=index.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/MergedList.tsx","../src/utils/spanManager.ts"],"sourcesContent":["import React, { useMemo } from \"react\";\r\n\r\nimport * as ReactWindowNamespace from \"react-window\";\r\n\r\n// πŸ› οΈ FIX: Handle different export names dynamically\r\nconst ReactWindow = ReactWindowNamespace as any;\r\n\r\n// Try to find the List component in this order:\r\n// 1. VariableSizeList (Standard v1.8)\r\n// 2. List (What you are seeing in your console)\r\n// 3. default.VariableSizeList (CommonJS fallback)\r\nconst ListComponent =\r\n ReactWindow.VariableSizeList ||\r\n ReactWindow.List ||\r\n ReactWindow.default?.VariableSizeList ||\r\n ReactWindow.default?.List;\r\n\r\nif (!ListComponent) {\r\n console.error(\r\n \"❌ Critical Error: Could not find 'List' or 'VariableSizeList' in react-window exports.\",\r\n ReactWindowNamespace\r\n );\r\n}\r\n\r\nimport { calculateSpans } from \"./utils/spanManager\";\r\n\r\ninterface ColumnDef {\r\n key: string;\r\n width: number;\r\n render?: (row: any) => React.ReactNode; // User can provide custom content\r\n style?: React.CSSProperties;\r\n}\r\n\r\ninterface MergedListProps {\r\n data: any[];\r\n columns: ColumnDef[]; // <--- New Prop\r\n rowHeight: number;\r\n height: number;\r\n width: number | string;\r\n}\r\n\r\nexport const MergedList = ({\r\n data,\r\n columns,\r\n rowHeight,\r\n height,\r\n width,\r\n}: MergedListProps) => {\r\n // 1. Identify which keys need span calculation\r\n const mergeKeys = useMemo(() => columns.map((c) => c.key), [columns]);\r\n const spanMap = useMemo(\r\n () => calculateSpans(data, mergeKeys),\r\n [data, mergeKeys]\r\n );\r\n\r\n // 2. The Internal Row Renderer\r\n const Row = ({ index, style }: any) => {\r\n const row = data[index];\r\n\r\n // We render a container, but the cells inside are ABSOLUTE positioned\r\n return (\r\n <div style={{ ...style, position: \"absolute\" }}>\r\n {\" \"}\r\n {/* Keep react-window's position */}\r\n {\r\n columns.reduce(\r\n (acc, col) => {\r\n const { key, width, render } = col;\r\n const currentLeft = acc.leftOffset; // Where does this column start?\r\n\r\n const spanData = spanMap[`${index}:${key}`];\r\n const isVisible = spanData ? spanData.isVisible : true; // Default true if not merged\r\n const spanCount = spanData ? spanData.span : 1;\r\n\r\n // LOGIC: Only render if it's the start of a span (or not a merge column)\r\n if (isVisible) {\r\n acc.elements.push(\r\n <div\r\n key={key}\r\n style={{\r\n position: \"absolute\",\r\n left: currentLeft, // πŸ“ PINNED POSITION\r\n width: width,\r\n height: spanCount * rowHeight, // πŸ“ Grow downwards\r\n zIndex: spanCount > 1 ? 10 : 1, // Float on top if merged\r\n background: \"white\", // Important to cover rows below\r\n borderRight: \"1px solid #eee\",\r\n borderBottom: \"1px solid #eee\",\r\n display: \"flex\",\r\n alignItems: \"center\",\r\n ...col.style,\r\n }}\r\n >\r\n {/* Render user content or default text */}\r\n {render ? render(row) : row[key]}\r\n </div>\r\n );\r\n }\r\n\r\n // Move the cursor to the right for the next column\r\n // We add width even if we didn't render anything!\r\n acc.leftOffset += width;\r\n\r\n return acc;\r\n },\r\n { elements: [] as React.ReactNode[], leftOffset: 0 }\r\n ).elements\r\n }\r\n </div>\r\n );\r\n };\r\n\r\n return (\r\n <ListComponent\r\n height={height}\r\n itemCount={data.length}\r\n itemSize={() => rowHeight}\r\n width={width}\r\n >\r\n {Row}\r\n </ListComponent>\r\n );\r\n};\r\n","export type SpanMap = Record<\r\n string,\r\n { span: number; isVisible: boolean; refRow?: number }\r\n>;\r\n\r\nexport const calculateSpans = (data: any[], groupKeys: string[]): SpanMap => {\r\n const spanMap: SpanMap = {};\r\n\r\n // Initialize pointers for each column (groupKey)\r\n const lastSeenValues: Record<string, any> = {};\r\n const lastStartIndices: Record<string, number> = {};\r\n\r\n data.forEach((row, rowIndex) => {\r\n groupKeys.forEach((key) => {\r\n const cellId = `${rowIndex}:${key}`;\r\n const currentValue = row[key];\r\n\r\n // If it matches the previous row's value, extend the span\r\n if (rowIndex > 0 && currentValue === lastSeenValues[key]) {\r\n const parentRowIndex = lastStartIndices[key];\r\n const parentId = `${parentRowIndex}:${key}`;\r\n\r\n // Increment parent span\r\n if (!spanMap[parentId])\r\n spanMap[parentId] = { span: 1, isVisible: true };\r\n spanMap[parentId].span += 1;\r\n\r\n // Mark current cell as hidden\r\n spanMap[cellId] = { span: 0, isVisible: false, refRow: parentRowIndex };\r\n } else {\r\n // New Value: Start a new span\r\n lastSeenValues[key] = currentValue;\r\n lastStartIndices[key] = rowIndex;\r\n spanMap[cellId] = { span: 1, isVisible: true };\r\n }\r\n });\r\n });\r\n\r\n return spanMap;\r\n};\r\n"],"mappings":";AAAA,OAAO,SAAS,eAAe;AAE/B,YAAY,0BAA0B;;;ACG/B,IAAM,iBAAiB,CAAC,MAAa,cAAiC;AAC3E,QAAM,UAAmB,CAAC;AAG1B,QAAM,iBAAsC,CAAC;AAC7C,QAAM,mBAA2C,CAAC;AAElD,OAAK,QAAQ,CAAC,KAAK,aAAa;AAC9B,cAAU,QAAQ,CAAC,QAAQ;AACzB,YAAM,SAAS,GAAG,QAAQ,IAAI,GAAG;AACjC,YAAM,eAAe,IAAI,GAAG;AAG5B,UAAI,WAAW,KAAK,iBAAiB,eAAe,GAAG,GAAG;AACxD,cAAM,iBAAiB,iBAAiB,GAAG;AAC3C,cAAM,WAAW,GAAG,cAAc,IAAI,GAAG;AAGzC,YAAI,CAAC,QAAQ,QAAQ;AACnB,kBAAQ,QAAQ,IAAI,EAAE,MAAM,GAAG,WAAW,KAAK;AACjD,gBAAQ,QAAQ,EAAE,QAAQ;AAG1B,gBAAQ,MAAM,IAAI,EAAE,MAAM,GAAG,WAAW,OAAO,QAAQ,eAAe;AAAA,MACxE,OAAO;AAEL,uBAAe,GAAG,IAAI;AACtB,yBAAiB,GAAG,IAAI;AACxB,gBAAQ,MAAM,IAAI,EAAE,MA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AAa;AAAA,kBACb,cAAc;AAAA,kBACd,SAAS;AAAA,kBACT,YAAY;AAAA,kBACZ,GAAG,IAAI;AAAA,gBACT;AAAA;AAAA,cAGC,SAAS,OAAO,GAAG,IAAI,IAAI,GAAG;AAAA,YACjC;AAAA,UACF;AAAA,QACF;AAIA,YAAI,cAAcA;AAElB,eAAO;AAAA,MACT;AAAA,MACA,EAAE,UAAU,CAAC,GAAwB,YAAY,EAAE;AAAA,IACrD,EAAE,QAEN;AAAA,EAEJ;AAEA,SACE;AAAA,IAAC;AAAA;AAAA,MACC;AAAA,MACA,WAAW,KAAK;AAAA,MAChB,UAAU,MAAM;AAAA,MAChB;AAAA;AAAA,IAEC;AAAA,EACH;AAEJ;","names":["width"]}
package/package.json ADDED
@@ -0,0 +1,46 @@
1
+ {
2
+ "name": "react-virtual-merged-list",
3
+ "version": "1.0.0",
4
+ "description": "A React component for rendering virtualized lists with merged cells/rowspans using react-window.",
5
+ "main": "./dist/index.js",
6
+ "module": "./dist/index.mjs",
7
+ "types": "./dist/index.d.ts",
8
+ "files": [
9
+ "dist"
10
+ ],
11
+ "scripts": {
12
+ "build": "tsup",
13
+ "dev": "tsup --watch",
14
+ "lint": "tsc --noEmit",
15
+ "prepublishOnly": "npm run build"
16
+ },
17
+ "peerDependencies": {
18
+ "react": ">=16.8.0",
19
+ "react-dom": ">=16.8.0",
20
+ "react-window": ">=1.8.0"
21
+ },
22
+ "devDependencies": {
23
+ "@types/react": "^18.2.0",
24
+ "@types/react-dom": "^18.2.0",
25
+ "@types/react-window": "^1.8.5",
26
+ "react": "^18.2.0",
27
+ "react-dom": "^18.2.0",
28
+ "react-window": "^1.8.10",
29
+ "tsup": "^8.0.0",
30
+ "typescript": "^5.0.0"
31
+ },
32
+ "keywords": [
33
+ "react",
34
+ "virtualized",
35
+ "table",
36
+ "rowspan",
37
+ "merged-cells",
38
+ "react-window"
39
+ ],
40
+ "author": "Nilesh Ingale",
41
+ "license": "MIT",
42
+ "repository": {
43
+ "type": "git",
44
+ "url": "git+https://github.com/nilesh-sde/react-virtual-merged-list.git"
45
+ }
46
+ }