overleaf/services/web/frontend/js/features/symbol-palette/components/symbol-palette-search.js
Alf Eaton d7b997e86c Merge pull request #4038 from overleaf/ae-move-use-debounce
Move useDebounce hook

GitOrigin-RevId: f047e279ca1694fc4fa68aa098d6608edefd20fe
2021-05-15 02:09:58 +00:00

36 lines
989 B
JavaScript

import React, { useEffect, useState } from 'react'
import { useTranslation } from 'react-i18next'
import PropTypes from 'prop-types'
import useDebounce from '../../../shared/hooks/use-debounce'
export default function SymbolPaletteSearch({ setInput, inputRef }) {
const [localInput, setLocalInput] = useState('')
// debounce the search input until a typing delay
const debouncedLocalInput = useDebounce(localInput, 250)
useEffect(() => {
setInput(debouncedLocalInput)
}, [debouncedLocalInput, setInput])
const { t } = useTranslation()
return (
<input
className="symbol-palette-search"
type="search"
ref={inputRef}
id="symbol-palette-input"
aria-label="Search"
value={localInput}
placeholder={t('search') + '…'}
onChange={event => {
setLocalInput(event.target.value)
}}
/>
)
}
SymbolPaletteSearch.propTypes = {
setInput: PropTypes.func.isRequired,
inputRef: PropTypes.object.isRequired,
}