Skip to content

Commit 69dfbf0

Browse files
(Lookup): memorize optionValues
1 parent cbe1da7 commit 69dfbf0

File tree

1 file changed

+5
-9
lines changed

1 file changed

+5
-9
lines changed

src/scripts/Lookup.tsx

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -923,8 +923,8 @@ export const Lookup = createFC<LookupProps, { isFormElement: boolean }>(
923923
[data]
924924
);
925925

926-
// Get option values from data
927-
const getOptionValues = useCallback(() => {
926+
// Memoized option values - filtered data with optional header and footer
927+
const optionValues = useMemo(() => {
928928
const filteredData = lookupFilter
929929
? data.filter((entry) => lookupFilter(entry, searchText, targetScope))
930930
: data;
@@ -947,7 +947,6 @@ export const Lookup = createFC<LookupProps, { isFormElement: boolean }>(
947947
// Get next option value for keyboard navigation
948948
const getNextValue = useCallback(
949949
(currentValue?: string) => {
950-
const optionValues = getOptionValues();
951950
if (optionValues.length === 0) return undefined;
952951

953952
if (!currentValue) return optionValues[0];
@@ -957,21 +956,20 @@ export const Lookup = createFC<LookupProps, { isFormElement: boolean }>(
957956
Math.min(currentIndex + 1, optionValues.length - 1)
958957
]; // not wrap around
959958
},
960-
[getOptionValues]
959+
[optionValues]
961960
);
962961

963962
// Get previous option value for keyboard navigation
964963
const getPrevValue = useCallback(
965964
(currentValue?: string) => {
966-
const optionValues = getOptionValues();
967965
if (optionValues.length === 0) return undefined;
968966

969967
if (!currentValue) return optionValues[optionValues.length - 1];
970968

971969
const currentIndex = optionValues.indexOf(currentValue);
972970
return optionValues[Math.max(currentIndex - 1, 0)]; // not wrap around
973971
},
974-
[getOptionValues]
972+
[optionValues]
975973
);
976974

977975
// Scroll focused element into view
@@ -999,7 +997,7 @@ export const Lookup = createFC<LookupProps, { isFormElement: boolean }>(
999997
if (!opened) {
1000998
setFocusedValue(undefined);
1001999
}
1002-
}, [opened, getOptionValues, focusedValue, scrollFocusedElementIntoView]);
1000+
}, [opened, optionValues, focusedValue, scrollFocusedElementIntoView]);
10031001

10041002
const elRef = useRef<HTMLDivElement | null>(null);
10051003
const elementRef = useMergeRefs([elRef, elementRef_]);
@@ -1101,7 +1099,6 @@ export const Lookup = createFC<LookupProps, { isFormElement: boolean }>(
11011099
}
11021100
},
11031101
isTabNavigationIgnored: (direction) => {
1104-
const optionValues = getOptionValues();
11051102
const currentIndex = focusedValue
11061103
? optionValues.indexOf(focusedValue)
11071104
: -1;
@@ -1113,7 +1110,6 @@ export const Lookup = createFC<LookupProps, { isFormElement: boolean }>(
11131110
);
11141111
},
11151112
onTabNavigation: (direction) => {
1116-
const optionValues = getOptionValues();
11171113
const currentIndex = focusedValue
11181114
? optionValues.indexOf(focusedValue)
11191115
: -1;

0 commit comments

Comments
 (0)