@trackunit/react-components 0.1.39 → 0.1.41

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/index.js CHANGED
@@ -1723,34 +1723,19 @@ const SkeletonLine = tw.div `
1723
1723
  * @param {IPaginationProps} props - The props for the Pagination component
1724
1724
  * @returns {JSX.Element} Pagination component
1725
1725
  */
1726
- const Pagination = ({ gotoPage, previousPage, nextPage, pageCount, pageIndex, canPreviousPage, canNextPage, loading, className, dataTestId, getTranslatedCount, getTranslatedPage, onPageChange, cursorBase, }) => {
1727
- const [page, setPage] = React.useState(pageIndex);
1728
- const [pageInputValue, setPageInputValue] = React.useState(String(pageIndex !== undefined ? pageIndex + 1 : 1));
1726
+ const Pagination = ({ previousPage, nextPage, canPreviousPage, canNextPage, pageCount, pageIndex, loading, className, dataTestId, getTranslatedCount, onPageChange, cursorBase, }) => {
1727
+ const [page, setPage] = useState(pageIndex);
1728
+ const [currentPage, setCurrentPage] = useState(String(pageIndex !== undefined ? pageIndex + 1 : 1));
1729
1729
  if (!loading && pageCount === undefined) {
1730
1730
  throw Error("Pagination should be provided with a pageCount");
1731
1731
  }
1732
- React.useEffect(() => {
1732
+ useEffect(() => {
1733
1733
  if (pageIndex !== undefined && (isNaN(pageIndex) || pageIndex < 0)) {
1734
- gotoPage(0);
1735
- }
1736
- else if (pageIndex !== undefined && pageCount !== null && pageCount !== undefined && pageIndex > pageCount - 1) {
1737
- gotoPage(pageCount - 1);
1738
- }
1739
- else {
1740
1734
  setPage(pageIndex);
1741
- pageIndex !== undefined && setPageInputValue(String(pageIndex + 1));
1742
- }
1743
- }, [pageIndex, gotoPage, setPage, setPageInputValue, pageCount]);
1744
- const parsePageString = React.useCallback((pageString) => {
1745
- let val = parseInt(pageString);
1746
- val = isNaN(val) ? 0 : val;
1747
- val = val < 1 ? 0 : val - 1;
1748
- if (pageCount !== undefined && pageCount !== null) {
1749
- val = val > pageCount ? pageCount - 1 : val;
1735
+ pageIndex !== undefined && setCurrentPage(String(pageIndex + 1));
1750
1736
  }
1751
- return val;
1752
- }, [pageCount]);
1753
- const handlePageChange = React.useCallback((action) => {
1737
+ }, [pageIndex, setPage, setCurrentPage, pageCount]);
1738
+ const handlePageChange = useCallback((action) => {
1754
1739
  const from = page;
1755
1740
  let to = from;
1756
1741
  let description = "";
@@ -1765,38 +1750,18 @@ const Pagination = ({ gotoPage, previousPage, nextPage, pageCount, pageIndex, ca
1765
1750
  nextPage();
1766
1751
  description = "Next Page";
1767
1752
  break;
1768
- case "manual":
1769
- to = parsePageString(pageInputValue);
1770
- gotoPage(to);
1771
- description = "Manual Page Change";
1772
- break;
1773
1753
  }
1774
1754
  if (to) {
1775
1755
  setPage(to);
1776
- setPageInputValue(String(to + 1));
1756
+ setCurrentPage(String(to + 1));
1777
1757
  }
1778
1758
  onPageChange === null || onPageChange === void 0 ? void 0 : onPageChange({ from, to, description });
1779
- }, [page, onPageChange, previousPage, nextPage, parsePageString, pageInputValue, gotoPage]);
1780
- const handleOnBlur = () => {
1781
- handlePageChange("manual");
1782
- };
1783
- const handleOnFocus = (e) => e.target.select();
1784
- const handleOnChange = React.useCallback((e) => {
1785
- setPageInputValue(e.target.value);
1786
- }, []);
1787
- const handleOnKeyPress = React.useCallback((e) => {
1788
- if (e.key !== "Enter") {
1789
- return;
1790
- }
1791
- handlePageChange("manual");
1792
- }, [handlePageChange]);
1759
+ }, [page, onPageChange, previousPage, nextPage]);
1793
1760
  if (loading) {
1794
1761
  return (jsx(Root$3, Object.assign({ className: className }, { children: jsx(SkeletonLines, { height: 16, width: 150 }) })));
1795
1762
  }
1796
- return (jsxs(Root$3, Object.assign({ "data-testid": dataTestId, className: className }, { children: [jsx(IconButton, { onClick: () => handlePageChange("prev"), disabled: cursorBase ? false : page !== undefined && page <= 0, "data-testid": "prev-page", icon: jsx(Icon, { name: "ChevronLeftIcon", size: "small" }), size: "small", color: "secondary", variant: "transparent" }), !cursorBase && (jsxs(Fragment, { children: [jsx(Text$1, { children: getTranslatedPage() }), jsx(PageInput, { type: "number", min: 1,
1797
- // max={amountOfPages}
1798
- value: pageInputValue, onFocus: handleOnFocus, onBlur: handleOnBlur, onChange: handleOnChange, onKeyPress: handleOnKeyPress, "data-testid": "page-input" }), jsx(TextStyled, Object.assign({ dataTestId: "page-count" }, { children: pageCount !== null && pageCount !== undefined && getTranslatedCount(pageCount) }))] })), jsx(IconButton, { onClick: () => handlePageChange("next"), disabled: cursorBase
1799
- ? false
1763
+ return (jsxs(Root$3, Object.assign({ "data-testid": dataTestId, className: className }, { children: [jsx(IconButton, { onClick: () => handlePageChange("prev"), disabled: cursorBase ? !canPreviousPage || false : page !== undefined && page <= 0, "data-testid": "prev-page", icon: jsx(Icon, { name: "ChevronLeftIcon", size: "small" }), size: "small", color: "secondary", variant: "transparent" }), !cursorBase && (jsxs(Fragment, { children: [jsx(TextStyled, Object.assign({ dataTestId: "current-page" }, { children: currentPage })), jsx(TextStyled, Object.assign({ dataTestId: "page-count" }, { children: pageCount !== null && pageCount !== undefined && getTranslatedCount(pageCount) }))] })), jsx(IconButton, { onClick: () => handlePageChange("next"), disabled: cursorBase
1764
+ ? !canNextPage || false
1800
1765
  : page !== undefined && pageCount !== undefined && pageCount !== null && page >= pageCount - 1, "data-testid": "next-page", icon: jsx(Icon, { name: "ChevronRightIcon", size: "small" }), size: "small", color: "secondary", variant: "transparent" })] })));
1801
1766
  };
1802
1767
  const Root$3 = tw.div `
@@ -1804,14 +1769,6 @@ tu-pagination
1804
1769
  `;
1805
1770
  const TextStyled = twx(Text$1) `
1806
1771
  tu-pagination__text
1807
- `;
1808
- const PageInput = tw.input `
1809
- tu-pagination__page-input
1810
- [&::-webkit-inner-spin-button]:[-webkit-appearance:none]
1811
- [&::-webkit-outer-spin-button]:[-webkit-appearance:none]
1812
- [&::-webkit-inner-spin-button]:m-0
1813
- [&::-webkit-outer-spin-button]:m-0
1814
- [-moz-appearance:textfield]
1815
1772
  `;
1816
1773
 
1817
1774
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@trackunit/react-components",
3
- "version": "0.1.39",
3
+ "version": "0.1.41",
4
4
  "repository": "https://github.com/Trackunit/manager",
5
5
  "license": "SEE LICENSE IN LICENSE.txt",
6
6
  "dependencies": {
@@ -1,9 +1,5 @@
1
1
  /// <reference types="react" />
2
2
  export interface IPaginationProps {
3
- /**
4
- * A go to page handler
5
- */
6
- gotoPage: (index: number) => void;
7
3
  /**
8
4
  * A go to previous page handler
9
5
  */
@@ -44,10 +40,6 @@ export interface IPaginationProps {
44
40
  * An helper to get translation for count.
45
41
  */
46
42
  getTranslatedCount: (count: number) => string;
47
- /**
48
- * An helper to get translation for page.
49
- */
50
- getTranslatedPage: () => string;
51
43
  /**
52
44
  * Callback that fires on page change with details about the action
53
45
  */
@@ -67,4 +59,4 @@ export interface IPaginationProps {
67
59
  * @param {IPaginationProps} props - The props for the Pagination component
68
60
  * @returns {JSX.Element} Pagination component
69
61
  */
70
- export declare const Pagination: ({ gotoPage, previousPage, nextPage, pageCount, pageIndex, canPreviousPage, canNextPage, loading, className, dataTestId, getTranslatedCount, getTranslatedPage, onPageChange, cursorBase, }: IPaginationProps) => JSX.Element;
62
+ export declare const Pagination: ({ previousPage, nextPage, canPreviousPage, canNextPage, pageCount, pageIndex, loading, className, dataTestId, getTranslatedCount, onPageChange, cursorBase, }: IPaginationProps) => JSX.Element;