From 909a82c4cd2e259a59785ad5f705bde4ee0c046f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8F=B6=E6=9E=AB?= <7971419+crazyair@users.noreply.github.com> Date: Mon, 18 Mar 2024 16:45:31 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BC=98=E5=8C=96=E6=90=9C=E7=B4=A2?= =?UTF-8?q?=E4=BA=A4=E4=BA=92=20(#76)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit https://github.com/cnpm/cnpmweb/assets/7971419/7b98f7e3-78a9-42e2-a8c7-9dd5f7d15376 --- package.json | 2 +- src/components/LandingSearch.tsx | 16 +++++---- src/components/PackageCard.tsx | 58 ++++++++++++++++++++++++-------- src/components/SearchBox.tsx | 43 ++++++++++++++--------- 4 files changed, 81 insertions(+), 38 deletions(-) diff --git a/package.json b/package.json index cb2bbdb..61e6691 100644 --- a/package.json +++ b/package.json @@ -16,7 +16,7 @@ "@monaco-editor/react": "^4.4.2", "@types/react-dom": "18.2.5", "@vercel/node": "^2.15.5", - "antd": "^5.6.4", + "antd": "^5.15.3", "antd-style": "^3.4.4", "chart.js": "^4.4.1", "dayjs": "^1.11.9", diff --git a/src/components/LandingSearch.tsx b/src/components/LandingSearch.tsx index dc8cbac..c4428d7 100644 --- a/src/components/LandingSearch.tsx +++ b/src/components/LandingSearch.tsx @@ -1,10 +1,9 @@ 'use client'; import React, { useLayoutEffect, useState } from 'react'; -import { Input, Typography, AutoComplete, Space } from 'antd'; +import { Input, Typography, AutoComplete } from 'antd'; import { useCachedSearch } from '@/hooks/useSearch'; import { useRouter } from 'next/router'; -import Link from 'next/link'; import { PackageTag } from './PackageCard'; import { useRecent } from '@/hooks/useRecent'; @@ -29,13 +28,13 @@ export default function LandingSearch() { return searchResult.objects.map((object) => ({ label: ( - + <> {object.package.name}@{object.package.version}
{object.package.description} - + ), value: object.package.name, })); @@ -49,15 +48,18 @@ export default function LandingSearch() { options={options} autoFocus onChange={setSearch} - onSelect={(search) => router.push(`/package/${search}`)} + onSelect={(search) => router.push(`/package/${search}`, undefined, { shallow: true })} > { - e?.stopPropagation(); - router.push(`/packages?q=${search}`); + // 点击搜索按钮才进搜索页面 + if (e?.type === 'click') { + e?.stopPropagation(); + router.push(`/packages?q=${search}`); + } }} loading={!!(search && isLoading)} /> diff --git a/src/components/PackageCard.tsx b/src/components/PackageCard.tsx index 3fd2d71..7b53762 100644 --- a/src/components/PackageCard.tsx +++ b/src/components/PackageCard.tsx @@ -15,7 +15,15 @@ dayjs.locale('zh-cn'); dayjs.extend(relativeTime); -export function PackageTag({ tags, closeIcon, onClose }: { tags: {label: string, href: string}[], closeIcon?: boolean, onClose?: (tag: string) => void }) { +export function PackageTag({ + tags, + closeIcon, + onClose, +}: { + tags: { label: string; href: string }[]; + closeIcon?: boolean; + onClose?: (tag: string) => void; +}) { if (!tags) { return null; } @@ -26,14 +34,21 @@ export function PackageTag({ tags, closeIcon, onClose }: { tags: {label: string, className={styles.tagCon} maxCount="responsive" data={tags} - renderItem={tag => ( - onClose?.(tag.label)}> - - {tag.label} - + renderItem={(tag) => ( + onClose?.(tag.label)} + > + {tag.label} + + )} + renderRest={() => ( + + ... )} - renderRest={() => ...} /> ); } @@ -45,11 +60,15 @@ export const PackageCard = ({ }: { package: SearchPackageResult; loading?: boolean; - themeMode: ThemeMode, + themeMode: ThemeMode; }) => { return ( - +
{/* Logo */}
@@ -60,7 +79,12 @@ export const PackageCard = ({
- + {pkg.name}@{pkg.version} @@ -94,10 +118,16 @@ export const PackageCard = ({ > - ({ - label: item, - href: `/packages?q=${item}`, - })): []} /> + ({ + label: item, + href: `/packages?q=${item}`, + })) + : [] + } + /> diff --git a/src/components/SearchBox.tsx b/src/components/SearchBox.tsx index fc633a8..c4affbd 100644 --- a/src/components/SearchBox.tsx +++ b/src/components/SearchBox.tsx @@ -1,9 +1,8 @@ import { SearchOutlined } from '@ant-design/icons'; import { Col, Input, Row, Space, Tabs } from 'antd'; -import React from 'react'; +import React, { useEffect, useState } from 'react'; import styles from './SearchBox.module.css'; import { SearchResult } from '@/hooks/useSearch'; -import { useRouter } from 'next/router'; export interface SearchBoxProps { defaultSearch: string; @@ -12,6 +11,11 @@ export interface SearchBoxProps { } export default function SearchBox({ defaultSearch, onSearch, searchResult }: SearchBoxProps) { + const [value, setValue] = useState(defaultSearch); + useEffect(() => { + setValue(defaultSearch); + }, [defaultSearch]); + return (
@@ -28,26 +32,33 @@ export default function SearchBox({ defaultSearch, onSearch, searchResult }: Sea } size="large" - defaultValue={defaultSearch} + value={value} + onChange={(e) => setValue(e.target.value)} onSearch={onSearch} />
- - - 搜索结果 - - {((searchResult?.total as number) > 9999 ? '9999+' : searchResult?.total) ?? '-'} - - - } - /> - + + 搜索结果 + + {((searchResult?.total as number) > 9999 ? '9999+' : searchResult?.total) ?? + '-'} + + + ), + }, + ]} + />
);