@yeverlibs/ds 1.0.4 → 1.0.6

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
@@ -1,7 +1,7 @@
1
1
  import { clsx } from 'clsx';
2
2
  import { twMerge } from 'tailwind-merge';
3
3
  import * as React107 from 'react';
4
- import React107__default, { forwardRef, createContext, useState, useEffect, memo, useCallback, useContext, useRef, Suspense } from 'react';
4
+ import React107__default, { forwardRef, createContext, useState, useEffect, memo, useCallback, useContext, useRef } from 'react';
5
5
  import { usePathname, useRouter, useSearchParams } from 'next/navigation';
6
6
  import { jsx, jsxs, Fragment } from 'react/jsx-runtime';
7
7
  import tw5 from 'tailwind-styled-components';
@@ -6135,6 +6135,26 @@ var Skeleton = ({ className }) => {
6135
6135
  return /* @__PURE__ */ jsx("div", { className: cn("animate-pulse rounded-md bg-gray-200", className) });
6136
6136
  };
6137
6137
  var RESET_PAGE = 1;
6138
+ var defaultUpdateUrlParam = (key, value) => {
6139
+ if (typeof window === "undefined") return;
6140
+ const params = new URLSearchParams(window.location.search);
6141
+ if (value) {
6142
+ params.set(key, value);
6143
+ } else {
6144
+ params.delete(key);
6145
+ }
6146
+ const queryString = params.toString();
6147
+ const newUrl = queryString ? `${window.location.pathname}?${queryString}` : window.location.pathname;
6148
+ window.history.replaceState(null, "", newUrl);
6149
+ };
6150
+ var defaultGetUrlParam = (key, defaultValue = "") => {
6151
+ if (typeof window === "undefined") return defaultValue;
6152
+ const params = new URLSearchParams(window.location.search);
6153
+ const value = params.get(key);
6154
+ if (value === null) return defaultValue;
6155
+ if (typeof defaultValue === "number") return Number(value) || defaultValue;
6156
+ return value;
6157
+ };
6138
6158
  var TableContent = ({
6139
6159
  columns,
6140
6160
  data,
@@ -6150,9 +6170,11 @@ var TableContent = ({
6150
6170
  onPaginateAction,
6151
6171
  noPagination,
6152
6172
  wrapperClassName,
6153
- noBackground = false
6173
+ noBackground = false,
6174
+ getUrlParam,
6175
+ updateUrlParam,
6176
+ getPathname
6154
6177
  }) => {
6155
- const pathname = usePathname();
6156
6178
  const actionMenuRef = useRef(null);
6157
6179
  const [visibleActionRow, setVisibleActionRow] = useState(null);
6158
6180
  const [isLoading, setIsLoading] = useState(false);
@@ -6160,20 +6182,29 @@ var TableContent = ({
6160
6182
  const [searchQuery, setSearchQuery] = useState("");
6161
6183
  const [itemsPerPage, setItemsPerPage] = useState(10);
6162
6184
  const [selectedRows, setSelectedRows] = useState(/* @__PURE__ */ new Set());
6163
- const searchParams = useSearchParams();
6164
- const perPage = Number(searchParams?.get("perPage")) || 10;
6165
- const page = Number(searchParams?.get("page")) || 1;
6166
- const searchParam = searchParams?.get("search") || "";
6185
+ const getUrlParamValue = getUrlParam || defaultGetUrlParam;
6186
+ const updateUrlParamValue = updateUrlParam || defaultUpdateUrlParam;
6187
+ getUrlParamValue("perPage", 10);
6188
+ const page = getUrlParamValue("page", 1);
6189
+ getUrlParamValue("search", "");
6190
+ const handlePagination = useCallback(async () => {
6191
+ if (!onPaginateAction) return;
6192
+ setIsLoading(true);
6193
+ await onPaginateAction();
6194
+ setIsLoading(false);
6195
+ }, [onPaginateAction]);
6167
6196
  useEffect(() => {
6168
6197
  setCurrentPage(page);
6169
6198
  }, [page]);
6170
6199
  useEffect(() => {
6171
- const searchParams2 = new URLSearchParams(window.location.search);
6172
- if (searchParams2.toString().length > 0) {
6200
+ if (typeof window === "undefined") return;
6201
+ const searchParams = new URLSearchParams(window.location.search);
6202
+ if (searchParams.toString().length > 0) {
6173
6203
  onPaginateAction?.();
6174
6204
  }
6175
- }, []);
6205
+ }, [onPaginateAction]);
6176
6206
  useEffect(() => {
6207
+ if (typeof window === "undefined") return;
6177
6208
  const shouldReset = document.cookie.includes("reset-table-params=true");
6178
6209
  if (shouldReset) {
6179
6210
  document.cookie = "reset-table-params=; max-age=0; path=/";
@@ -6189,38 +6220,39 @@ var TableContent = ({
6189
6220
  }
6190
6221
  window.history.replaceState({}, "", url);
6191
6222
  }
6192
- }, [pathname]);
6223
+ }, []);
6193
6224
  useEffect(() => {
6194
- if (!pathname) return;
6195
- if (searchParams?.has("perPage")) {
6196
- setItemsPerPage(perPage);
6197
- nextReplaceUrl("perPage", String(perPage), pathname);
6198
- handlePagination();
6199
- }
6200
- if (searchParams?.has("search")) {
6201
- setSearchQuery(searchParam);
6202
- nextReplaceUrl("search", searchParam, pathname);
6203
- handlePagination();
6204
- }
6205
- }, [perPage, searchParam]);
6206
- const handlePagination = async () => {
6207
- if (!onPaginateAction) return;
6208
- setIsLoading(true);
6209
- await onPaginateAction();
6210
- setIsLoading(false);
6211
- };
6225
+ if (!getUrlParam && typeof window === "undefined") return;
6226
+ const urlPerPage = getUrlParamValue("perPage", 10);
6227
+ const urlSearch = getUrlParamValue("search", "");
6228
+ if (urlPerPage !== itemsPerPage) {
6229
+ setItemsPerPage(urlPerPage);
6230
+ if (onPaginateAction) {
6231
+ handlePagination();
6232
+ }
6233
+ }
6234
+ if (urlSearch !== searchQuery) {
6235
+ setSearchQuery(urlSearch);
6236
+ if (onPaginateAction) {
6237
+ handlePagination();
6238
+ }
6239
+ }
6240
+ }, [itemsPerPage, searchQuery, handlePagination, getUrlParam, getUrlParamValue, onPaginateAction]);
6212
6241
  const totalPages = pagination ? Math.ceil(pagination.total / itemsPerPage) : 1;
6213
- const paginatedData = data;
6242
+ const paginatedData = data || [];
6214
6243
  const visibleColumns = columns.filter((column) => !hiddenColumns.includes(column.accessor));
6215
6244
  const headersWithContent = visibleColumns.filter((column) => column.Header);
6245
+ if (visibleColumns.length === 0 && columns.length > 0) {
6246
+ console.warn("Table: Todas as colunas est\xE3o ocultas. Verifique a prop hiddenColumns.");
6247
+ }
6216
6248
  const handleSearch = (e) => {
6217
6249
  setSearchQuery(e.target.value);
6218
6250
  };
6219
6251
  const handleSearchSubmit = () => {
6220
- if (!pathname) return;
6221
- Promise.all([nextReplaceUrl("search", searchQuery, pathname), nextReplaceUrl("page", "1", pathname)]).then(() => {
6222
- handlePagination();
6223
- });
6252
+ updateUrlParamValue("search", searchQuery);
6253
+ updateUrlParamValue("page", "1");
6254
+ setCurrentPage(1);
6255
+ handlePagination();
6224
6256
  };
6225
6257
  const handleClickOutside = useCallback((event) => {
6226
6258
  if (actionMenuRef.current && !actionMenuRef.current.contains(event.target)) {
@@ -6275,7 +6307,7 @@ var TableContent = ({
6275
6307
  return cellValue;
6276
6308
  }
6277
6309
  };
6278
- return isLink && linkHref ? /* @__PURE__ */ jsx(Link2, { href: linkHref, children: content() }) : content();
6310
+ return isLink && linkHref ? /* @__PURE__ */ jsx("a", { href: linkHref, className: "text-blue-600 hover:text-blue-800 underline", children: content() }) : content();
6279
6311
  };
6280
6312
  if (isLoading) {
6281
6313
  return /* @__PURE__ */ jsxs(
@@ -6379,7 +6411,7 @@ var TableContent = ({
6379
6411
  onChange: handleSelectAllRows
6380
6412
  }
6381
6413
  ) }),
6382
- headersWithContent.length === 1 ? /* @__PURE__ */ jsx(
6414
+ headersWithContent.length === 1 && visibleColumns.length > 0 ? /* @__PURE__ */ jsx(
6383
6415
  "th",
6384
6416
  {
6385
6417
  colSpan: visibleColumns.length,
@@ -6393,10 +6425,10 @@ var TableContent = ({
6393
6425
  style: { minWidth: columnWidths[index] },
6394
6426
  children: column.Header ?? ""
6395
6427
  },
6396
- column.id || index
6428
+ column.id || `column-${index}`
6397
6429
  ))
6398
6430
  ] }) }),
6399
- /* @__PURE__ */ jsx("tbody", { children: paginatedData?.length > 0 ? paginatedData?.map((row, rowIndex) => /* @__PURE__ */ jsxs(
6431
+ /* @__PURE__ */ jsx("tbody", { children: paginatedData && paginatedData.length > 0 ? paginatedData.map((row, rowIndex) => /* @__PURE__ */ jsxs(
6400
6432
  "tr",
6401
6433
  {
6402
6434
  className: cn(
@@ -6455,15 +6487,18 @@ var TableContent = ({
6455
6487
  ]
6456
6488
  },
6457
6489
  rowIndex
6458
- )) : /* @__PURE__ */ jsx("tr", { children: /* @__PURE__ */ jsx("td", { colSpan: columns.length + (selectable ? 1 : 0), style: { textAlign: "center" }, children: /* @__PURE__ */ jsxs("div", { className: "py-6", children: [
6490
+ )) : /* @__PURE__ */ jsx("tr", { children: /* @__PURE__ */ jsx("td", { colSpan: visibleColumns.length + (selectable ? 1 : 0), style: { textAlign: "center" }, children: /* @__PURE__ */ jsxs("div", { className: "py-6", children: [
6459
6491
  /* @__PURE__ */ jsx(
6460
- Image4,
6492
+ "img",
6461
6493
  {
6462
6494
  src: "/empty-table.svg",
6463
6495
  width: 666,
6464
6496
  height: 329,
6465
6497
  alt: "Nenhum resultado encontrado",
6466
- className: "mx-auto"
6498
+ className: "mx-auto",
6499
+ onError: (e) => {
6500
+ e.currentTarget.style.display = "none";
6501
+ }
6467
6502
  }
6468
6503
  ),
6469
6504
  /* @__PURE__ */ jsxs("div", { className: "mt-8", children: [
@@ -6494,16 +6529,12 @@ var TableContent = ({
6494
6529
  {
6495
6530
  value: itemsPerPage,
6496
6531
  onChange: async (e) => {
6497
- if (!pathname) return;
6498
6532
  const newPerPage = Number(e.target.value);
6499
6533
  setItemsPerPage(newPerPage);
6500
6534
  setCurrentPage(RESET_PAGE);
6501
- Promise.all([
6502
- nextReplaceUrl("page", "1", pathname),
6503
- nextReplaceUrl("perPage", String(newPerPage), pathname)
6504
- ]).then(() => {
6505
- handlePagination();
6506
- });
6535
+ updateUrlParamValue("page", "1");
6536
+ updateUrlParamValue("perPage", String(newPerPage));
6537
+ handlePagination();
6507
6538
  },
6508
6539
  className: "block h-8 w-full cursor-pointer appearance-none rounded border border-gray-300 bg-white p-2 text-sm leading-tight text-gray-700 focus:border-gray-500 focus:bg-white focus:outline-none",
6509
6540
  children: [
@@ -6533,7 +6564,23 @@ var TableContent = ({
6533
6564
  ] }) });
6534
6565
  };
6535
6566
  var Table = (props) => {
6536
- return /* @__PURE__ */ jsx(Suspense, { fallback: null, children: /* @__PURE__ */ jsx(TableContent, { ...props }) });
6567
+ if (!props.columns || props.columns.length === 0) {
6568
+ return /* @__PURE__ */ jsx(
6569
+ Box,
6570
+ {
6571
+ className: cn(
6572
+ "flex flex-col overflow-hidden",
6573
+ props.wrapperClassName,
6574
+ props.noBackground ? "bg-transparent border-none p-0" : ""
6575
+ ),
6576
+ children: /* @__PURE__ */ jsx("div", { className: "p-6", children: /* @__PURE__ */ jsx(Text, { $as: "p", children: "Nenhuma coluna definida para a tabela." }) })
6577
+ }
6578
+ );
6579
+ }
6580
+ if (!props.data) {
6581
+ console.warn('Table: A prop "data" n\xE3o foi fornecida ou \xE9 undefined.');
6582
+ }
6583
+ return /* @__PURE__ */ jsx(TableContent, { ...props });
6537
6584
  };
6538
6585
  var Tabs = ({ headers, children, noGap, active, alignDiv = "full" }) => {
6539
6586
  const [activeTab, setActiveTab] = useState(active || 0);