chore: fix search bar focus handling (#2932)
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
import { ItemListController } from '@/Controllers/ItemList/ItemListController'
|
import { ItemListController } from '@/Controllers/ItemList/ItemListController'
|
||||||
import { KeyboardKey } from '@standardnotes/ui-services'
|
import { KeyboardKey } from '@standardnotes/ui-services'
|
||||||
import { useState, useCallback, KeyboardEventHandler, useRef } from 'react'
|
import { useCallback, KeyboardEventHandler, useRef } from 'react'
|
||||||
import SearchOptions from '@/Components/SearchOptions/SearchOptions'
|
import SearchOptions from '@/Components/SearchOptions/SearchOptions'
|
||||||
import { SearchOptionsController } from '@/Controllers/SearchOptionsController'
|
import { SearchOptionsController } from '@/Controllers/SearchOptionsController'
|
||||||
import Icon from '../Icon/Icon'
|
import Icon from '../Icon/Icon'
|
||||||
@@ -8,6 +8,7 @@ import DecoratedInput from '../Input/DecoratedInput'
|
|||||||
import { observer } from 'mobx-react-lite'
|
import { observer } from 'mobx-react-lite'
|
||||||
import ClearInputButton from '../ClearInputButton/ClearInputButton'
|
import ClearInputButton from '../ClearInputButton/ClearInputButton'
|
||||||
import { ElementIds } from '@/Constants/ElementIDs'
|
import { ElementIds } from '@/Constants/ElementIDs'
|
||||||
|
import { classNames } from '@standardnotes/snjs'
|
||||||
|
|
||||||
type Props = {
|
type Props = {
|
||||||
itemListController: ItemListController
|
itemListController: ItemListController
|
||||||
@@ -16,12 +17,11 @@ type Props = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const SearchBar = ({ itemListController, searchOptionsController, hideOptions = false }: Props) => {
|
const SearchBar = ({ itemListController, searchOptionsController, hideOptions = false }: Props) => {
|
||||||
|
const searchBarRef = useRef<HTMLDivElement>(null)
|
||||||
const searchInputRef = useRef<HTMLInputElement>(null)
|
const searchInputRef = useRef<HTMLInputElement>(null)
|
||||||
|
|
||||||
const { noteFilterText, setNoteFilterText, clearFilterText, onFilterEnter } = itemListController
|
const { noteFilterText, setNoteFilterText, clearFilterText, onFilterEnter } = itemListController
|
||||||
|
|
||||||
const [focusedSearch, setFocusedSearch] = useState(false)
|
|
||||||
|
|
||||||
const onNoteFilterTextChange = useCallback(
|
const onNoteFilterTextChange = useCallback(
|
||||||
(text: string) => {
|
(text: string) => {
|
||||||
setNoteFilterText(text)
|
setNoteFilterText(text)
|
||||||
@@ -38,16 +38,13 @@ const SearchBar = ({ itemListController, searchOptionsController, hideOptions =
|
|||||||
[onFilterEnter],
|
[onFilterEnter],
|
||||||
)
|
)
|
||||||
|
|
||||||
const onSearchFocus = useCallback(() => setFocusedSearch(true), [])
|
|
||||||
const onSearchBlur = useCallback(() => setFocusedSearch(false), [])
|
|
||||||
|
|
||||||
const onClearSearch = useCallback(() => {
|
const onClearSearch = useCallback(() => {
|
||||||
clearFilterText()
|
clearFilterText()
|
||||||
searchInputRef.current?.focus()
|
searchInputRef.current?.focus()
|
||||||
}, [clearFilterText])
|
}, [clearFilterText])
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="pb-0.5 pt-3" role="search">
|
<div className="group pb-0.5 pt-3" role="search" ref={searchBarRef}>
|
||||||
<DecoratedInput
|
<DecoratedInput
|
||||||
autocomplete={false}
|
autocomplete={false}
|
||||||
id={ElementIds.SearchBar}
|
id={ElementIds.SearchBar}
|
||||||
@@ -58,20 +55,21 @@ const SearchBar = ({ itemListController, searchOptionsController, hideOptions =
|
|||||||
placeholder={'Search...'}
|
placeholder={'Search...'}
|
||||||
value={noteFilterText}
|
value={noteFilterText}
|
||||||
ref={searchInputRef}
|
ref={searchInputRef}
|
||||||
onBlur={onSearchBlur}
|
|
||||||
onChange={onNoteFilterTextChange}
|
onChange={onNoteFilterTextChange}
|
||||||
onFocus={onSearchFocus}
|
|
||||||
onKeyUp={onNoteFilterKeyUp}
|
onKeyUp={onNoteFilterKeyUp}
|
||||||
left={[<Icon type="search" className="mr-1 h-4.5 w-4.5 flex-shrink-0 text-passive-1" />]}
|
left={[<Icon type="search" className="mr-1 h-4.5 w-4.5 flex-shrink-0 text-passive-1" />]}
|
||||||
right={[noteFilterText && <ClearInputButton onClick={onClearSearch} />]}
|
right={[noteFilterText && <ClearInputButton onClick={onClearSearch} />]}
|
||||||
roundedFull
|
roundedFull
|
||||||
/>
|
/>
|
||||||
|
|
||||||
{(focusedSearch || noteFilterText) && !hideOptions && (
|
<div
|
||||||
<div className="animate-fade-from-top">
|
className={classNames(
|
||||||
<SearchOptions searchOptions={searchOptionsController} />
|
'animate-fade-from-top',
|
||||||
</div>
|
hideOptions ? 'hidden' : !noteFilterText && 'hidden group-focus-within:flex',
|
||||||
)}
|
)}
|
||||||
|
>
|
||||||
|
<SearchOptions searchOptions={searchOptionsController} />
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user