Guard SlashMenu navigation against empty filtered items#19
Conversation
Co-authored-by: akshad-exe <[email protected]>
There was a problem hiding this comment.
Pull request overview
Fixes a keyboard-navigation edge case in the editor SlashMenu where filtering down to zero items could cause modulo-by-zero (% 0) and set selectedIndex to NaN, breaking subsequent navigation.
Changes:
- Guard
ArrowDown/ArrowUpselection updates whenfilteredItems.length === 0. - Guard
Enterselection to avoid selecting when there are no filtered items.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| if (e.key === 'ArrowDown') { | ||
| e.preventDefault(); | ||
| setSelectedIndex((prev) => (prev + 1) % filteredItems.length); | ||
| if (filteredItems.length > 0) { | ||
| setSelectedIndex((prev) => (prev + 1) % filteredItems.length); | ||
| } |
There was a problem hiding this comment.
Consider adding an e2e regression test for this empty-results navigation case (e.g., type a slash query that yields 0 suggestions, press ArrowDown/ArrowUp/Enter, then continue typing/backspacing and verify the editor remains responsive and subsequent navigation works). The repo already has Playwright coverage for slash-menu keyboard navigation, but it doesn’t currently exercise the 0-items path that previously produced NaN.
Arrow key navigation in SlashMenu breaks when filtering returns zero items. Modulo operations like
(prev + 1) % 0yieldNaN, poisoningselectedIndexand breaking all subsequent navigation.Changes:
filteredItems.length === 0💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.