Skip to content

Commit

Permalink
Move to next/form (vercel#1369)
Browse files Browse the repository at this point in the history
  • Loading branch information
leerob authored Aug 13, 2024
1 parent 556aa77 commit 694c5c1
Show file tree
Hide file tree
Showing 6 changed files with 700 additions and 972 deletions.
10 changes: 10 additions & 0 deletions app/search/children-wrapper.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
'use client';

import { useSearchParams } from 'next/navigation';
import { Fragment } from 'react';

// Ensure children are re-rendered when the search query changes
export default function ChildrenWrapper({ children }: { children: React.ReactNode }) {
const searchParams = useSearchParams();
return <Fragment key={searchParams.get('q')}>{children}</Fragment>;
}
5 changes: 4 additions & 1 deletion app/search/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import Footer from 'components/layout/footer';
import Collections from 'components/layout/search/collections';
import FilterList from 'components/layout/search/filter';
import { sorting } from 'lib/constants';
import ChildrenWrapper from './children-wrapper';

export default function SearchLayout({ children }: { children: React.ReactNode }) {
return (
Expand All @@ -10,7 +11,9 @@ export default function SearchLayout({ children }: { children: React.ReactNode }
<div className="order-first w-full flex-none md:max-w-[125px]">
<Collections />
</div>
<div className="order-last min-h-screen w-full md:order-none">{children}</div>
<div className="order-last min-h-screen w-full md:order-none">
<ChildrenWrapper>{children}</ChildrenWrapper>
</div>
<div className="order-none flex-none md:order-last md:w-[125px]">
<FilterList list={sorting} title="Sort by" />
</div>
Expand Down
21 changes: 12 additions & 9 deletions app/search/loading.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@ import Grid from 'components/grid';

export default function Loading() {
return (
<Grid className="grid-cols-2 lg:grid-cols-3">
{Array(12)
.fill(0)
.map((_, index) => {
return (
<Grid.Item key={index} className="animate-pulse bg-neutral-100 dark:bg-neutral-800" />
);
})}
</Grid>
<>
<div className="mb-4 h-6" />
<Grid className="grid-cols-2 lg:grid-cols-3">
{Array(12)
.fill(0)
.map((_, index) => {
return (
<Grid.Item key={index} className="animate-pulse bg-neutral-100 dark:bg-neutral-800" />
);
})}
</Grid>
</>
);
}
27 changes: 5 additions & 22 deletions components/layout/navbar/search.tsx
Original file line number Diff line number Diff line change
@@ -1,35 +1,18 @@
'use client';

import { MagnifyingGlassIcon } from '@heroicons/react/24/outline';
import { createUrl } from 'lib/utils';
import { useRouter, useSearchParams } from 'next/navigation';
import Form from 'next/form';
import { useSearchParams } from 'next/navigation';

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="w-max-[550px] relative w-full lg:w-80 xl:w-full">
<Form action="/search" className="w-max-[550px] relative w-full lg:w-80 xl:w-full">
<input
key={searchParams?.get('q')}
type="text"
name="search"
name="q"
placeholder="Search for products..."
autoComplete="off"
defaultValue={searchParams?.get('q') || ''}
Expand All @@ -38,7 +21,7 @@ export default function Search() {
<div className="absolute right-0 top-0 mr-3 flex h-full items-center">
<MagnifyingGlassIcon className="h-4" />
</div>
</form>
</Form>
);
}

Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
"@heroicons/react": "^2.1.5",
"clsx": "^2.1.1",
"geist": "^1.3.1",
"next": "14.2.5",
"react": "18.3.1",
"react-dom": "18.3.1",
"next": "15.0.0-canary.113",
"react": "19.0.0-rc-3208e73e-20240730",
"react-dom": "19.0.0-rc-3208e73e-20240730",
"sonner": "^1.5.0"
},
"devDependencies": {
Expand Down
Loading

0 comments on commit 694c5c1

Please sign in to comment.