From 8970f50f1de68c75271b42b6e1ab586b25bfbe29 Mon Sep 17 00:00:00 2001 From: Flaminia Cavallo Date: Thu, 31 Oct 2024 11:35:59 +0100 Subject: [PATCH] feat: make infinite loading work for simple transfer --- .../simple-transfer/src/options-container.js | 80 +++++++++++++++++-- .../src/simple-transfer-option.js | 3 + .../src/simple-transfer.prod.stories.js | 76 +----------------- 3 files changed, 78 insertions(+), 81 deletions(-) diff --git a/components/simple-transfer/src/options-container.js b/components/simple-transfer/src/options-container.js index 8986e4ef8..5bb5b569f 100644 --- a/components/simple-transfer/src/options-container.js +++ b/components/simple-transfer/src/options-container.js @@ -1,11 +1,13 @@ +import { spacers } from '@dhis2/ui-constants' import { CircularLoader } from '@dhis2-ui/loader' import PropTypes from 'prop-types' -import React, { Fragment, useRef } from 'react' +import React, { Fragment, useEffect, useRef } from 'react' import { SimpleTransferOption } from './simple-transfer-option.js' export const OptionsContainer = ({ dataTest, emptyComponent, + onEndReached, highlightedOptions, loading, maxSelections, @@ -14,8 +16,37 @@ export const OptionsContainer = ({ selectionHandler, setHighlightedOptions, }) => { - const optionsRef = useRef(null) + const selectRef = useRef(null) const wrapperRef = useRef(null) + // const resizeCounter = useResizeCounter(wrapperRef.current) + const lastOptionRef = useRef(null) + + useEffect(() => { + const observer = new IntersectionObserver( + (entries) => { + entries.forEach((entry) => { + if (entry.isIntersecting) { + onEndReached && onEndReached() + } + }) + }, + { + root: wrapperRef.current, + threshold: 1.0, + } + ) + + if (lastOptionRef.current) { + observer.observe(lastOptionRef.current) + } + + return () => { + if (lastOptionRef.current) { + observer.unobserve(lastOptionRef.current) + } + observer.disconnect() + } + }, [options]) return (
@@ -25,14 +56,14 @@ export const OptionsContainer = ({
)} -
+
{!options.length && emptyComponent} {!!options.length && (