Improve rendering of large lists
This commit is contained in:
parent
2b92211b99
commit
baff4413a5
1 changed files with 13 additions and 1 deletions
|
@ -124,6 +124,8 @@ function getChoiceIndex(
|
||||||
return choiceIndex;
|
return choiceIndex;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const sliceSize = 20;
|
||||||
|
|
||||||
const SingleAutocompleteSelectFieldContent: React.FC<SingleAutocompleteSelectFieldContentProps> = props => {
|
const SingleAutocompleteSelectFieldContent: React.FC<SingleAutocompleteSelectFieldContentProps> = props => {
|
||||||
const {
|
const {
|
||||||
add,
|
add,
|
||||||
|
@ -147,6 +149,7 @@ const SingleAutocompleteSelectFieldContent: React.FC<SingleAutocompleteSelectFie
|
||||||
const anchor = React.useRef<HTMLDivElement>();
|
const anchor = React.useRef<HTMLDivElement>();
|
||||||
const scrollPosition = useElementScroll(anchor);
|
const scrollPosition = useElementScroll(anchor);
|
||||||
const [calledForMore, setCalledForMore] = React.useState(false);
|
const [calledForMore, setCalledForMore] = React.useState(false);
|
||||||
|
const [slice, setSlice] = React.useState(sliceSize);
|
||||||
|
|
||||||
const scrolledToBottom = isScrolledToBottom(anchor, scrollPosition, offset);
|
const scrolledToBottom = isScrolledToBottom(anchor, scrollPosition, offset);
|
||||||
|
|
||||||
|
@ -154,9 +157,18 @@ const SingleAutocompleteSelectFieldContent: React.FC<SingleAutocompleteSelectFie
|
||||||
if (!calledForMore && onFetchMore && scrolledToBottom) {
|
if (!calledForMore && onFetchMore && scrolledToBottom) {
|
||||||
onFetchMore();
|
onFetchMore();
|
||||||
setCalledForMore(true);
|
setCalledForMore(true);
|
||||||
|
} else if (scrolledToBottom) {
|
||||||
|
setSlice(slice => slice + sliceSize);
|
||||||
}
|
}
|
||||||
}, [scrolledToBottom]);
|
}, [scrolledToBottom]);
|
||||||
|
|
||||||
|
React.useEffect(() => {
|
||||||
|
setSlice(sliceSize);
|
||||||
|
anchor.current.scrollTo({
|
||||||
|
top: 0
|
||||||
|
});
|
||||||
|
}, [choices?.length]);
|
||||||
|
|
||||||
React.useEffect(() => {
|
React.useEffect(() => {
|
||||||
if (calledForMore && !loading) {
|
if (calledForMore && !loading) {
|
||||||
setCalledForMore(false);
|
setCalledForMore(false);
|
||||||
|
@ -219,7 +231,7 @@ const SingleAutocompleteSelectFieldContent: React.FC<SingleAutocompleteSelectFie
|
||||||
{choices.length > 0 && (!!add || displayCustomValue) && (
|
{choices.length > 0 && (!!add || displayCustomValue) && (
|
||||||
<Hr className={classes.hr} />
|
<Hr className={classes.hr} />
|
||||||
)}
|
)}
|
||||||
{choices.map((suggestion, index) => {
|
{choices.slice(0, slice).map((suggestion, index) => {
|
||||||
const choiceIndex = getChoiceIndex(
|
const choiceIndex = getChoiceIndex(
|
||||||
index,
|
index,
|
||||||
emptyOption,
|
emptyOption,
|
||||||
|
|
Loading…
Reference in a new issue