1+ import { spacers } from '@dhis2/ui-constants'
12import { CircularLoader } from '@dhis2-ui/loader'
23import PropTypes from 'prop-types'
3- import React , { Fragment , useRef } from 'react'
4+ import React , { Fragment , useEffect , useRef } from 'react'
45import { SimpleTransferOption } from './simple-transfer-option.js'
56
67export const OptionsContainer = ( {
78 dataTest,
89 emptyComponent,
10+ onEndReached,
911 highlightedOptions,
1012 loading,
1113 maxSelections,
@@ -14,8 +16,37 @@ export const OptionsContainer = ({
1416 selectionHandler,
1517 setHighlightedOptions,
1618} ) => {
17- const optionsRef = useRef ( null )
19+ const selectRef = useRef ( null )
1820 const wrapperRef = useRef ( null )
21+ // const resizeCounter = useResizeCounter(wrapperRef.current)
22+ const lastOptionRef = useRef ( null )
23+
24+ useEffect ( ( ) => {
25+ const observer = new IntersectionObserver (
26+ ( entries ) => {
27+ entries . forEach ( ( entry ) => {
28+ if ( entry . isIntersecting ) {
29+ onEndReached && onEndReached ( )
30+ }
31+ } )
32+ } ,
33+ {
34+ root : wrapperRef . current ,
35+ threshold : 1.0 ,
36+ }
37+ )
38+
39+ if ( lastOptionRef . current ) {
40+ observer . observe ( lastOptionRef . current )
41+ }
42+
43+ return ( ) => {
44+ if ( lastOptionRef . current ) {
45+ observer . unobserve ( lastOptionRef . current )
46+ }
47+ observer . disconnect ( )
48+ }
49+ } , [ options ] )
1950
2051 return (
2152 < div className = "optionsContainer" >
@@ -25,14 +56,14 @@ export const OptionsContainer = ({
2556 </ div >
2657 ) }
2758
28- < div className = "container" data-test = { dataTest } ref = { optionsRef } >
59+ < div className = "container" data-test = { dataTest } ref = { wrapperRef } >
2960 { ! options . length && emptyComponent }
3061 { ! ! options . length && (
3162 < select
63+ ref = { selectRef }
64+ className = "content-select"
3265 multiple = { maxSelections === Infinity }
3366 size = { maxSelections === 1 ? 2 : undefined }
34- className = "content-container"
35- ref = { wrapperRef }
3667 onChange = { ( e ) => {
3768 const nextSelected = [ ...e . target . options ] . reduce (
3869 ( curNextSelected , option ) => {
@@ -47,7 +78,7 @@ export const OptionsContainer = ({
4778 setHighlightedOptions ( nextSelected )
4879 } }
4980 >
50- { options . map ( ( option ) => {
81+ { options . map ( ( option , index ) => {
5182 const highlighted = ! ! highlightedOptions . find (
5283 ( highlightedSourceOption ) =>
5384 highlightedSourceOption === option . value
@@ -61,6 +92,11 @@ export const OptionsContainer = ({
6192 highlighted = { highlighted }
6293 selected = { selected }
6394 onDoubleClick = { selectionHandler }
95+ lastOptionReference = {
96+ index === options . length - 1
97+ ? lastOptionRef
98+ : undefined
99+ }
64100 />
65101 </ Fragment >
66102 )
@@ -70,6 +106,37 @@ export const OptionsContainer = ({
70106 </ div >
71107
72108 < style jsx > { `
109+ .optionsContainer {
110+ flex-grow: 1;
111+ padding: ${ spacers . dp4 } 0;
112+ position: relative;
113+ overflow: hidden;
114+ }
115+
116+ .container {
117+ overflow-y: auto;
118+ height: 100%;
119+ }
120+
121+ .loading {
122+ display: flex;
123+ height: 100%;
124+ width: 100%;
125+ align-items: center;
126+ justify-content: center;
127+ position: absolute;
128+ z-index: 2;
129+ top: 0;
130+ inset-inline-start: 0;
131+ }
132+
133+ .content-select {
134+ border: none;
135+ position: relative;
136+ height: 100%;
137+ width: 100%;
138+ }
139+
73140 .loading + .container .content-container {
74141 filter: blur(2px);
75142 }
@@ -93,4 +160,5 @@ OptionsContainer.propTypes = {
93160 ) ,
94161 selected : PropTypes . bool ,
95162 selectionHandler : PropTypes . func ,
163+ onEndReached : PropTypes . func ,
96164}
0 commit comments