Skip to content

Commit

Permalink
fix: Close search form after submitting (#23)
Browse files Browse the repository at this point in the history
* fix: Improve search experience (#18)

* fix: Improve search experience

* Search on icon click

* Add margin top to header when items in bag

* Use cn

* fix: Close search form after submitting

* Make onSubmit optional
  • Loading branch information
soniaklimas authored and karolkarolka committed Oct 25, 2024
1 parent 26aa9e6 commit ec6d36d
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
6 changes: 5 additions & 1 deletion apps/storefront/src/components/header/mobile-search.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ export const MobileSearch = () => {
const pathname = usePathname();
const [isOpen, setIsOpen] = useState(false);

const handleCloseSheet = () => {
setIsOpen(false);
};

useEffect(() => {
setIsOpen(false);
}, [pathname]);
Expand All @@ -38,7 +42,7 @@ export const MobileSearch = () => {
modal={true}
>
<SheetContent side="top" closeClassName="right-2 top-2">
<SearchForm />
<SearchForm onSubmit={handleCloseSheet} />
</SheetContent>
</Sheet>
</>
Expand Down
6 changes: 5 additions & 1 deletion apps/storefront/src/components/header/search-form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ const initialSearchState: SearchState = {
showOptions: false,
};

export const SearchForm = () => {
export const SearchForm = ({ onSubmit }: { onSubmit?: () => void }) => {
const ts = useTranslations("search");
const tc = useTranslations("common");

Expand Down Expand Up @@ -85,6 +85,9 @@ export const SearchForm = () => {
if (event.code === keyboardCodes.Enter) {
event.preventDefault();
resetSearchState();
if (onSubmit) {
onSubmit();
}

// Handle query search
if (isNoOptionHighlighted || isLastOptionHighlighted) {
Expand Down Expand Up @@ -130,6 +133,7 @@ export const SearchForm = () => {
const handleSearchIconClick = () => {
if (isNoOptionHighlighted || isLastOptionHighlighted) {
router.push(paths.search.asPath({ query: { q: inputValue } }));
resetSearchState();

return;
}
Expand Down

0 comments on commit ec6d36d

Please sign in to comment.