react-auto-smart-table 1.0.0 → 1.0.3

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/index.mjs CHANGED
@@ -490,7 +490,7 @@ var generateInsights = (dataset, schema) => {
490
490
  };
491
491
 
492
492
  // src/components/SmartTable.tsx
493
- import React3, { useMemo as useMemo5 } from "react";
493
+ import React3, { useMemo as useMemo6 } from "react";
494
494
 
495
495
  // src/components/TableHeader.tsx
496
496
  import { jsx, jsxs } from "react/jsx-runtime";
@@ -531,7 +531,7 @@ var TableHeader = ({
531
531
  };
532
532
 
533
533
  // src/components/TableBody.tsx
534
- import React, { useRef } from "react";
534
+ import { useMemo as useMemo4, useCallback } from "react";
535
535
 
536
536
  // src/renderers/numberRenderer.tsx
537
537
  import { jsx as jsx2 } from "react/jsx-runtime";
@@ -665,7 +665,7 @@ var DefaultRenderer = ({ value }) => {
665
665
  // src/plugins/currencyPlugin.tsx
666
666
  import { jsx as jsx10 } from "react/jsx-runtime";
667
667
  var currencyPlugin = {
668
- detect: (columnKey, sample) => {
668
+ detect: (columnKey, _sample) => {
669
669
  const lowerKey = columnKey.toLowerCase();
670
670
  if (lowerKey.includes("amount") || lowerKey.includes("price") || lowerKey.includes("revenue")) {
671
671
  return "currency";
@@ -689,7 +689,7 @@ var currencyPlugin = {
689
689
  // src/plugins/percentagePlugin.tsx
690
690
  import { jsx as jsx11, jsxs as jsxs3 } from "react/jsx-runtime";
691
691
  var percentagePlugin = {
692
- detect: (columnKey, sample) => {
692
+ detect: (columnKey, _sample) => {
693
693
  const lowerKey = columnKey.toLowerCase();
694
694
  if (lowerKey.includes("percent") || lowerKey.includes("rate")) {
695
695
  return "percentage";
@@ -759,76 +759,91 @@ var getCellRenderer = (type, registry) => {
759
759
  };
760
760
 
761
761
  // src/components/TableBody.tsx
762
- import { useVirtual } from "react-virtual";
762
+ import { useVirtualizer } from "@tanstack/react-virtual";
763
763
  import { jsx as jsx12 } from "react/jsx-runtime";
764
- var TableBody = ({ data, schema, registry }) => {
765
- const columns = Object.keys(schema);
766
- const parentRef = useRef(null);
767
- const rowVirtualizer = useVirtual({
768
- size: data.length,
769
- parentRef,
770
- estimateSize: React.useCallback(() => 45, []),
771
- overscan: 5
772
- });
764
+ var TableRow = ({ row, index, columns, schema, registry, style }) => {
765
+ return /* @__PURE__ */ jsx12("tr", { className: "rst-row", style, children: columns.map((colKey) => {
766
+ const colInfo = schema[colKey];
767
+ const Renderer = getCellRenderer(colInfo.type, registry);
768
+ const widthClass = getColumnWidthClass(colKey, colInfo.type);
769
+ const val = row[colKey];
770
+ return /* @__PURE__ */ jsx12(
771
+ "td",
772
+ {
773
+ className: `rst-td rst-td-col-${colKey} rst-td-type-${colInfo.type} ${widthClass}`,
774
+ children: colInfo.type === "boolean" ? /* @__PURE__ */ jsx12(
775
+ "span",
776
+ {
777
+ className: `rst-badge ${val ? "rst-badge-yes" : "rst-badge-no"}`,
778
+ children: val ? "Yes" : "No"
779
+ }
780
+ ) : /* @__PURE__ */ jsx12(Renderer, { value: val })
781
+ },
782
+ colKey
783
+ );
784
+ }) });
785
+ };
786
+ var TableBody = ({
787
+ data,
788
+ schema,
789
+ registry,
790
+ scrollRef
791
+ }) => {
792
+ const columns = useMemo4(() => Object.keys(schema), [schema]);
773
793
  const isVirtualized = data.length > 20;
774
- if (!isVirtualized) {
775
- return /* @__PURE__ */ jsx12("tbody", { className: "rst-body", children: data.map((row, index) => /* @__PURE__ */ jsx12("tr", { className: "rst-row", children: columns.map((colKey) => {
776
- const colInfo = schema[colKey];
777
- const Renderer = getCellRenderer(colInfo.type, registry);
778
- const widthClass = getColumnWidthClass(colKey, colInfo.type);
779
- const val = row[colKey];
780
- return /* @__PURE__ */ jsx12(
781
- "td",
782
- {
783
- className: `rst-td rst-td-col-${colKey} rst-td-type-${colInfo.type} ${widthClass}`,
784
- children: colInfo.type === "boolean" ? /* @__PURE__ */ jsx12("span", { className: `rst-badge ${val ? "rst-badge-yes" : "rst-badge-no"}`, children: val ? "Yes" : "No" }) : /* @__PURE__ */ jsx12(Renderer, { value: val })
785
- },
786
- colKey
787
- );
788
- }) }, index)) });
794
+ const rowVirtualizer = useVirtualizer({
795
+ count: data.length,
796
+ getScrollElement: () => scrollRef?.current || null,
797
+ estimateSize: useCallback(() => 45, []),
798
+ overscan: 5,
799
+ enabled: isVirtualized
800
+ });
801
+ if (!isVirtualized || !scrollRef) {
802
+ return /* @__PURE__ */ jsx12("tbody", { className: "rst-body", children: data.map((row, index) => /* @__PURE__ */ jsx12(
803
+ TableRow,
804
+ {
805
+ row,
806
+ index,
807
+ columns,
808
+ schema,
809
+ registry
810
+ },
811
+ index
812
+ )) });
789
813
  }
814
+ const virtualRows = rowVirtualizer.getVirtualItems();
815
+ const totalSize = rowVirtualizer.getTotalSize();
790
816
  return /* @__PURE__ */ jsx12(
791
817
  "tbody",
792
818
  {
793
- className: "rst-body",
794
- ref: parentRef,
819
+ className: "rst-body rst-body-virtualized",
795
820
  style: {
796
- height: `${rowVirtualizer.totalSize}px`,
821
+ height: `${totalSize}px`,
797
822
  width: "100%",
798
823
  position: "relative"
799
824
  },
800
- children: rowVirtualizer.virtualItems.map((virtualRow) => {
801
- const row = data[virtualRow.index];
802
- return /* @__PURE__ */ jsx12(
803
- "tr",
804
- {
805
- className: "rst-row",
806
- style: {
807
- position: "absolute",
808
- top: 0,
809
- left: 0,
810
- width: "100%",
811
- height: `${virtualRow.size}px`,
812
- transform: `translateY(${virtualRow.start}px)`
813
- },
814
- children: columns.map((colKey) => {
815
- const val = row[colKey];
816
- const colInfo = schema[colKey];
817
- const Renderer = getCellRenderer(colInfo.type, registry);
818
- const widthClass = getColumnWidthClass(colKey, colInfo.type);
819
- return /* @__PURE__ */ jsx12(
820
- "td",
821
- {
822
- className: `rst-td rst-td-col-${colKey} rst-td-type-${colInfo.type} ${widthClass}`,
823
- children: colInfo.type === "boolean" ? /* @__PURE__ */ jsx12("span", { className: `rst-badge ${val ? "rst-badge-yes" : "rst-badge-no"}`, children: val ? "Yes" : "No" }) : /* @__PURE__ */ jsx12(Renderer, { value: val })
824
- },
825
- colKey
826
- );
827
- })
828
- },
829
- virtualRow.index
830
- );
831
- })
825
+ children: virtualRows.length > 0 ? virtualRows.map((virtualRow) => /* @__PURE__ */ jsx12(
826
+ TableRow,
827
+ {
828
+ row: data[virtualRow.index],
829
+ index: virtualRow.index,
830
+ columns,
831
+ schema,
832
+ registry,
833
+ style: {
834
+ position: "absolute",
835
+ top: 0,
836
+ left: 0,
837
+ width: "100%",
838
+ height: `${virtualRow.size}px`,
839
+ transform: `translateY(${virtualRow.start}px)`
840
+ }
841
+ },
842
+ virtualRow.key
843
+ )) : (
844
+ // Fallback for edge cases where virtualizer might be settling
845
+ /* @__PURE__ */ jsx12("tr", { style: { height: `${totalSize}px` }, children: /* @__PURE__ */ jsx12("td", { colSpan: columns.length }) })
846
+ )
832
847
  }
833
848
  );
834
849
  };
@@ -961,7 +976,7 @@ var FilterPanel = ({ schema, filters, onFilterChange, onClear }) => {
961
976
  };
962
977
 
963
978
  // src/components/InsightsPanel.tsx
964
- import { useMemo as useMemo4 } from "react";
979
+ import { useMemo as useMemo5 } from "react";
965
980
  import {
966
981
  BarChart,
967
982
  Bar,
@@ -980,7 +995,7 @@ import {
980
995
  import { jsx as jsx14, jsxs as jsxs5 } from "react/jsx-runtime";
981
996
  var COLORS = ["#8884d8", "#82ca9d", "#ffc658", "#ff8042", "#a4de6c", "#d0ed57"];
982
997
  var InsightsPanel = ({ data, schema }) => {
983
- const insights = useMemo4(() => {
998
+ const insights = useMemo5(() => {
984
999
  return generateInsights(data, schema);
985
1000
  }, [data, schema]);
986
1001
  if (!insights || insights.length === 0) {
@@ -1026,7 +1041,7 @@ var InsightsPanel = ({ data, schema }) => {
1026
1041
  dataKey: widget.yAxisKeys[0],
1027
1042
  nameKey: widget.xAxisKey,
1028
1043
  label: false,
1029
- children: widget.data.map((entry, index) => /* @__PURE__ */ jsx14(Cell, { fill: COLORS[index % COLORS.length] }, `cell-${index}`))
1044
+ children: widget.data.map((_entry, index) => /* @__PURE__ */ jsx14(Cell, { fill: COLORS[index % COLORS.length] }, `cell-${index}`))
1030
1045
  }
1031
1046
  )
1032
1047
  ] }) }) })
@@ -1062,8 +1077,6 @@ var PluginRegistry = class {
1062
1077
  }
1063
1078
  getRenderers() {
1064
1079
  const renderers = {};
1065
- for (const plugin of this.plugins) {
1066
- }
1067
1080
  return renderers;
1068
1081
  }
1069
1082
  getPlugins() {
@@ -1082,14 +1095,14 @@ var SmartTable = ({
1082
1095
  insights = false,
1083
1096
  plugins = []
1084
1097
  }) => {
1085
- const registry = useMemo5(() => {
1098
+ const registry = useMemo6(() => {
1086
1099
  const reg = new PluginRegistry();
1087
1100
  if (plugins && plugins.length > 0) {
1088
1101
  reg.register(plugins);
1089
1102
  }
1090
1103
  return reg;
1091
1104
  }, [plugins]);
1092
- const schema = useMemo5(() => {
1105
+ const schema = useMemo6(() => {
1093
1106
  return buildSchema(data, registry);
1094
1107
  }, [data, registry]);
1095
1108
  const { filteredData, filters, setFilter, clearFilters } = useFilters(data, schema);
@@ -1099,6 +1112,7 @@ var SmartTable = ({
1099
1112
  const { paginatedData, currentPage, totalPages, setPage } = usePagination(dataToRenderOrPaginate, 10);
1100
1113
  const finalData = pagination ? paginatedData : dataToRenderOrPaginate;
1101
1114
  const [showFilters, setShowFilters] = React3.useState(true);
1115
+ const scrollRef = React3.useRef(null);
1102
1116
  if (!data || data.length === 0) {
1103
1117
  return /* @__PURE__ */ jsx15("div", { className: "rst-container", children: /* @__PURE__ */ jsx15("div", { className: "rst-empty", children: "No data to display." }) });
1104
1118
  }
@@ -1154,7 +1168,7 @@ var SmartTable = ({
1154
1168
  /* @__PURE__ */ jsx15("button", { className: "btn btn-primary", onClick: clearFilters, children: "Clear All Filters" })
1155
1169
  ] }),
1156
1170
  data.length > 0 && finalData.length > 0 && /* @__PURE__ */ jsxs6("section", { className: "rst-card", children: [
1157
- /* @__PURE__ */ jsx15("div", { className: "rst-table-container", children: /* @__PURE__ */ jsxs6("table", { className: "rst-table", children: [
1171
+ /* @__PURE__ */ jsx15("div", { className: "rst-table-container", ref: scrollRef, children: /* @__PURE__ */ jsxs6("table", { className: "rst-table", children: [
1158
1172
  /* @__PURE__ */ jsx15(
1159
1173
  TableHeader,
1160
1174
  {
@@ -1165,7 +1179,15 @@ var SmartTable = ({
1165
1179
  onSort: handleSort
1166
1180
  }
1167
1181
  ),
1168
- /* @__PURE__ */ jsx15(TableBody, { data: finalData, schema, registry })
1182
+ /* @__PURE__ */ jsx15(
1183
+ TableBody,
1184
+ {
1185
+ data: finalData,
1186
+ schema,
1187
+ registry,
1188
+ scrollRef
1189
+ }
1190
+ )
1169
1191
  ] }) }),
1170
1192
  pagination && /* @__PURE__ */ jsxs6("div", { className: "rst-pagination", children: [
1171
1193
  /* @__PURE__ */ jsx15(