-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'bose/2470' into staging
- Loading branch information
Showing
10 changed files
with
540 additions
and
423 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,52 +1,106 @@ | ||
import { useEffect, useRef, useState, ChangeEvent, ComponentProps, useContext } from 'react' | ||
import { useRef, useState, ChangeEvent, ComponentProps } from 'react' | ||
import { useSearchBox, UseSearchBoxProps } from 'react-instantsearch' | ||
import ControlledSearchBox from './ControlledSearchBox' | ||
import { SearchContext } from './SearchContext' | ||
import { close, search } from '@equinor/eds-icons' | ||
import { useIntl } from 'react-intl' | ||
import { Icon } from '@equinor/eds-core-react' | ||
import envisTwMerge from '../../twMerge' | ||
|
||
const DEBOUNCE_TIME = 800 | ||
|
||
export type SearchBoxProps = ComponentProps<'div'> & UseSearchBoxProps | ||
|
||
let timerId: any = undefined | ||
export type SearchBoxProps = { | ||
className?: string | ||
resetClassName?: string | ||
submitClassName?: string | ||
} & ComponentProps<'div'> & | ||
UseSearchBoxProps | ||
|
||
const queryHook: UseSearchBoxProps['queryHook'] = (query, search) => { | ||
if (timerId) { | ||
clearTimeout(timerId) | ||
if (query !== '') { | ||
search(query) | ||
} | ||
timerId = setTimeout(() => search(query), DEBOUNCE_TIME) | ||
} | ||
|
||
export function SearchBox(props: SearchBoxProps) { | ||
// I don't think we need iSearchStalled when we don't have a manual reset button and/or loading | ||
// spinner if search is slow? Do we need a spinner if this happens? | ||
const { query, refine /* isSearchStalled */, clear } = useSearchBox({ ...props, queryHook }) | ||
export function SearchBox({ className = '', resetClassName = '', submitClassName = '', ...rest }: SearchBoxProps) { | ||
const intl = useIntl() | ||
const { query, refine, clear } = useSearchBox({ ...rest, queryHook }) | ||
const [value, setValue] = useState(query) | ||
const { setUserTyped } = useContext(SearchContext) | ||
const inputRef = useRef<HTMLInputElement>(null) | ||
|
||
function onReset() { | ||
function handleReset() { | ||
setValue('') | ||
clear() | ||
setUserTyped(false) | ||
} | ||
|
||
function onSubmit(event: React.FormEvent) { | ||
event.preventDefault() | ||
event.stopPropagation() | ||
refine(value) | ||
} | ||
|
||
function onChange(event: ChangeEvent<HTMLInputElement>) { | ||
setUserTyped(true) | ||
setValue(event.currentTarget.value) | ||
refine(event.currentTarget.value) | ||
} | ||
|
||
useEffect(() => { | ||
setValue(query) | ||
}, [query]) | ||
|
||
return ( | ||
<ControlledSearchBox | ||
inputRef={inputRef} | ||
/* isSearchStalled={isSearchStalled} */ | ||
onChange={onChange} | ||
onReset={onReset} | ||
value={value} | ||
/> | ||
<form action="" role="search" noValidate onSubmit={onSubmit} onReset={handleReset} className="flex "> | ||
<input | ||
aria-label={intl.formatMessage({ id: 'search', defaultMessage: 'Search' })} | ||
ref={inputRef} | ||
autoComplete="off" | ||
autoCorrect="off" | ||
autoCapitalize="off" | ||
placeholder={intl.formatMessage({ id: 'search', defaultMessage: 'Search' })} | ||
spellCheck={false} | ||
maxLength={512} | ||
type="search" | ||
value={value} | ||
onChange={onChange} | ||
className={envisTwMerge( | ||
`flex-grow | ||
border-y | ||
border-l | ||
border-white-100 | ||
bg-slate-blue-95 | ||
rounded-s-xs | ||
focus:outline-none | ||
focus-visible:envis-outline-invert | ||
text-white-100 | ||
pl-6 pr-12 | ||
py-4 | ||
no-webkit-search-cancel | ||
`, | ||
className, | ||
)} | ||
/> | ||
<button | ||
type="reset" | ||
hidden={value.length === 0} | ||
className={envisTwMerge( | ||
`group/reset px-4 border-y | ||
border-r | ||
border-white-100 focus:outline-none focus-visible:envis-outline-invert | ||
text-white-100 group-hover/reset:text-white-100/40`, | ||
resetClassName, | ||
)} | ||
> | ||
<Icon size={24} data={close} /> | ||
</button> | ||
<button | ||
type="submit" | ||
className={envisTwMerge( | ||
`h-inherit | ||
rounded-e-xs | ||
px-4 | ||
py-3 | ||
bg-white-100 | ||
text-slate-blue-95 | ||
hover:bg-white-100/40 | ||
hover:text-white-100 | ||
focus:outline-none | ||
focus-visible:envis-outline-invert`, | ||
submitClassName, | ||
)} | ||
> | ||
<Icon size={24} data={search} /> | ||
</button> | ||
</form> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.