Skip to content

Commit

Permalink
Slot for no results added
Browse files Browse the repository at this point in the history
  • Loading branch information
rparnpuu committed Aug 21, 2023
1 parent 670bde2 commit 21be956
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
16 changes: 13 additions & 3 deletions src/components/InfiniteLoading.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<script lang="ts" setup>
import type { Props, Params, State, StateHandler } from "@root/types";
import { onMounted, ref, toRefs, onUnmounted, watch, nextTick } from "vue";
import { computed, onMounted, ref, toRefs, onUnmounted, watch, nextTick } from "vue";
import { startObserver, getParentEl, isVisible } from "@root/utils";
// @ts-ignore
import Spinner from "./Spinner.vue";
Expand Down Expand Up @@ -43,6 +43,7 @@ const stateHandler: StateHandler = {
state.value = "loading";
},
async loaded() {
params.firstload = false;
state.value = "loaded";
const parentEl = params.parentEl || document.documentElement;
await nextTick();
Expand Down Expand Up @@ -71,6 +72,12 @@ onMounted(async () => {
onUnmounted(() => {
observer?.disconnect();
});
const showNoMore = computed(() => state.value == "complete" && !params.firstload);
const showNoResults = computed(() => state.value == "complete" && params.firstload);
const showError = computed(() => state.value === "error");
</script>

<template>
Expand All @@ -80,10 +87,13 @@ onUnmounted(() => {
<Spinner />
</slot>
</div>
<slot v-if="state == 'complete'" name="complete">
<slot v-if="showNoMore" name="complete">
<span> {{ slots?.complete || "No more results!" }} </span>
</slot>
<slot v-if="state == 'error'" name="error" :retry="params.emit">
<slot v-if="showNoResults" name="noResults">
<span> {{ slots?.noResults || "No data found!" }} </span>
</slot>
<slot v-if="showError" name="error" :retry="params.emit">
<span class="state-error">
<span>{{ slots?.error || "Oops something went wrong!" }}</span>
<button class="retry" @click="params.emit">retry</button>
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export interface Params {

export interface Slots {
complete?: string;
noResults?: string;
error?: string;
}

Expand Down

0 comments on commit 21be956

Please sign in to comment.