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

added focus functionality on home page search bar #962

Merged
merged 5 commits into from
Mar 2, 2025
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
12 changes: 12 additions & 0 deletions frontend/src/components/MultiSearch.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ const MultiSearchBar: React.FC<MultiSearchBarProps> = ({
const pageCount = 1
const suggestionCount = 3
const searchBarRef = useRef<HTMLDivElement>(null)
const inputRef = useRef<HTMLInputElement>(null)

const debouncedSearch = useMemo(
() =>
Expand Down Expand Up @@ -87,6 +88,7 @@ const MultiSearchBar: React.FC<MultiSearchBarProps> = ({
const handleKeyDown = (event: KeyboardEvent) => {
if (event.key === 'Escape') {
setShowSuggestions(false)
inputRef.current?.blur()
} else if (event.key === 'Enter' && highlightedIndex !== null) {
const { index, subIndex } = highlightedIndex
const suggestion = suggestions[index].hits[subIndex]
Expand Down Expand Up @@ -154,6 +156,14 @@ const MultiSearchBar: React.FC<MultiSearchBarProps> = ({
setHighlightedIndex(null)
}

const handleFocusSearch = () => {
if (searchQuery.trim().length > 0 && !showSuggestions) {
setShowSuggestions(true)
} else {
setHighlightedIndex(null)
}
}

const getIconForIndex = (indexName: string) => {
switch (indexName) {
case 'chapters':
Expand All @@ -179,7 +189,9 @@ const MultiSearchBar: React.FC<MultiSearchBarProps> = ({
<input
type="text"
value={searchQuery}
ref={inputRef}
onChange={handleSearchChange}
onFocus={handleFocusSearch}
placeholder={placeholder}
className="h-12 w-full rounded-lg border border-gray-300 pl-10 pr-10 text-lg text-black focus:border-blue-500 focus:outline-none focus:ring-2 focus:ring-blue-500 dark:border-gray-600 dark:bg-gray-800 dark:text-white dark:focus:border-blue-300 dark:focus:ring-blue-300"
/>
Expand Down