1
0
mirror of https://github.com/vercel/commerce.git synced 2025-07-22 20:26:49 +00:00
Files
.github
.husky
.vscode
app
components
cart
collection
grid
icons
layout
navbar
index.tsx
mobile-menu.tsx
search.tsx
search
footer.tsx
product-grid-items.tsx
product
carousel.tsx
loading-dots.tsx
opengraph-image.tsx
price.tsx
prose.tsx
e2e
fonts
lib
.env.example
.eslintrc.js
.gitignore
.nvmrc
.prettierignore
README.md
license.md
next.config.js
package.json
playwright.config.ts
pnpm-lock.yaml
postcss.config.js
prettier.config.js
tailwind.config.js
tsconfig.json
commerce/components/layout/navbar/search.tsx
2023-05-12 16:02:51 -07:00

47 lines
1.3 KiB
TypeScript

'use client';
import { useRouter, useSearchParams } from 'next/navigation';
import SearchIcon from 'components/icons/search';
import { createUrl } from 'lib/utils';
export default function Search() {
const router = useRouter();
const searchParams = useSearchParams();
function onSubmit(e: React.FormEvent<HTMLFormElement>) {
e.preventDefault();
const val = e.target as HTMLFormElement;
const search = val.search as HTMLInputElement;
const newParams = new URLSearchParams(searchParams.toString());
if (search.value) {
newParams.set('q', search.value);
} else {
newParams.delete('q');
}
router.push(createUrl('/search', newParams));
}
return (
<form
onSubmit={onSubmit}
className="relative m-0 flex w-full items-center border border-gray-200 bg-transparent p-0 dark:border-gray-500"
>
<input
type="text"
name="search"
placeholder="Search for products..."
autoComplete="off"
defaultValue={searchParams?.get('q') || ''}
className="w-full px-4 py-2 text-black dark:bg-black dark:text-gray-100"
/>
<div className="absolute right-0 top-0 mr-3 flex h-full items-center">
<SearchIcon className="h-5" />
</div>
</form>
);
}