trotl-table 1.0.10 → 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,6 +26,7 @@ yarn add trotl-table
26
26
 
27
27
  ## Versions
28
28
  ```js
29
+ 1.0.9 => add search input
29
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)
package/dist/index.cjs.js CHANGED
@@ -9693,26 +9693,49 @@ function Table({
9693
9693
  // Option to read search param from URL
9694
9694
  const [searchTerm, setSearchTerm] = React.useState(extraSearchTerm);
9695
9695
 
9696
- // Recognize "entry" in URL search param and set as searchTerm if present
9697
- // Extract window.location.search to a variable for useEffect dependency
9698
- const locationSearch = typeof window !== "undefined" ? window.location.search : "";
9696
+ // Listen to URL changes via popstate (back/forward) and custom events
9699
9697
  React.useEffect(() => {
9700
9698
  if (!enableSearchUrlParam) {
9701
9699
  setSearchTerm(extraSearchTerm);
9702
9700
  return;
9703
9701
  }
9704
- const params = new URLSearchParams(locationSearch);
9705
- const entrySearch = params.get("entry");
9706
- const urlSearch = params.get("search");
9707
- console.log(urlSearch);
9708
- if (entrySearch) {
9709
- setSearchTerm(entrySearch);
9710
- } else if (urlSearch) {
9711
- setSearchTerm(urlSearch);
9712
- } else {
9713
- setSearchTerm(extraSearchTerm);
9714
- }
9715
- }, [locationSearch, enableSearchUrlParam, extraSearchTerm]);
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]);
9716
9739
  const toggleRowSelection = React.useCallback(rowId => {
9717
9740
  setSelectedRows(prev => prev.includes(rowId) ? prev.filter(r => r !== rowId) : [...prev, rowId]);
9718
9741
  }, []);