()
| 9 | import { useBreakpoint } from './hooks/useBreakpoint' |
| 10 | |
| 11 | export function Search() { |
| 12 | const router = useRouter() |
| 13 | const { query, debug } = useQuery() |
| 14 | const [localQuery, setLocalQuery] = useState(query) |
| 15 | const { t } = useTranslation('search') |
| 16 | const { currentVersion } = useVersion() |
| 17 | const upToMediumViewport = useBreakpoint('medium') |
| 18 | |
| 19 | function redirectSearch() { |
| 20 | let asPath = `/${router.locale}` |
| 21 | if (currentVersion !== DEFAULT_VERSION) { |
| 22 | asPath += `/${currentVersion}` |
| 23 | } |
| 24 | asPath += '/search' |
| 25 | const params = new URLSearchParams({ query: localQuery }) |
| 26 | if (debug) { |
| 27 | params.set('debug', '1') |
| 28 | } |
| 29 | asPath += `?${params}` |
| 30 | router.push(asPath) |
| 31 | } |
| 32 | |
| 33 | return ( |
| 34 | <div data-testid="search"> |
| 35 | <div className="position-relative z-2"> |
| 36 | <form |
| 37 | role="search" |
| 38 | className="width-full d-flex" |
| 39 | onSubmit={(event) => { |
| 40 | event.preventDefault() |
| 41 | if (!localQuery.trim()) return |
| 42 | redirectSearch() |
| 43 | }} |
| 44 | > |
| 45 | {/* This prevents zooming in on iOS when you touch the search input text area */} |
| 46 | <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" /> |
| 47 | <label className="text-normal width-full"> |
| 48 | <span |
| 49 | className="visually-hidden" |
| 50 | aria-label={t`label`} |
| 51 | aria-describedby={t`description`} |
| 52 | >{t`placeholder`}</span> |
| 53 | <TextInput |
| 54 | required |
| 55 | onInvalid={(e) => |
| 56 | (e.target as HTMLInputElement).setCustomValidity('Please enter a search query.') |
| 57 | } |
| 58 | onInput={(e) => (e.target as HTMLInputElement).setCustomValidity('')} |
| 59 | data-testid="site-search-input" |
| 60 | // This adds focus in particular for iOS to focus and bring up the keyboard when you touch the search input text area |
| 61 | ref={(inputRef) => upToMediumViewport && inputRef && inputRef.focus()} |
| 62 | type="search" |
| 63 | placeholder={t`placeholder`} |
| 64 | autoComplete={localQuery ? 'on' : 'off'} |
| 65 | autoCorrect="off" |
| 66 | autoCapitalize="off" |
| 67 | spellCheck="false" |
| 68 | maxLength={512} |
nothing calls this directly
no test coverage detected