1
0
mirror of synced 2025-12-19 18:10:59 -05:00
Files
docs/components/BasicSearch.tsx
Peter Bengtsson 8765c628ff dedicated search results page (redux) (#29902)
* dedicated search results page (redux)

* Update SearchResults.tsx

* adding pagination

* fix pagination

* say something on NoQuery

* better Flash

* tidying link

* small fixes for results

* debug info

* l18n the meta info

* inDebugMode

* basic jest rendering of the skeleton page

* basic jest rendering test

* fix content tests

* better document title

* fix tests

* quote query in page title

* use home page sidebar

* something when nothing is found

* parseInt no longer needs the 10

* fix linting tests

* fix test

* prettier

* Update pages/search.tsx

Co-authored-by: Rachael Sewell <rachmari@github.com>

Co-authored-by: Kevin Heis <heiskr@users.noreply.github.com>
Co-authored-by: Rachael Sewell <rachmari@github.com>
2022-08-19 13:36:55 +00:00

90 lines
2.9 KiB
TypeScript

import { useState, useRef } from 'react'
import { useRouter } from 'next/router'
import cx from 'classnames'
import { useTranslation } from 'components/hooks/useTranslation'
import { DEFAULT_VERSION, useVersion } from 'components/hooks/useVersion'
import { useQuery } from 'components/hooks/useQuery'
import styles from './Search.module.scss'
type Props = {
isHeaderSearch?: true
variant?: 'compact' | 'expanded'
iconSize: number
}
export function BasicSearch({ isHeaderSearch = true, variant = 'compact', iconSize = 24 }: Props) {
const router = useRouter()
const { query, debug } = useQuery()
const [localQuery, setLocalQuery] = useState(query)
const inputRef = useRef<HTMLInputElement>(null)
const { t } = useTranslation('search')
const { currentVersion } = useVersion()
function redirectSearch() {
let asPath = `/${router.locale}`
if (currentVersion !== DEFAULT_VERSION) {
asPath += `/${currentVersion}`
}
asPath += '/search'
const params = new URLSearchParams({ query: localQuery })
if (debug) {
params.set('debug', '1')
}
asPath += `?${params}`
router.push(asPath)
}
return (
<div data-testid="search">
<div className="position-relative z-2">
<form
role="search"
className="width-full d-flex"
noValidate
onSubmit={(event) => {
event.preventDefault()
redirectSearch()
}}
>
<label className="text-normal width-full">
<span
className="visually-hidden"
aria-label={t`label`}
aria-describedby={t`description`}
>{t`placeholder`}</span>
<input
data-testid="site-search-input"
ref={inputRef}
className={cx(
styles.searchInput,
iconSize === 24 && styles.searchIconBackground24,
iconSize === 24 && 'form-control px-6 f4',
iconSize === 16 && styles.searchIconBackground16,
iconSize === 16 && 'form-control px-5 f4',
variant === 'compact' && 'py-2',
variant === 'expanded' && 'py-3',
isHeaderSearch && styles.searchInputHeader,
!isHeaderSearch && 'width-full'
)}
type="search"
placeholder={t`placeholder`}
autoComplete={localQuery ? 'on' : 'off'}
autoCorrect="off"
autoCapitalize="off"
spellCheck="false"
maxLength={512}
onChange={(e) => setLocalQuery(e.target.value)}
value={localQuery}
aria-label={t`label`}
aria-describedby={t`description`}
/>
</label>
<button className="d-none" type="submit" title="Submit the search query." hidden />
</form>
</div>
</div>
)
}