Skip to content

Commit

Permalink
feat: auto trim discover in search input
Browse files Browse the repository at this point in the history
Signed-off-by: Innei <i@innei.in>
  • Loading branch information
Innei committed Sep 20, 2024
1 parent 4a07b2e commit 039dd3d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
14 changes: 10 additions & 4 deletions apps/renderer/src/modules/discover/form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand All @@ -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,
}

0 comments on commit 039dd3d

Please sign in to comment.