@digital-ai/dot-components 4.6.0 → 4.7.0

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.esm.js CHANGED
@@ -8561,14 +8561,26 @@ const defaultColumns = {
8561
8561
  xl: 12,
8562
8562
  xxl: 12
8563
8563
  };
8564
+ // Ensures that the children returned is a list of children
8565
+ const ensureChildrenList = children => {
8566
+ if (children instanceof Array) {
8567
+ return children;
8568
+ }
8569
+ return [children];
8570
+ };
8564
8571
  const Grid = ({
8565
8572
  className,
8566
- children
8573
+ children,
8574
+ isLoading
8567
8575
  }) => {
8568
8576
  const rootClasses = useStylesWithRootClass(rootClassName$I, className);
8569
- return jsx("div", {
8577
+ return jsxs("div", {
8570
8578
  className: rootClasses,
8571
- children: children
8579
+ children: [children, isLoading && jsx(DotProgress, {
8580
+ className: "loading-more-data",
8581
+ color: "inherit",
8582
+ size: 20
8583
+ })]
8572
8584
  });
8573
8585
  };
8574
8586
  const StyledGrid = styled(Grid)`
@@ -8592,10 +8604,75 @@ const StyledGrid = styled(Grid)`
8592
8604
  ${gap && `grid-gap: ${gap};`}
8593
8605
  ${width && `width: ${width};`}
8594
8606
  ${height && `height: ${height};`}
8607
+ .loading-more-data {
8608
+ margin: auto;
8609
+ }
8595
8610
  `}
8596
8611
  `;
8597
- const CssGrid = props => {
8598
- return jsx(StyledGrid, Object.assign({}, props));
8612
+ const CssGrid = _a => {
8613
+ var {
8614
+ children,
8615
+ loadMoreData,
8616
+ onError
8617
+ } = _a,
8618
+ gridArgs = __rest(_a, ["children", "loadMoreData", "onError"]);
8619
+ const [page, setPage] = useState(0);
8620
+ const [isLoading, setIsLoading] = useState(false);
8621
+ const [isDone, setIsDone] = useState(false);
8622
+ const [gridItems, setGridItems] = useState(ensureChildrenList(children));
8623
+ const [lastElement, setLastElement] = useState(null);
8624
+ useEffect(() => {
8625
+ setGridItems(ensureChildrenList(children));
8626
+ }, [children]);
8627
+ const fetchData = useCallback(nextPage => __awaiter(void 0, void 0, void 0, function* () {
8628
+ setIsLoading(true);
8629
+ try {
8630
+ const newItems = yield loadMoreData(nextPage);
8631
+ if (newItems && newItems.length > 0) {
8632
+ setGridItems(prevItems => [...prevItems, ...newItems]);
8633
+ } else {
8634
+ setIsDone(true);
8635
+ }
8636
+ } catch (error) {
8637
+ if (onError) {
8638
+ onError(error);
8639
+ }
8640
+ } finally {
8641
+ setIsLoading(false);
8642
+ }
8643
+ }), [loadMoreData, onError]);
8644
+ const observer = useRef(new IntersectionObserver(entries => {
8645
+ const first = entries[0];
8646
+ if (first.isIntersecting) {
8647
+ setPage(prevNum => prevNum + 1);
8648
+ }
8649
+ }));
8650
+ useEffect(() => {
8651
+ if (loadMoreData && !isDone && page) {
8652
+ fetchData(page);
8653
+ }
8654
+ }, [page, loadMoreData, isDone]);
8655
+ useEffect(() => {
8656
+ const currentElement = lastElement;
8657
+ const currentObserver = observer.current;
8658
+ if (currentElement) {
8659
+ currentObserver.observe(currentElement);
8660
+ }
8661
+ return () => {
8662
+ if (currentElement) {
8663
+ currentObserver.unobserve(currentElement);
8664
+ }
8665
+ };
8666
+ }, [lastElement]);
8667
+ // Adds the lastElement ref to the last Element of the grid
8668
+ const gridItemsWithLastElement = useMemo(() => [...gridItems.slice(0, -1), jsx("div", {
8669
+ ref: setLastElement,
8670
+ children: gridItems[gridItems.length - 1]
8671
+ }, "last-grid-item")], [gridItems]);
8672
+ return jsx(StyledGrid, Object.assign({}, gridArgs, {
8673
+ isLoading: isLoading,
8674
+ children: gridItemsWithLastElement
8675
+ }));
8599
8676
  };
8600
8677
 
8601
8678
  const breakpointsGetter = (value, theme, breakpoint) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@digital-ai/dot-components",
3
- "version": "4.6.0",
3
+ "version": "4.7.0",
4
4
  "private": false,
5
5
  "license": "SEE LICENSE IN <LICENSE.md>",
6
6
  "contributors": [
@@ -23,6 +23,10 @@ export interface CssGridProps extends CommonProps {
23
23
  gridTemplateAreas?: string;
24
24
  /** Height of the grid */
25
25
  height?: string;
26
+ /** Function called to load more items when the end of the grid is reached. Should return an array of ReactNodes. */
27
+ loadMoreData?: (page: number) => Promise<ReactNode[]>;
28
+ /** Error handler function for handling any errors that might come from the loadMoreData function */
29
+ onError?: (error: Error) => void;
26
30
  /** Row, overrides columnsBreakpoints */
27
31
  rowGap?: GridOptions;
28
32
  /** Row, configuration */
@@ -30,9 +34,13 @@ export interface CssGridProps extends CommonProps {
30
34
  /** Width of the grid */
31
35
  width?: string;
32
36
  }
37
+ interface InternalGridProps extends CssGridProps {
38
+ isLoading: boolean;
39
+ }
33
40
  export declare const rootClassName = "dot-grid";
34
41
  export declare const defaultGutter: GridOptions;
35
42
  export declare const defaultColumns: GridOptions;
36
- export declare const Grid: ({ className, children }: CssGridProps) => import("react/jsx-runtime").JSX.Element;
37
- export declare const StyledGrid: import("styled-components").StyledComponent<({ className, children }: CssGridProps) => import("react/jsx-runtime").JSX.Element, any, {}, never>;
38
- export declare const CssGrid: (props: CssGridProps) => import("react/jsx-runtime").JSX.Element;
43
+ export declare const Grid: ({ className, children, isLoading }: InternalGridProps) => import("react/jsx-runtime").JSX.Element;
44
+ export declare const StyledGrid: import("styled-components").StyledComponent<({ className, children, isLoading }: InternalGridProps) => import("react/jsx-runtime").JSX.Element, any, {}, never>;
45
+ export declare const CssGrid: ({ children, loadMoreData, onError, ...gridArgs }: CssGridProps) => import("react/jsx-runtime").JSX.Element;
46
+ export {};