Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion components/lib/multiselect/MultiSelect.js
Original file line number Diff line number Diff line change
Expand Up @@ -604,7 +604,22 @@ export const MultiSelect = React.memo(
};

const findOptionIndexInList = (value, list) => {
return list.findIndex((item) => value.some((val) => ObjectUtils.equals(val, getOptionValue(item), equalityKey)));
if (!value || value.length === 0) {
return -1;
}

if (value.length <= 10) {
return list.findIndex((item) => value.some((val) => ObjectUtils.equals(val, getOptionValue(item), equalityKey)));
}

const valueSet = new Set(value.map((val) => (typeof val === 'object' ? JSON.stringify(val) : val)));

return list.findIndex((item) => {
const itemValue = getOptionValue(item);
const lookupValue = typeof itemValue === 'object' ? JSON.stringify(itemValue) : itemValue;

return valueSet.has(lookupValue) || value.some((val) => ObjectUtils.equals(val, itemValue, equalityKey));
});
};

const isEquals = (value1, value2) => {
Expand Down Expand Up @@ -753,6 +768,9 @@ export const MultiSelect = React.memo(

const findSelectedOptionIndex = () => {
if (hasSelectedOption()) {
if (props.value.length > 10) {
return visibleOptions.findIndex((option) => isValidSelectedOption(option));
}
for (let index = props.value.length - 1; index >= 0; index--) {
const value = props.value[index];
const matchedOptionIndex = visibleOptions.findIndex((option) => isValidSelectedOption(option) && isEquals(value, getOptionValue(option)));
Expand Down
9 changes: 7 additions & 2 deletions components/lib/multiselect/MultiSelectPanel.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,13 @@ export const MultiSelectPanel = React.memo(
if (virtualScrollerRef.current) {
const selectedIndex = props.getSelectedOptionIndex();

if (selectedIndex !== -1) {
setTimeout(() => virtualScrollerRef.current.scrollToIndex(selectedIndex), 0);
if (selectedIndex !== -1 && typeof selectedIndex === 'number') {
const maxSelectedForScroll = 100;
const selectedCount = props.value ? (Array.isArray(props.value) ? props.value.length : 1) : 0;

if (selectedCount <= maxSelectedForScroll) {
setTimeout(() => virtualScrollerRef.current.scrollToIndex(selectedIndex), 0);
}
}
}
});
Expand Down
Loading