Skip to content

Commit

Permalink
feat(#197): add search component to sidebar
Browse files Browse the repository at this point in the history
  • Loading branch information
Decipher committed May 18, 2022
1 parent 85823a3 commit 1514c7f
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 0 deletions.
93 changes: 93 additions & 0 deletions docs/components/app/Search.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<template>
<ul class="mb-5 menu">
<li slot="title" class="menu-title">
<span>Search</span>
</li>
<li>
<div class="form-control">
<input
type="text"
placeholder=""
class="input input-bordered"
v-model="model"
/>
</div>
</li>
<template v-if="isResults">
<!-- Guide pages -->
<template v-if="results.guide.length">
<li v-for="item of results.guide" :key="item.id">
<NuxtLink class="rounded-btn" :to="item.path">
<AppIconGuide class="inline-block w-5 h-5 mr-2 stroke-current text-gray-400" />
{{ item.title }}
</NuxtLink>
</li>
</template>

<!-- Module pages -->
<template v-if="results.modules.length">
<li v-for="item of results.modules" :key="item.path">
<NuxtLink class="rounded-btn" to="/">
<AppIconModules class="inline-block w-5 h-5 mr-2 stroke-current text-gray-400" />
{{ item.title }}
</NuxtLink>
</li>
</template>

<!-- API pages -->
<template v-if="results.api.length">
<li v-for="item of results.api" :key="item.id">
<NuxtLink class="rounded-btn" :to="item.path">
<AppIconApi class="inline-block w-5 h-5 mr-2 stroke-current text-gray-400" />
{{ item.title }}
</NuxtLink>
</li>
</template>
</template>

<!-- No results -->
<li v-else-if="model && !isResults">
<span class="rounded-btn">No results</span>
</li>
</ul>
</template>

<script>
export default {
data: () => ({
model: '',
results: {
api: [],
guide: [],
modules: [],
},
}),
computed: {
isResults: ({ results }) => results.api.length || results.guide.length || results.modules.length
},
watch: {
async model() {
// Empty results if model is empty.
if (!this.model) {
this.results = {
api: [],
guide: [],
modules: [],
}
return
}
// Search all available data.
for (const type of ['api', 'guide', 'modules']) {
const results = await this.$content(type)
.search(this.model)
.limit(3)
.fetch()
this.results[type] = results
}
}
}
}
</script>
3 changes: 3 additions & 0 deletions docs/components/app/Sidebar.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
<template>
<div class="px-1 pt-20 overflow-y-auto w-80 bg-base-100">
<!-- Search -->
<AppSearch class="mb-5" />

<!-- Recent documents -->
<ul v-if="$store.state.recent.length" class="mb-5 menu">
<li slot="title" class="menu-title">
Expand Down

0 comments on commit 1514c7f

Please sign in to comment.