Files
.github
.vscode
app
components
breadcrumb
cart
filters
grid
icons
layout
navbar
products-list
search
filters
sorting-menu
index.tsx
item.tsx
header.tsx
placeholder.tsx
footer-menu.tsx
footer.tsx
page
product
profile
banner.tsx
carousel.tsx
checkbox.tsx
hero-icon.tsx
hero.tsx
home-page-content.tsx
label.tsx
loading-dots.tsx
logo-square.tsx
opengraph-image.tsx
price.tsx
prose.tsx
side-dialog.tsx
tooltip.tsx
fonts
hooks
lib
public
.env.example
.eslintrc.js
.gitignore
.nvmrc
.prettierignore
README.md
license.md
next.config.js
package.json
pnpm-lock.yaml
postcss.config.js
prettier.config.js
tailwind.config.js
tsconfig.json
commerce/components/layout/search/sorting-menu/item.tsx
2024-05-07 14:40:18 +07:00

37 lines
1019 B
TypeScript

import clsx from 'clsx';
import { SortFilterItem } from 'lib/constants';
import { createUrl } from 'lib/utils';
import Link from 'next/link';
import { usePathname, useSearchParams } from 'next/navigation';
const SortingItem = ({ item }: { item: SortFilterItem }) => {
const pathname = usePathname();
const searchParams = useSearchParams();
const active = searchParams.get('sort') === item.slug;
const newSearchParams = new URLSearchParams(searchParams);
if (item.slug && item.slug.length) {
newSearchParams.set('sort', item.slug);
} else {
newSearchParams.delete('sort');
}
const href = createUrl(pathname, newSearchParams);
const DynamicTag = active ? 'p' : Link;
return (
<DynamicTag
prefetch={!active ? false : undefined}
href={href}
className={clsx('block bg-transparent px-4 py-2 text-sm', {
'font-medium text-gray-900': active,
'text-gray-500': !active
})}
>
{item.title}
</DynamicTag>
);
};
export default SortingItem;