Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: handle keydown event handling in Popover component #143

Merged
merged 1 commit into from
Nov 27, 2024
Merged
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
18 changes: 10 additions & 8 deletions src/components/chat-view/ChatListDropdown.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as Popover from '@radix-ui/react-popover'
import { Pencil, Trash2 } from 'lucide-react'
import { useEffect, useRef, useState } from 'react'
import { useCallback, useEffect, useRef, useState } from 'react'

import { ChatConversationMeta } from '../../types/chat'

Expand Down Expand Up @@ -135,10 +135,10 @@
setFocusedIndex(currentIndex === -1 ? 0 : currentIndex)
setEditingId(null)
}
}, [open])

Check warning on line 138 in src/components/chat-view/ChatListDropdown.tsx

View workflow job for this annotation

GitHub Actions / check

React Hook useEffect has missing dependencies: 'chatList' and 'currentConversationId'. Either include them or remove the dependency array

useEffect(() => {
const handleKeyDown = (e: KeyboardEvent) => {
const handleKeyDown = useCallback(
(e: React.KeyboardEvent) => {
if (e.key === 'ArrowUp') {
setFocusedIndex(Math.max(0, focusedIndex - 1))
} else if (e.key === 'ArrowDown') {
Expand All @@ -147,10 +147,9 @@
onSelect(chatList[focusedIndex].id)
setOpen(false)
}
}
window.addEventListener('keydown', handleKeyDown)
return () => window.removeEventListener('keydown', handleKeyDown)
}, [chatList, focusedIndex, setFocusedIndex, onSelect])
},
[chatList, focusedIndex, setFocusedIndex, onSelect],
)

return (
<Popover.Root open={open} onOpenChange={setOpen}>
Expand All @@ -159,7 +158,10 @@
</Popover.Trigger>

<Popover.Portal>
<Popover.Content className="smtcmp-popover smtcmp-chat-list-dropdown-content">
<Popover.Content
className="smtcmp-popover smtcmp-chat-list-dropdown-content"
onKeyDown={handleKeyDown}
>
<ul>
{chatList.length === 0 ? (
<li className="smtcmp-chat-list-dropdown-empty">
Expand Down