Skip to content

Commit

Permalink
search
Browse files Browse the repository at this point in the history
  • Loading branch information
Myllaume committed Oct 18, 2021
1 parent 3d4edec commit 9099630
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 2 deletions.
9 changes: 9 additions & 0 deletions includes/fuse.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion includes/search.njk
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<div class="font-sans text-black bg-white flex items-center justify-center" style="height: 100px;">
<div class="border rounded overflow-hidden flex">
<input type="text" class="px-4 py-2" style="width: 300px;" placeholder="Rechercher une personnalité politique">
<input id="search-input" type="text" class="px-4 py-2" style="width: 300px;" placeholder="Rechercher une personnalité politique">
<button class="flex items-center justify-center px-4 border-l">
<svg class="h-4 w-4 text-grey-dark" fill="currentColor" xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 24 24">
Expand Down
42 changes: 41 additions & 1 deletion layouts/home.njk
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ description: >
<ul class="page-list grid gap-4 grid-cols-2">
{% for post in collections.personnes %}
{% if post.data.published !== false %}
<a href="{{ post.url }}" class=" ">
<a href="{{ post.url }}" class=" "
data-people="{{ post.data.title }}">



Expand Down Expand Up @@ -58,6 +59,45 @@ description: >

</div>

<script>{% include "fuse.js" %}</script>

<script>
const searchInput = document.getElementById('search-input');
let resultList = [];
searchInput.value = '';
const elts = document.querySelectorAll('[data-people]');
const searchData = Array.from(elts)
.map((elt) => {
return {
elt: elt,
title: elt.dataset.people
}
});
const fuse = new Fuse(searchData, {
includeScore: false,
keys: ['title'] // search field from nodes metas
});
searchInput.addEventListener('input', () => {
resultList = fuse.search(searchInput.value);
if (resultList.length === 0) {
elts.forEach(elt => elt.style.display = null);
return;
}
elts.forEach(elt => elt.style.display = 'none');
for (const result of resultList) {
result.item.elt.style.display = null;
}
})
</script>

</body>

</html>

0 comments on commit 9099630

Please sign in to comment.