Skip to content

Commit 86d569a

Browse files
authored
Merge pull request #46 from constructive-io/devin/1766819656-fix-checkbox-empty-filter
fix(inquirerer): handle empty filteredOptions in checkbox and autocomplete
2 parents 833fb88 + 7d4b32c commit 86d569a

1 file changed

Lines changed: 32 additions & 0 deletions

File tree

packages/inquirerer/src/prompt.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -923,6 +923,18 @@ export class Inquirerer {
923923

924924
const updateFilteredOptions = (): void => {
925925
filteredOptions = this.filterOptions(options, input);
926+
// Clamp selectedIndex and startIndex when filtered results change
927+
if (filteredOptions.length === 0) {
928+
selectedIndex = 0;
929+
startIndex = 0;
930+
} else {
931+
if (selectedIndex >= filteredOptions.length) {
932+
selectedIndex = filteredOptions.length - 1;
933+
}
934+
if (startIndex > Math.max(0, filteredOptions.length - maxLines)) {
935+
startIndex = Math.max(0, filteredOptions.length - maxLines);
936+
}
937+
}
926938
};
927939

928940
const display = (): void => {
@@ -963,6 +975,10 @@ export class Inquirerer {
963975
});
964976

965977
this.keypress.on(KEY_CODES.UP_ARROW, () => {
978+
if (filteredOptions.length === 0) {
979+
display();
980+
return;
981+
}
966982
selectedIndex = selectedIndex > 0 ? selectedIndex - 1 : filteredOptions.length - 1;
967983
if (selectedIndex < startIndex) {
968984
startIndex = selectedIndex; // Scroll up
@@ -973,6 +989,10 @@ export class Inquirerer {
973989
});
974990

975991
this.keypress.on(KEY_CODES.DOWN_ARROW, () => {
992+
if (filteredOptions.length === 0) {
993+
display();
994+
return;
995+
}
976996
selectedIndex = (selectedIndex + 1) % filteredOptions.length;
977997
if (selectedIndex >= startIndex + maxLines) {
978998
startIndex = selectedIndex - maxLines + 1; // Scroll down
@@ -983,6 +1003,10 @@ export class Inquirerer {
9831003
});
9841004

9851005
this.keypress.on(KEY_CODES.SPACE, () => {
1006+
if (filteredOptions.length === 0 || !filteredOptions[selectedIndex]) {
1007+
display();
1008+
return;
1009+
}
9861010
// Map filtered index back to the original index in options
9871011
selections[options.indexOf(filteredOptions[selectedIndex])] = !selections[options.indexOf(filteredOptions[selectedIndex])];
9881012
display();
@@ -1089,6 +1113,10 @@ export class Inquirerer {
10891113

10901114
// Navigation
10911115
this.keypress.on(KEY_CODES.UP_ARROW, () => {
1116+
if (filteredOptions.length === 0) {
1117+
display();
1118+
return;
1119+
}
10921120
selectedIndex = selectedIndex - 1 >= 0 ? selectedIndex - 1 : filteredOptions.length - 1;
10931121
if (selectedIndex < startIndex) {
10941122
startIndex = selectedIndex; // Scroll up
@@ -1098,6 +1126,10 @@ export class Inquirerer {
10981126
display();
10991127
});
11001128
this.keypress.on(KEY_CODES.DOWN_ARROW, () => {
1129+
if (filteredOptions.length === 0) {
1130+
display();
1131+
return;
1132+
}
11011133
selectedIndex = (selectedIndex + 1) % filteredOptions.length;
11021134
if (selectedIndex >= startIndex + maxLines) {
11031135
startIndex = selectedIndex - maxLines + 1; // Scroll down

0 commit comments

Comments
 (0)