Skip to content

Commit 2d2a7d5

Browse files
authored
feat(theme-doc): clear search history (#108)
1 parent 0445271 commit 2d2a7d5

File tree

2 files changed

+33
-3
lines changed

2 files changed

+33
-3
lines changed

packages/theme-doc/src/Layout/Search/index.tsx

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
SearchOutlined,
55
NumberOutlined,
66
ProfileOutlined,
7+
ClearOutlined,
78
} from '@ant-design/icons'
89
import { useNavigate } from 'react-router-dom'
910
import { useAllPagesOutlines } from 'vite-plugin-react-pages/client'
@@ -69,8 +70,20 @@ const renderSearchResultItem = (
6970
throw new Error('unexpected SearchResultItem: type ' + type)
7071
}
7172

72-
const calcRecentSearchesOptions = (recentSearches: SearchResultItem[]) => {
73-
const label = <p>Recent</p>
73+
const calcRecentSearchesOptions = (
74+
recentSearches: SearchResultItem[],
75+
clearAllHistory: () => void
76+
) => {
77+
const len = recentSearches.length
78+
79+
const label = (
80+
<p className={s.recentSearchesLabel}>
81+
<div className={s.recentSearchesLabelText}>
82+
{len ? 'Recent' : 'No recent searches'}
83+
</div>
84+
{len ? <ClearOutlined onClick={clearAllHistory} /> : null}
85+
</p>
86+
)
7487

7588
const options = recentSearches.map((item) => {
7689
const { type, page, matechedString } = item
@@ -110,8 +123,13 @@ const Search: React.FC<React.PropsWithChildren<Props>> = (props) => {
110123

111124
const allPagesOutlines = useAllPagesOutlines(2000)?.allPagesOutlines
112125

126+
const clearAllHistory = () => {
127+
setRecentSearches([])
128+
localStorage.setItem(recentSearchesKey, JSON.stringify([]))
129+
}
130+
113131
const recentSearchesOptions = useMemo(
114-
() => calcRecentSearchesOptions(recentSearches),
132+
() => calcRecentSearchesOptions(recentSearches, clearAllHistory),
115133
[recentSearches]
116134
)
117135

packages/theme-doc/src/Layout/Search/search.module.less

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,15 @@
5959
.searchResultMatchedText {
6060
font-size: 18px;
6161
}
62+
63+
.recentSearchesLabel {
64+
color: #1677ff;
65+
display: flex;
66+
flex-direction: row;
67+
justify-content: space-between;
68+
}
69+
70+
.recentSearchesLabelText {
71+
font-size: 18px;
72+
font-weight: 500;
73+
}

0 commit comments

Comments
 (0)