Skip to content

Commit 6a5196f

Browse files
committed
[Docs Site] Add tags filter to /search/
1 parent c6f7e0d commit 6a5196f

File tree

2 files changed

+52
-29
lines changed

2 files changed

+52
-29
lines changed

src/components/search/InstantSearch.tsx

Lines changed: 40 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -90,12 +90,18 @@ function InfiniteHits(props: UseInfiniteHitsProps) {
9090
function FilterDropdown({
9191
attribute,
9292
label,
93+
limit = 1000,
9394
}: {
9495
attribute: string;
9596
label: string;
97+
limit?: number;
9698
}) {
9799
const [isOpen, setIsOpen] = useState(false);
98-
const { items, refine } = useRefinementList({ attribute });
100+
const { items, refine } = useRefinementList({
101+
attribute,
102+
limit,
103+
sortBy: ["count:desc"],
104+
});
99105

100106
useEffect(() => {
101107
const params = new URLSearchParams(window.location.search);
@@ -112,12 +118,19 @@ function FilterDropdown({
112118
const refined = items
113119
.filter((item) => item.isRefined)
114120
.map((item) => item.value);
115-
if (refined.length === 0) return;
121+
122+
const params = new URLSearchParams(window.location.search);
123+
124+
if (refined.length === 0) {
125+
params.delete(attribute);
126+
} else {
127+
params.set(attribute, refined.join(","));
128+
}
116129

117130
history.pushState(
118131
null,
119132
"",
120-
`${window.location.pathname}?${attribute}=${refined.join(",")}`,
133+
`${window.location.pathname}?${params.toString()}`,
121134
);
122135
}, [items]);
123136

@@ -160,21 +173,28 @@ function FilterDropdown({
160173
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"
161174
>
162175
<div className="max-h-60 space-y-2 overflow-y-auto">
163-
{items.map((item) => (
164-
<label
165-
key={item.value}
166-
className="flex items-center gap-2 text-sm"
167-
>
168-
<input
169-
type="checkbox"
170-
checked={item.isRefined}
171-
onChange={() => refine(item.value)}
172-
/>
173-
<span>
174-
{item.label} ({item.count})
175-
</span>
176-
</label>
177-
))}
176+
{items
177+
.sort((a, b) => {
178+
if (a.isRefined && !b.isRefined) return -1;
179+
if (!a.isRefined && b.isRefined) return 1;
180+
return b.count - a.count;
181+
})
182+
.map((item) => (
183+
<label
184+
key={item.value}
185+
className="flex items-center gap-2 text-sm"
186+
>
187+
<input
188+
type="checkbox"
189+
className="bg-transparent"
190+
checked={item.isRefined}
191+
onChange={() => refine(item.value)}
192+
/>
193+
<span>
194+
{item.label} ({item.count})
195+
</span>
196+
</label>
197+
))}
178198
</div>
179199
</div>
180200
</FloatingPortal>
@@ -198,8 +218,9 @@ export default function InstantSearchComponent() {
198218
<Configure filters="type:content" />
199219
<div className="space-y-4">
200220
<SearchBox />
201-
<div className="flex gap-2">
221+
<div className="not-content flex gap-2">
202222
<FilterDropdown attribute="product" label="Products" />
223+
<FilterDropdown attribute="tags" label="Tags" />
203224
</div>
204225
<InfiniteHits />
205226
</div>

src/styles/input.css

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1-
input,
2-
textarea,
3-
select {
4-
background-color: var(--sl-color-bg-nav);
5-
border-color: var(--sl-color-gray-5);
6-
border-width: 2px;
7-
}
1+
@layer theme {
2+
input,
3+
textarea,
4+
select {
5+
background-color: var(--sl-color-bg-nav);
6+
border-color: var(--sl-color-gray-5);
7+
border-width: 2px;
8+
}
89

9-
input[readonly] {
10-
background-color: var(--sl-color-backdrop-overlay);
11-
cursor: not-allowed;
10+
input[readonly] {
11+
background-color: var(--sl-color-backdrop-overlay);
12+
cursor: not-allowed;
13+
}
1214
}

0 commit comments

Comments
 (0)