Skip to content

Commit

Permalink
feat: add autoFocus (#74)
Browse files Browse the repository at this point in the history
  • Loading branch information
crazyair authored Feb 29, 2024
1 parent 2b91ed1 commit c678c21
Showing 1 changed file with 26 additions and 19 deletions.
45 changes: 26 additions & 19 deletions src/components/LandingSearch.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use client';

import React, { useState } from 'react';
import React, { useLayoutEffect, useState } from 'react';
import { Input, Typography, AutoComplete, Space } from 'antd';
import { useCachedSearch } from '@/hooks/useSearch';
import { useRouter } from 'next/router';
Expand All @@ -12,6 +12,10 @@ export default function LandingSearch() {
const [search, setSearch] = useState('');
const [recent, setRecent] = useRecent();
const router = useRouter();
const [load, setLoad] = useState(false);
useLayoutEffect(() => {
setLoad(true);
}, []);

const { data: searchResult, isLoading } = useCachedSearch({
keyword: search,
Expand Down Expand Up @@ -39,27 +43,30 @@ export default function LandingSearch() {

return (
<>
<AutoComplete
style={{ width: '100%' }}
options={options}
onChange={setSearch}
onSelect={(search) => router.push(`/package/${search}`)}
>
<Input.Search
size="large"
placeholder="输入 NPM 包名、作者、关键字等信息即可搜索..."
enterButton
onSearch={(_, e) => {
e?.stopPropagation();
router.push(`/packages?q=${search}`);
}}
loading={!!(search && isLoading)}
/>
</AutoComplete>
{load && (
<AutoComplete
style={{ width: '100%' }}
options={options}
autoFocus
onChange={setSearch}
onSelect={(search) => router.push(`/package/${search}`)}
>
<Input.Search
size="large"
placeholder="输入 NPM 包名、作者、关键字等信息即可搜索..."
enterButton
onSearch={(_, e) => {
e?.stopPropagation();
router.push(`/packages?q=${search}`);
}}
loading={!!(search && isLoading)}
/>
</AutoComplete>
)}
<div style={{ marginTop: 16 }}>
{recent && (
<PackageTag
tags={recent.map(item => ({
tags={recent.map((item) => ({
label: item,
href: `/${item}`,
}))}
Expand Down

0 comments on commit c678c21

Please sign in to comment.