feat: auto trim discover in search input
Signed-off-by: Innei <i@innei.in>
This commit is contained in:
@@ -101,17 +101,23 @@ export function DiscoverForm({ type }: { type: string }) {
|
||||
|
||||
const keyword = form.watch("keyword")
|
||||
useEffect(() => {
|
||||
if (!prefix) return
|
||||
const isValidPrefix = prefix.find((p) => keyword.startsWith(p))
|
||||
const trimmedKeyword = keyword.trim()
|
||||
if (!prefix) {
|
||||
form.setValue("keyword", trimmedKeyword)
|
||||
return
|
||||
}
|
||||
const isValidPrefix = prefix.find((p) => trimmedKeyword.startsWith(p))
|
||||
if (!isValidPrefix) {
|
||||
form.setValue("keyword", prefix[0])
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
if (keyword.startsWith(`${isValidPrefix}${isValidPrefix}`)) {
|
||||
form.setValue("keyword", keyword.slice(isValidPrefix.length))
|
||||
if (trimmedKeyword.startsWith(`${isValidPrefix}${isValidPrefix}`)) {
|
||||
form.setValue("keyword", trimmedKeyword.slice(isValidPrefix.length))
|
||||
}
|
||||
|
||||
form.setValue("keyword", trimmedKeyword)
|
||||
}, [form, keyword, prefix])
|
||||
|
||||
return (
|
||||
|
||||
@@ -73,11 +73,9 @@ export function Component() {
|
||||
</TabsList>
|
||||
{tabs.map((tab) => (
|
||||
<TabsContent key={tab.name} value={tab.value} className="mt-8">
|
||||
{TabComponent[tab.value] ? (
|
||||
createElement(TabComponent[tab.value])
|
||||
) : (
|
||||
<DiscoverForm type={tab.value} />
|
||||
)}
|
||||
{createElement(TabComponent[tab.value] || TabComponent.default, {
|
||||
type: tab.value,
|
||||
})}
|
||||
</TabsContent>
|
||||
))}
|
||||
</Tabs>
|
||||
@@ -86,8 +84,9 @@ export function Component() {
|
||||
)
|
||||
}
|
||||
|
||||
const TabComponent = {
|
||||
const TabComponent: Record<string, React.FC<{ type: string }>> = {
|
||||
import: DiscoverImport,
|
||||
rss3: DiscoverRSS3,
|
||||
user: DiscoverUser,
|
||||
default: DiscoverForm,
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user