import { useEffect, useRef } from 'react'; import { Icon } from '../Icon'; interface Props { value: string; onChange: (value: string) => void; } /** * Search input with a `/` keyboard shortcut for focus. Ignores the shortcut * when the user is already typing into another input or textarea. */ export const SearchField = ({ value, onChange }: Props) => { const inputRef = useRef(null); useEffect(() => { const onKey = (e: KeyboardEvent) => { if (e.key !== '/') return; const target = e.target as HTMLElement | null; if (target && (target.tagName === 'INPUT' || target.tagName === 'TEXTAREA')) return; e.preventDefault(); inputRef.current?.focus(); }; window.addEventListener('keydown', onKey); return () => window.removeEventListener('keydown', onKey); }, []); const blurInput = () => inputRef.current?.blur(); return (
onChange(e.target.value)} onKeyDown={(e) => { if (e.key === 'Enter') { e.preventDefault(); blurInput(); } else if (e.key === 'Escape') { e.preventDefault(); onChange(''); blurInput(); } }} spellCheck={false} /> {value && ( )} /
); };