Refactor search suggestion keyboard interaction
This commit is contained in:
parent
6cd3d1fef6
commit
d23af171f7
@ -1,4 +1,4 @@
|
|||||||
import { useState, useEffect, useCallback, Fragment, useRef } from "react";
|
import { useState, useEffect, Fragment } from "react";
|
||||||
import { useTranslation } from "next-i18next";
|
import { useTranslation } from "next-i18next";
|
||||||
import { FiSearch } from "react-icons/fi";
|
import { FiSearch } from "react-icons/fi";
|
||||||
import { SiDuckduckgo, SiMicrosoftbing, SiGoogle, SiBaidu, SiBrave } from "react-icons/si";
|
import { SiDuckduckgo, SiMicrosoftbing, SiGoogle, SiBaidu, SiBrave } from "react-icons/si";
|
||||||
@ -71,9 +71,6 @@ export function getStoredProvider() {
|
|||||||
export default function Search({ options }) {
|
export default function Search({ options }) {
|
||||||
const { t } = useTranslation();
|
const { t } = useTranslation();
|
||||||
|
|
||||||
const searchProviderButton = useRef();
|
|
||||||
const comboboxOptions = useRef();
|
|
||||||
|
|
||||||
const availableProviderIds = getAvailableProviderIds(options);
|
const availableProviderIds = getAvailableProviderIds(options);
|
||||||
|
|
||||||
const [query, setQuery] = useState("");
|
const [query, setQuery] = useState("");
|
||||||
@ -122,15 +119,9 @@ export default function Search({ options }) {
|
|||||||
};
|
};
|
||||||
}, [selectedProvider, options, query, searchSuggestions]);
|
}, [selectedProvider, options, query, searchSuggestions]);
|
||||||
|
|
||||||
const handleSearchKeyDown = (event) => {
|
let currentSuggestion;
|
||||||
if (event.key === "Tab" && comboboxOptions.current?.getAttribute("data-headlessui-state") === "open") {
|
|
||||||
searchProviderButton.current.focus();
|
|
||||||
event.preventDefault();
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
const submitCallback = useCallback(
|
function doSearch(value) {
|
||||||
(value) => {
|
|
||||||
const q = encodeURIComponent(value);
|
const q = encodeURIComponent(value);
|
||||||
const { url } = selectedProvider;
|
const { url } = selectedProvider;
|
||||||
if (url) {
|
if (url) {
|
||||||
@ -140,9 +131,15 @@ export default function Search({ options }) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
setQuery("");
|
setQuery("");
|
||||||
},
|
currentSuggestion = null;
|
||||||
[selectedProvider, options.url, options.target],
|
}
|
||||||
);
|
|
||||||
|
const handleSearchKeyDown = (event) => {
|
||||||
|
const useSuggestion = searchSuggestions.length && currentSuggestion;
|
||||||
|
if (event.key === "Enter") {
|
||||||
|
doSearch(useSuggestion ? currentSuggestion : event.target.value);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
if (!availableProviderIds) {
|
if (!availableProviderIds) {
|
||||||
return null;
|
return null;
|
||||||
@ -158,7 +155,7 @@ export default function Search({ options }) {
|
|||||||
<Raw>
|
<Raw>
|
||||||
<div className="flex-col relative h-8 my-4 min-w-fit z-20">
|
<div className="flex-col relative h-8 my-4 min-w-fit z-20">
|
||||||
<div className="flex absolute inset-y-0 left-0 items-center pl-3 pointer-events-none w-full text-theme-800 dark:text-white" />
|
<div className="flex absolute inset-y-0 left-0 items-center pl-3 pointer-events-none w-full text-theme-800 dark:text-white" />
|
||||||
<Combobox value={query} onChange={submitCallback}>
|
<Combobox value={query}>
|
||||||
<Combobox.Input
|
<Combobox.Input
|
||||||
type="text"
|
type="text"
|
||||||
className="
|
className="
|
||||||
@ -170,7 +167,9 @@ export default function Search({ options }) {
|
|||||||
focus:border-theme-500 dark:focus:border-white/50
|
focus:border-theme-500 dark:focus:border-white/50
|
||||||
border border-theme-300 dark:border-theme-200/50"
|
border border-theme-300 dark:border-theme-200/50"
|
||||||
placeholder={t("search.placeholder")}
|
placeholder={t("search.placeholder")}
|
||||||
onChange={(event) => setQuery(event.target.value)}
|
onChange={(event) => {
|
||||||
|
setQuery(event.target.value);
|
||||||
|
}}
|
||||||
required
|
required
|
||||||
autoCapitalize="off"
|
autoCapitalize="off"
|
||||||
autoCorrect="off"
|
autoCorrect="off"
|
||||||
@ -194,7 +193,6 @@ export default function Search({ options }) {
|
|||||||
text-white font-medium text-sm
|
text-white font-medium text-sm
|
||||||
bg-theme-600/40 dark:bg-white/10
|
bg-theme-600/40 dark:bg-white/10
|
||||||
focus:ring-theme-500 dark:focus:ring-white/50"
|
focus:ring-theme-500 dark:focus:ring-white/50"
|
||||||
ref={searchProviderButton}
|
|
||||||
>
|
>
|
||||||
<selectedProvider.icon className="text-white w-3 h-3" />
|
<selectedProvider.icon className="text-white w-3 h-3" />
|
||||||
<span className="sr-only">{t("search.search")}</span>
|
<span className="sr-only">{t("search.search")}</span>
|
||||||
@ -238,15 +236,21 @@ export default function Search({ options }) {
|
|||||||
</Listbox>
|
</Listbox>
|
||||||
|
|
||||||
{searchSuggestions[1]?.length > 0 && (
|
{searchSuggestions[1]?.length > 0 && (
|
||||||
<Combobox.Options
|
<Combobox.Options className="mt-1 rounded-md bg-theme-50 dark:bg-theme-800 border border-theme-300 dark:border-theme-200/30 cursor-pointer shadow-lg">
|
||||||
className="mt-1 rounded-md bg-theme-50 dark:bg-theme-800 border border-theme-300 dark:border-theme-200/30 cursor-pointer shadow-lg"
|
|
||||||
ref={comboboxOptions}
|
|
||||||
>
|
|
||||||
<div className="p-1 bg-white/50 dark:bg-white/10 text-theme-900/90 dark:text-white/90 text-xs">
|
<div className="p-1 bg-white/50 dark:bg-white/10 text-theme-900/90 dark:text-white/90 text-xs">
|
||||||
<Combobox.Option key={query} value={query} />
|
<Combobox.Option key={query} value={query} />
|
||||||
{searchSuggestions[1].map((suggestion) => (
|
{searchSuggestions[1].map((suggestion) => (
|
||||||
<Combobox.Option key={suggestion} value={suggestion} className="flex w-full">
|
<Combobox.Option
|
||||||
{({ active }) => (
|
key={suggestion}
|
||||||
|
value={suggestion}
|
||||||
|
onClick={() => {
|
||||||
|
doSearch(suggestion);
|
||||||
|
}}
|
||||||
|
className="flex w-full"
|
||||||
|
>
|
||||||
|
{({ active }) => {
|
||||||
|
if (active) currentSuggestion = suggestion;
|
||||||
|
return (
|
||||||
<div
|
<div
|
||||||
className={classNames(
|
className={classNames(
|
||||||
"px-2 py-1 rounded-md w-full flex-nowrap",
|
"px-2 py-1 rounded-md w-full flex-nowrap",
|
||||||
@ -258,7 +262,8 @@ export default function Search({ options }) {
|
|||||||
{suggestion.indexOf(query) === 0 ? suggestion.substring(query.length) : suggestion}
|
{suggestion.indexOf(query) === 0 ? suggestion.substring(query.length) : suggestion}
|
||||||
</span>
|
</span>
|
||||||
</div>
|
</div>
|
||||||
)}
|
);
|
||||||
|
}}
|
||||||
</Combobox.Option>
|
</Combobox.Option>
|
||||||
))}
|
))}
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user