trotl-table 1.0.9 → 1.0.11

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
@@ -26,7 +26,8 @@ yarn add trotl-table
26
26
 
27
27
  ## Versions
28
28
  ```js
29
- 1.0.9 => fix readme
29
+ 1.0.9 => add search input
30
+ 1.0.9 => fix readme, whatever
30
31
  1.0.8 => fix readme
31
32
  1.0.7 => removed context (better for npm consumers)
32
33
  1.0.6 => ...
package/dist/index.cjs.js CHANGED
@@ -9683,11 +9683,59 @@ function Table({
9683
9683
  viewCallback = () => {},
9684
9684
  deleteCallback = null,
9685
9685
  buttons = ["view", "edit", "delete"],
9686
- enableDragRow = false // new prop
9686
+ enableDragRow = false,
9687
+ enableSearchUrlParam = false
9687
9688
  }) {
9688
9689
  // Local selection & sorting state (removed TableContext dependency)
9689
9690
  const [selectedRows, setSelectedRows] = React.useState([]);
9690
9691
  const [sorting, setSorting] = React.useState(null);
9692
+
9693
+ // Option to read search param from URL
9694
+ const [searchTerm, setSearchTerm] = React.useState(extraSearchTerm);
9695
+
9696
+ // Listen to URL changes via popstate (back/forward) and custom events
9697
+ React.useEffect(() => {
9698
+ if (!enableSearchUrlParam) {
9699
+ setSearchTerm(extraSearchTerm);
9700
+ return;
9701
+ }
9702
+ const updateSearchFromUrl = () => {
9703
+ const params = new URLSearchParams(window.location.search);
9704
+ const entrySearch = params.get("entry");
9705
+ const urlSearch = params.get("search");
9706
+ if (entrySearch) {
9707
+ setSearchTerm(entrySearch);
9708
+ } else if (urlSearch) {
9709
+ setSearchTerm(urlSearch);
9710
+ } else {
9711
+ setSearchTerm(extraSearchTerm);
9712
+ }
9713
+ };
9714
+
9715
+ // Initial sync
9716
+ updateSearchFromUrl();
9717
+
9718
+ // Listen for URL changes (back/forward navigation)
9719
+ window.addEventListener('popstate', updateSearchFromUrl);
9720
+
9721
+ // Listen for programmatic URL changes if you use history.pushState/replaceState
9722
+ const originalPushState = window.history.pushState;
9723
+ const originalReplaceState = window.history.replaceState;
9724
+ window.history.pushState = function (...args) {
9725
+ originalPushState.apply(this, args);
9726
+ updateSearchFromUrl();
9727
+ };
9728
+ window.history.replaceState = function (...args) {
9729
+ originalReplaceState.apply(this, args);
9730
+ updateSearchFromUrl();
9731
+ };
9732
+ return () => {
9733
+ window.removeEventListener('popstate', updateSearchFromUrl);
9734
+ // Restore original methods
9735
+ window.history.pushState = originalPushState;
9736
+ window.history.replaceState = originalReplaceState;
9737
+ };
9738
+ }, [enableSearchUrlParam, extraSearchTerm]);
9691
9739
  const toggleRowSelection = React.useCallback(rowId => {
9692
9740
  setSelectedRows(prev => prev.includes(rowId) ? prev.filter(r => r !== rowId) : [...prev, rowId]);
9693
9741
  }, []);
@@ -9705,7 +9753,9 @@ function Table({
9705
9753
  return null;
9706
9754
  });
9707
9755
  };
9708
- console.log(extraSearchTerm);
9756
+
9757
+ // console.log(extraSearchTerm)
9758
+
9709
9759
  const [localData, setLocalData] = React.useState(data);
9710
9760
  React.useEffect(() => setLocalData(data), [data]);
9711
9761
  const listRef = React.useRef(null);
@@ -9713,14 +9763,14 @@ function Table({
9713
9763
  return normalizeData(isGrouped ? localData : [], isGrouped ? [] : localData);
9714
9764
  }, [localData, isGrouped]);
9715
9765
  const filterRows = React.useCallback(rows => {
9716
- if (!extraSearchTerm) return rows;
9717
- const query = extraSearchTerm.toLowerCase();
9766
+ if (!searchTerm) return rows;
9767
+ const query = searchTerm.toLowerCase();
9718
9768
  return rows.filter(row => columns.some(col => {
9719
9769
  const val = row[col.accessor];
9720
9770
  if (val == null) return false;
9721
9771
  return String(val).toLowerCase().includes(query);
9722
9772
  }));
9723
- }, [extraSearchTerm, columns]);
9773
+ }, [searchTerm, columns]);
9724
9774
 
9725
9775
  // Natural-ish comparator: handles nulls, numbers, strings, ISO dates
9726
9776
  const compare = (x, y, dir = "asc") => {
@@ -9857,15 +9907,16 @@ function Table({
9857
9907
 
9858
9908
  // RENDER CELL
9859
9909
  const highlight = React.useCallback(text => {
9860
- if (!extraSearchTerm || typeof text !== "string") return text;
9910
+ // Use the actual searchTerm (from URL or prop) for highlighting
9911
+ if (!searchTerm || typeof text !== "string") return text;
9861
9912
  const lower = text.toLowerCase();
9862
- const query = extraSearchTerm.toLowerCase();
9913
+ const query = searchTerm.toLowerCase();
9863
9914
  const idx = lower.indexOf(query);
9864
9915
  if (idx === -1) return text;
9865
9916
  return /*#__PURE__*/React.createElement(React.Fragment, null, text.slice(0, idx), /*#__PURE__*/React.createElement("span", {
9866
9917
  className: "highlight"
9867
9918
  }, text.slice(idx, idx + query.length)), text.slice(idx + query.length));
9868
- }, [extraSearchTerm]);
9919
+ }, [searchTerm]);
9869
9920
  const renderCell = React.useCallback((v, accessor) => {
9870
9921
  if (accessor === "deadlineISO") {
9871
9922
  return v ? new Date(v).toLocaleString("sl-SI", {