Skip to content

Commit dc4adb5

Browse files
committed
feat(search): enhance search functionality with input handling and clear button
1 parent e8beb53 commit dc4adb5

File tree

1 file changed

+57
-25
lines changed

1 file changed

+57
-25
lines changed

src/layouts/search/search.tsx

+57-25
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,23 @@
1+
import { useRef, useState } from 'react'
12
import { CiSearch } from 'react-icons/ci'
3+
import { MdOutlineClear } from 'react-icons/md'
24
import Browser from 'webextension-polyfill'
35
import { BookmarkProvider } from '../../context/bookmark.context'
46
import { useTheme } from '../../context/theme.context'
57
import { BookmarksComponent } from './bookmarks/bookmarks'
68

79
export function SearchLayout() {
810
const { theme, themeUtils } = useTheme()
11+
const [searchQuery, setSearchQuery] = useState('')
12+
const searchRef = useRef<HTMLDivElement>(null)
13+
const inputRef = useRef<HTMLInputElement>(null)
914

1015
const getSearchBoxBackground = () => {
1116
switch (theme) {
1217
case 'light':
1318
return 'bg-white hover:bg-white/95'
1419
case 'dark':
15-
return 'bg-neutral-800 hover:bg-neutral-700/90'
20+
return 'bg-neutral-800 hover:bg-neutral-700/90'
1621
default:
1722
return 'bg-neutral-900/70 backdrop-blur-sm hover:bg-neutral-800/80'
1823
}
@@ -29,39 +34,66 @@ export function SearchLayout() {
2934
}
3035
}
3136

32-
const handleSubmit = (e: React.FormEvent<HTMLFormElement>) => {
37+
const handleSubmit = async (e: React.FormEvent<HTMLFormElement>) => {
3338
e.preventDefault()
34-
const query = (e.target as HTMLFormElement).search.value
35-
if (query.trim()) {
39+
const query = searchQuery.trim()
40+
if (query) {
3641
Browser.search.query({ text: query })
3742
}
3843
}
3944

45+
const handleSearchInputChange = (e: React.ChangeEvent<HTMLInputElement>) => {
46+
setSearchQuery(e.target.value)
47+
}
48+
49+
const handleClearSearch = () => {
50+
setSearchQuery('')
51+
if (inputRef.current) {
52+
inputRef.current.focus()
53+
}
54+
}
55+
4056
return (
4157
<>
4258
<div className="flex flex-col items-center justify-center max-h-80">
43-
<form className="w-full" onSubmit={handleSubmit}>
44-
<div
45-
className={`relative overflow-hidden transition-all duration-300 shadow-xl rounded-2xl group ${getSearchBoxBackground()}`}
46-
>
47-
<input
48-
type="text"
49-
name="search"
50-
className={`w-full py-4 pl-16 pr-6 text-lg font-light text-right bg-transparent focus:outline-none ${themeUtils.getTextColor()}`}
51-
placeholder="جستجو ..."
52-
autoComplete="off"
53-
/>
54-
<button
55-
type="submit"
56-
className={`absolute p-2 transition-all duration-300 -translate-y-1/2 rounded-lg cursor-pointer left-3 top-1/2 ${getSearchButtonStyles()}`}
57-
>
58-
<CiSearch size={20} />
59-
</button>
59+
<div className="relative w-full" ref={searchRef}>
60+
<form className="w-full" onSubmit={handleSubmit}>
6061
<div
61-
className={`absolute inset-0 transition-all duration-300 border pointer-events-none rounded-2xl ${themeUtils.getBorderColor()}`}
62-
/>
63-
</div>
64-
</form>
62+
className={`relative overflow-hidden transition-all duration-300 shadow-lg hover:shadow-xl rounded-xl group ${getSearchBoxBackground()}`}
63+
>
64+
<input
65+
ref={inputRef}
66+
type="text"
67+
name="search"
68+
value={searchQuery}
69+
onChange={handleSearchInputChange}
70+
className={`w-full py-4 pr-12 pl-16 text-lg font-medium text-right bg-transparent focus:outline-none ${themeUtils.getTextColor()}`}
71+
placeholder="جستجو ..."
72+
autoComplete="off"
73+
/>
74+
<button
75+
type="submit"
76+
className={`absolute p-3 transition-all duration-300 -translate-y-1/2 rounded-lg cursor-pointer left-2 top-1/2 ${getSearchButtonStyles()}`}
77+
>
78+
<CiSearch size={22} />
79+
</button>
80+
{searchQuery && (
81+
<button
82+
type="button"
83+
onClick={handleClearSearch}
84+
className={
85+
'absolute p-2 transition-all duration-200 -translate-y-1/2 rounded-full cursor-pointer right-3 top-1/2 text-gray-400 hover:text-gray-600 dark:text-gray-500 dark:hover:text-gray-300 hover:bg-gray-100 dark:hover:bg-gray-700'
86+
}
87+
>
88+
<MdOutlineClear size={16} />
89+
</button>
90+
)}
91+
<div
92+
className={`absolute inset-0 transition-all duration-300 border pointer-events-none rounded-xl ${themeUtils.getBorderColor()}`}
93+
/>
94+
</div>
95+
</form>
96+
</div>
6597
<BookmarkProvider>
6698
<BookmarksComponent />
6799
</BookmarkProvider>

0 commit comments

Comments
 (0)