Skip to content

Commit 97e4f9a

Browse files
committed
Issue: #535
fix: react crash, virtual scroller does not have all items. First scroll to item, then highlight the item inside the shown virtual items.
1 parent 33dc7b0 commit 97e4f9a

1 file changed

Lines changed: 13 additions & 6 deletions

File tree

src/main/components/editors/linked/UIEditorLinked.tsx

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ import { formatCellEditorDateValue } from "../../table/CellRenderer/DateCellRend
5151
import { AppContextType } from "../../../contexts/AppProvider";
5252
import { ICellEditorDate } from "../date/UIEditorDate";
5353
import useAppContext from "../../../hooks/app-hooks/useAppContext";
54+
import { VirtualScroller } from "primereact/virtualscroller";
5455

5556
interface ReferencedColumnNames {
5657
columnNames: string[]
@@ -875,7 +876,7 @@ const UIEditorLinked: FC<IEditorLinked & IExtendableLinkedEditor & IComponentCon
875876

876877
// Handles the lazy-load, if the linked is at the end but not every row is fetched, it fetches 100 new rows
877878
const handleLazyLoad = (event:any) => {
878-
if (event.last >= providedData.length && !props.context.contentStore.getDataBook(props.screenName, props.cellEditor.linkReference.referencedDataBook || "")?.isAllFetched) {
879+
if (event.last >= providedData.length && !props.context.contentStore.getDataBook(props.screenName, props.cellEditor.linkReference.referencedDataBook || "")?.isAllFetched) {
879880
const fetchReq = createFetchRequest();
880881
fetchReq.dataProvider = props.cellEditor.linkReference.referencedDataBook;
881882
fetchReq.fromRow = providedData.length;
@@ -1112,7 +1113,6 @@ const UIEditorLinked: FC<IEditorLinked & IExtendableLinkedEditor & IComponentCon
11121113
}
11131114
}}
11141115
onShow={() => {
1115-
//select currently selected suggestion
11161116
if (suggestions.length && linkedRef.current?.getOverlay() && props.columnName) {
11171117
const { linkReference } = props.cellEditor;
11181118
const grouped = suggestions.length == 1 && suggestions[0].items?.length;
@@ -1121,10 +1121,17 @@ const UIEditorLinked: FC<IEditorLinked & IExtendableLinkedEditor & IComponentCon
11211121
(s:any) => s[linkReference.referencedColumnNames[linkReference.columnNames.indexOf(props.columnName)]] == props.selectedRow?.data[props.columnName]
11221122
);
11231123
if(index >= 0) {
1124-
const el = linkedRef.current?.getOverlay().querySelectorAll('.p-autocomplete-item')[index];
1125-
el.classList.add('p-highlight');
1126-
el.setAttribute('data-p-highlight', 'true');
1127-
el.scrollIntoView({ behavior: "instant" as ScrollBehavior });
1124+
// Virtual scroller does not scroll, if index is smaller than last - 2
1125+
// We want to show 2 items above the selected item, that the selected item is not the top one.
1126+
const last = linkedRef.current?.getVirtualScroller().getRenderedRange().viewport.last;
1127+
const scrollpos = index < last ? 0 : index - 2;
1128+
linkedRef.current?.getVirtualScroller().scrollToIndex(scrollpos, "auto");
1129+
1130+
setTimeout(() => {
1131+
const el = linkedRef.current?.getOverlay().querySelectorAll('.p-autocomplete-item')[index - linkedRef.current?.getVirtualScroller().getRenderedRange().first];
1132+
el?.classList.add('p-highlight');
1133+
el?.setAttribute('data-p-highlight', 'true');
1134+
}, 50);
11281135
}
11291136
}
11301137
}}

0 commit comments

Comments
 (0)