@trackunit/react-components 0.1.40 → 0.1.42

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));
1735
+ pageIndex !== undefined && setCurrentPage(String(pageIndex + 1));
1742
1736
  }
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;
1750
- }
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,37 +1750,17 @@ 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 ? !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(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
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
1799
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
  };
@@ -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
  /**
@@ -2971,35 +2928,36 @@ function tryParse(value) {
2971
2928
  }
2972
2929
 
2973
2930
  /**
2974
- * The useTimeout hook.
2975
- *
2976
2931
  * @param {UseTimeoutProps} props The props
2977
- * @returns {{ startTimeout: () => void; stopTimeout: () => void; }} return
2932
+ * @returns {{ startTimeout: () => void; stopTimeout: () => void; isReady: boolean | null}} return
2978
2933
  */
2979
2934
  const useTimeout = ({ onTimeout, duration }) => {
2980
- const [activated, setActivated] = useState(true);
2981
- const timeoutRef = useRef();
2935
+ const ready = useRef(false);
2936
+ const timeout = useRef();
2937
+ const callback = useRef(onTimeout);
2938
+ const isReady = useCallback(() => ready.current, []);
2982
2939
  const startTimeout = useCallback(() => {
2983
- setActivated(true);
2984
- }, []);
2940
+ ready.current = false;
2941
+ timeout.current && clearTimeout(timeout.current);
2942
+ timeout.current = setTimeout(() => {
2943
+ ready.current = true;
2944
+ callback.current();
2945
+ }, duration);
2946
+ }, [duration]);
2985
2947
  const stopTimeout = useCallback(() => {
2986
- window.clearTimeout(timeoutRef.current);
2987
- setActivated(false);
2948
+ ready.current = null;
2949
+ timeout.current && clearTimeout(timeout.current);
2988
2950
  }, []);
2951
+ // update ref when function changes
2989
2952
  useEffect(() => {
2990
- if (activated) {
2991
- timeoutRef.current = window.setTimeout(() => {
2992
- onTimeout();
2993
- }, duration);
2994
- }
2995
- return () => {
2996
- stopTimeout();
2997
- };
2998
- }, [onTimeout, activated, duration, stopTimeout]);
2999
- return {
3000
- startTimeout,
3001
- stopTimeout,
3002
- };
2953
+ callback.current = onTimeout;
2954
+ }, [onTimeout]);
2955
+ // startTimeout on mount, stopTimeout on unmount
2956
+ useEffect(() => {
2957
+ startTimeout();
2958
+ return stopTimeout;
2959
+ }, [duration, startTimeout, stopTimeout]);
2960
+ return [isReady, stopTimeout, startTimeout];
3003
2961
  };
3004
2962
 
3005
2963
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@trackunit/react-components",
3
- "version": "0.1.40",
3
+ "version": "0.1.42",
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;
@@ -1,15 +1,11 @@
1
+ export type UseTimeoutReturn = [() => boolean | null, () => void, () => void];
1
2
  interface UseTimeoutProps {
2
3
  onTimeout: () => void;
3
4
  duration: number;
4
5
  }
5
6
  /**
6
- * The useTimeout hook.
7
- *
8
7
  * @param {UseTimeoutProps} props The props
9
- * @returns {{ startTimeout: () => void; stopTimeout: () => void; }} return
8
+ * @returns {{ startTimeout: () => void; stopTimeout: () => void; isReady: boolean | null}} return
10
9
  */
11
- export declare const useTimeout: ({ onTimeout, duration }: UseTimeoutProps) => {
12
- startTimeout: () => void;
13
- stopTimeout: () => void;
14
- };
10
+ export declare const useTimeout: ({ onTimeout, duration }: UseTimeoutProps) => UseTimeoutReturn;
15
11
  export {};