mirror of
https://github.com/danny-avila/LibreChat.git
synced 2025-12-23 20:00:15 +01:00
* feat: Add SearchBar component to Nav This commit adds the SearchBar component to the navigation bar in order to enable search functionality. Now users can easily search for specific items within the navigation. * Refactor Nav and SearchBar components The commit refactors the Nav component by moving the SearchBar component within the Nav component. This change ensures that the SearchBar is rendered only when the isSearchEnabled condition is true. In addition, the commit also modifies the styling of the SearchBar component by adding rounded corners and border to enhance the visual appearance. * Update gitignore * C Refactor search bar styles This commit refactors the styles of the search bar component in the Nav component. The border color and hover background color have been modified to improve the visual appearance. * Fix margin * Rename Logout.jsx to Logout.tsx and update import statements accordingly. Replace the use of Recoil and store with useLocalize hook for localization. Update the usage of localize function by removing the lang parameter.
65 lines
1.9 KiB
JavaScript
65 lines
1.9 KiB
JavaScript
import { forwardRef, useState, useEffect } from 'react';
|
|
import { Search, X } from 'lucide-react';
|
|
import { useRecoilState, useRecoilValue } from 'recoil';
|
|
import store from '~/store';
|
|
import { localize } from '~/localization/Translation';
|
|
|
|
const SearchBar = forwardRef((props, ref) => {
|
|
const { clearSearch } = props;
|
|
const [searchQuery, setSearchQuery] = useRecoilState(store.searchQuery);
|
|
const [showClearIcon, setShowClearIcon] = useState(false);
|
|
const lang = useRecoilValue(store.lang);
|
|
|
|
const handleKeyUp = (e) => {
|
|
const { value } = e.target;
|
|
if (e.keyCode === 8 && value === '') {
|
|
setSearchQuery('');
|
|
clearSearch();
|
|
}
|
|
};
|
|
|
|
const onChange = (e) => {
|
|
const { value } = e.target;
|
|
setSearchQuery(value);
|
|
setShowClearIcon(value.length > 0);
|
|
};
|
|
|
|
useEffect(() => {
|
|
if (searchQuery.length === 0) {
|
|
setShowClearIcon(false);
|
|
} else {
|
|
setShowClearIcon(true);
|
|
}
|
|
}, [searchQuery]);
|
|
|
|
return (
|
|
<div
|
|
ref={ref}
|
|
className="relative flex w-full cursor-pointer items-center gap-3 rounded-md border border-white/20 px-3 py-3 text-sm text-white transition-colors duration-200 hover:bg-gray-500/10"
|
|
>
|
|
{<Search className="absolute left-3 h-4 w-4" />}
|
|
<input
|
|
type="text"
|
|
className="m-0 mr-0 w-full border-none bg-transparent p-0 pl-7 text-sm leading-tight outline-none"
|
|
value={searchQuery}
|
|
onChange={onChange}
|
|
onKeyDown={(e) => {
|
|
e.code === 'Space' ? e.stopPropagation() : null;
|
|
}}
|
|
placeholder={localize(lang, 'com_nav_search_placeholder')}
|
|
onKeyUp={handleKeyUp}
|
|
/>
|
|
<X
|
|
className={`absolute right-3 h-5 w-5 cursor-pointer ${
|
|
showClearIcon ? 'opacity-100' : 'opacity-0'
|
|
} transition-opacity duration-1000`}
|
|
onClick={() => {
|
|
setSearchQuery('');
|
|
clearSearch();
|
|
}}
|
|
/>
|
|
</div>
|
|
);
|
|
});
|
|
|
|
export default SearchBar;
|