93 lines
2.5 KiB
React
Raw Normal View History

2022-09-01 19:21:44 +02:00
import { useState } from "react";
2022-09-08 11:48:16 +03:00
import { useTranslation } from "react-i18next";
import { FiSearch } from "react-icons/fi";
2022-09-03 13:12:09 +03:00
import { SiDuckduckgo, SiMicrosoftbing, SiGoogle } from "react-icons/si";
2022-09-03 13:12:09 +03:00
const providers = {
google: {
name: "Google",
url: "https://www.google.com/search?q=",
icon: SiGoogle,
},
duckduckgo: {
name: "DuckDuckGo",
url: "https://duckduckgo.com/?q=",
icon: SiDuckduckgo,
},
bing: {
name: "Bing",
url: "https://www.bing.com/search?q=",
icon: SiMicrosoftbing,
},
2022-09-26 23:02:59 +08:00
baidu: {
name: "Baidu",
url: "https://www.baidu.com/s?wd=",
icon: SiBaidu,
},
2022-09-03 13:12:09 +03:00
custom: {
name: "Custom",
url: false,
icon: FiSearch,
},
};
2022-09-03 13:12:09 +03:00
export default function Search({ options }) {
2022-09-08 11:48:16 +03:00
const { t } = useTranslation();
2022-09-03 13:12:09 +03:00
const provider = providers[options.provider];
const [query, setQuery] = useState("");
2022-09-03 13:12:09 +03:00
if (!provider) {
2022-09-07 16:53:24 +03:00
return null;
2022-09-03 13:12:09 +03:00
}
function handleSubmit(event) {
const q = encodeURIComponent(query);
if (provider.url) {
window.open(`${provider.url}${q}`, options.target || "_blank");
} else {
2022-09-03 13:12:09 +03:00
window.open(`${options.url}${q}`, options.target || "_blank");
}
2022-09-03 13:12:09 +03:00
event.preventDefault();
event.target.reset();
setQuery("");
}
return (
2022-09-12 11:00:15 +03:00
<form className="flex-col relative h-8 my-4 min-w-full md:min-w-fit grow first:ml-0 ml-4" onSubmit={handleSubmit}>
2022-09-09 06:45:43 +03:00
<div className="flex absolute inset-y-0 left-0 items-center pl-3 pointer-events-none w-full text-theme-800 dark:text-white" />
<input
2022-09-09 06:45:43 +03:00
type="text"
className="
overflow-hidden w-full h-full rounded-md
text-xs text-theme-900 dark:text-white
placeholder-theme-900 dark:placeholder-white/80
bg-white/50 dark:bg-white/10
focus:ring-theme-500 dark:focus:ring-white/50
focus:border-theme-500 dark:focus:border-white/50
border border-theme-300 dark:border-theme-200/50"
2022-09-08 11:48:16 +03:00
placeholder={t("search.placeholder")}
onChange={(s) => setQuery(s.currentTarget.value)}
required
2022-09-09 06:45:43 +03:00
autoCapitalize="off"
autoCorrect="off"
autoComplete="off"
2022-09-15 05:28:40 +03:00
// eslint-disable-next-line jsx-a11y/no-autofocus
autoFocus={options.focus}
/>
<button
type="submit"
2022-09-09 06:45:43 +03:00
className="
absolute right-0.5 bottom-0.5 rounded-r-md px-4 py-2 border-1
text-white font-medium text-sm
bg-theme-600/40 dark:bg-white/10
focus:ring-theme-500 dark:focus:ring-white/50"
>
2022-09-09 06:45:43 +03:00
<provider.icon className="text-white w-3 h-3" />
</button>
</form>
);
}