-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathSearch.tsx
90 lines (89 loc) · 2.2 KB
/
Search.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import { useState, Fragment } from 'react';
import Input from '@uiw/react-input';
import Button from '@uiw/react-button';
import { useDispatch, useSelector } from 'react-redux';
import { Link, useNavigate } from 'react-router-dom';
import { Dispatch, RootState } from '../models';
import styles from './Search.module.less';
export default function Search() {
const { package: pkg, pkgname } = useSelector(({ global }: RootState) => ({
package: global.package,
pkgname: global.pkgname,
}));
const dispatch = useDispatch<Dispatch>();
const [value, setValue] = useState<string | undefined>(pkgname);
const navigate = useNavigate();
const [links] = useState<{ to: string; label: string }[]>([
{
to: '/pkg/uiw',
label: 'uiw',
},
{
to: '/pkg/react',
label: 'react',
},
{
to: '/pkg/vue',
label: 'vue',
},
{
to: '/pkg/react@18',
label: 'react@18',
},
{
to: '/pkg/[email protected]',
label: '[email protected]',
},
]);
const name = pkg && pkg.name ? `${pkg.name}@${pkg.version}` : pkgname;
const gotoPreview = () => {
if (value) {
dispatch.global.update({ showSearch: false });
navigate(`/pkg/${value}`);
}
}
return (
<Fragment>
<Input
className={styles.input}
size="large"
defaultValue={name}
preIcon="search"
onChange={(e) => {
setValue(e.target.value);
}}
onKeyUp={(e) => {
if (e.key === "Enter") {
gotoPreview();
}
}}
addonAfter={
<Button
onClick={gotoPreview}
icon="arrow-right"
size="small"
basic
type="light"
/>
}
placeholder="package or package@version"
/>
<div className={styles.egLink}>
<span>E.g.</span>
{links.map((item, index) => {
return (
<Link
key={index}
to={item.to}
onClick={(e) => {
dispatch.global.update({ showSearch: false });
}}
>
{item.label}
</Link>
);
})}
</div>
</Fragment>
);
}