Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 40 additions & 19 deletions src/components/search/InstantSearch.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,18 @@ function InfiniteHits(props: UseInfiniteHitsProps) {
function FilterDropdown({
attribute,
label,
limit = 1000,
}: {
attribute: string;
label: string;
limit?: number;
}) {
const [isOpen, setIsOpen] = useState(false);
const { items, refine } = useRefinementList({ attribute });
const { items, refine } = useRefinementList({
attribute,
limit,
sortBy: ["count:desc"],
});

useEffect(() => {
const params = new URLSearchParams(window.location.search);
Expand All @@ -112,12 +118,19 @@ function FilterDropdown({
const refined = items
.filter((item) => item.isRefined)
.map((item) => item.value);
if (refined.length === 0) return;

const params = new URLSearchParams(window.location.search);

if (refined.length === 0) {
params.delete(attribute);
} else {
params.set(attribute, refined.join(","));
}

history.pushState(
null,
"",
`${window.location.pathname}?${attribute}=${refined.join(",")}`,
`${window.location.pathname}?${params.toString()}`,
);
}, [items]);

Expand Down Expand Up @@ -160,21 +173,28 @@ function FilterDropdown({
className="border-cl1-gray-8 bg-cl1-white dark:border-cl1-gray-1 dark:bg-cl1-gray-0 rounded-sm border p-4 shadow-md"
>
<div className="max-h-60 space-y-2 overflow-y-auto">
{items.map((item) => (
<label
key={item.value}
className="flex items-center gap-2 text-sm"
>
<input
type="checkbox"
checked={item.isRefined}
onChange={() => refine(item.value)}
/>
<span>
{item.label} ({item.count})
</span>
</label>
))}
{items
.sort((a, b) => {
if (a.isRefined && !b.isRefined) return -1;
if (!a.isRefined && b.isRefined) return 1;
return b.count - a.count;
})
.map((item) => (
<label
key={item.value}
className="flex items-center gap-2 text-sm"
>
<input
type="checkbox"
className="bg-transparent"
checked={item.isRefined}
onChange={() => refine(item.value)}
/>
<span>
{item.label} ({item.count})
</span>
</label>
))}
</div>
</div>
</FloatingPortal>
Expand All @@ -198,8 +218,9 @@ export default function InstantSearchComponent() {
<Configure filters="type:content" />
<div className="space-y-4">
<SearchBox />
<div className="flex gap-2">
<div className="not-content flex gap-2">
<FilterDropdown attribute="product" label="Products" />
<FilterDropdown attribute="tags" label="Tags" />
</div>
<InfiniteHits />
</div>
Expand Down
22 changes: 12 additions & 10 deletions src/styles/input.css
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
input,
textarea,
select {
background-color: var(--sl-color-bg-nav);
border-color: var(--sl-color-gray-5);
border-width: 2px;
}
@layer theme {
input,
textarea,
select {
background-color: var(--sl-color-bg-nav);
border-color: var(--sl-color-gray-5);
border-width: 2px;
}

input[readonly] {
background-color: var(--sl-color-backdrop-overlay);
cursor: not-allowed;
input[readonly] {
background-color: var(--sl-color-backdrop-overlay);
cursor: not-allowed;
}
}