@charlesgomes/leafcode-shared-lib-react 1.0.48 → 1.0.50

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.js CHANGED
@@ -863,6 +863,16 @@ function DataTableAdvancedFilterWrapper({
863
863
  globalFilterFields
864
864
  )
865
865
  });
866
+ const didInitRef = (0, import_react6.useRef)(false);
867
+ (0, import_react6.useEffect)(() => {
868
+ if (didInitRef.current) return;
869
+ didInitRef.current = true;
870
+ window.history.replaceState(
871
+ null,
872
+ "",
873
+ window.location.href
874
+ );
875
+ }, []);
866
876
  const updateUrlParams = (params, options = { replace: true }) => {
867
877
  if (typeof window === "undefined" || !replaceUrl) return;
868
878
  const urlParams = new URLSearchParams(window.location.search);
@@ -879,6 +889,37 @@ function DataTableAdvancedFilterWrapper({
879
889
  const newUrl = `${window.location.pathname}?${urlParams.toString()}`;
880
890
  window.history.pushState(null, "", newUrl);
881
891
  };
892
+ const hydrateFromUrl = () => {
893
+ const params = new URLSearchParams(window.location.search);
894
+ const pageParam = Number(params.get("page") ?? 1);
895
+ const rowsParam = Number(params.get("rows") ?? rows);
896
+ const sortFieldParam = params.get("sortField") ?? sortFieldInitial;
897
+ const sortOrderParam = Number(params.get("sortOrder") ?? sortOrderInitial);
898
+ const filterParam = params.get("filter") ?? "";
899
+ setPage(pageParam);
900
+ setRows(rowsParam);
901
+ setFirst((pageParam - 1) * rowsParam);
902
+ setSortField(sortFieldParam);
903
+ setSortOrder(sortOrderParam);
904
+ setSearchText(filterParam);
905
+ setFilters((prev) => ({
906
+ ...prev,
907
+ global: {
908
+ ...prev.global ?? { matchMode: "contains" },
909
+ value: filterParam
910
+ }
911
+ }));
912
+ };
913
+ (0, import_react6.useEffect)(() => {
914
+ if (typeof window === "undefined") return;
915
+ const handlePopState = () => {
916
+ hydrateFromUrl();
917
+ };
918
+ window.addEventListener("popstate", handlePopState);
919
+ return () => {
920
+ window.removeEventListener("popstate", handlePopState);
921
+ };
922
+ }, []);
882
923
  const onPage = (event) => {
883
924
  setFirst(event.first);
884
925
  setRows(event.rows);
package/dist/index.mjs CHANGED
@@ -129,7 +129,7 @@ function ModalBase({
129
129
  import { useEffect as useEffect4, useState as useState3 } from "react";
130
130
 
131
131
  // src/components/DataTableAdvancedFilter/DataTableAdvancedFilterWrapper.tsx
132
- import { useEffect as useEffect3, useMemo, useState as useState2 } from "react";
132
+ import { useEffect as useEffect3, useMemo, useRef, useState as useState2 } from "react";
133
133
  import { useQuery } from "@tanstack/react-query";
134
134
 
135
135
  // src/primereact-compat.ts
@@ -808,6 +808,16 @@ function DataTableAdvancedFilterWrapper({
808
808
  globalFilterFields
809
809
  )
810
810
  });
811
+ const didInitRef = useRef(false);
812
+ useEffect3(() => {
813
+ if (didInitRef.current) return;
814
+ didInitRef.current = true;
815
+ window.history.replaceState(
816
+ null,
817
+ "",
818
+ window.location.href
819
+ );
820
+ }, []);
811
821
  const updateUrlParams = (params, options = { replace: true }) => {
812
822
  if (typeof window === "undefined" || !replaceUrl) return;
813
823
  const urlParams = new URLSearchParams(window.location.search);
@@ -824,6 +834,37 @@ function DataTableAdvancedFilterWrapper({
824
834
  const newUrl = `${window.location.pathname}?${urlParams.toString()}`;
825
835
  window.history.pushState(null, "", newUrl);
826
836
  };
837
+ const hydrateFromUrl = () => {
838
+ const params = new URLSearchParams(window.location.search);
839
+ const pageParam = Number(params.get("page") ?? 1);
840
+ const rowsParam = Number(params.get("rows") ?? rows);
841
+ const sortFieldParam = params.get("sortField") ?? sortFieldInitial;
842
+ const sortOrderParam = Number(params.get("sortOrder") ?? sortOrderInitial);
843
+ const filterParam = params.get("filter") ?? "";
844
+ setPage(pageParam);
845
+ setRows(rowsParam);
846
+ setFirst((pageParam - 1) * rowsParam);
847
+ setSortField(sortFieldParam);
848
+ setSortOrder(sortOrderParam);
849
+ setSearchText(filterParam);
850
+ setFilters((prev) => ({
851
+ ...prev,
852
+ global: {
853
+ ...prev.global ?? { matchMode: "contains" },
854
+ value: filterParam
855
+ }
856
+ }));
857
+ };
858
+ useEffect3(() => {
859
+ if (typeof window === "undefined") return;
860
+ const handlePopState = () => {
861
+ hydrateFromUrl();
862
+ };
863
+ window.addEventListener("popstate", handlePopState);
864
+ return () => {
865
+ window.removeEventListener("popstate", handlePopState);
866
+ };
867
+ }, []);
827
868
  const onPage = (event) => {
828
869
  setFirst(event.first);
829
870
  setRows(event.rows);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@charlesgomes/leafcode-shared-lib-react",
3
- "version": "1.0.48",
3
+ "version": "1.0.50",
4
4
  "description": "Lib de componentes react",
5
5
  "main": "dist/index.cjs",
6
6
  "module": "dist/index.mjs",