All files / lib SmartSuggest.vue

100% Statements 161/161
100% Branches 12/12
100% Functions 3/3
100% Lines 161/161

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 1611x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 22x 1x 1x 1x 61x 61x 1x 1x 1x 1x 61x 60x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 30x 20x 30x 10x 10x 30x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x
<script lang="ts">
/**
 * Component responsible for handling the input and displaying the dropdown.
 * Accepts a list of triggers which are used to determine when to open the dropdown.
 * It should have a single child element which is the input element (@see AcceptedInputType for supported types).
 * 
 * Based on your need, you can customize the dropdown items (SmartSuggestItem) or the full dropdown (slot="dropdown").
 * 
 * When using showNoResult=true, you can also customize the no result message (slot="no-result").
 */
export default {};
</script>
 
<script setup lang="ts">
import { onMounted, onUnmounted, onUpdated, ref, watch } from 'vue';
import { AcceptedInputType, ActiveTrigger, Item, Trigger } from './types';
import { useSmartSuggest } from './useSmartSuggest';
import SmartSuggestItem from './SmartSuggestItem.vue';
 
const props = defineProps<{
    /**
     * (prop) Triggers to use for this SmartSuggest instance
     */
    triggers: Trigger[];
}>();
 
const { setInputElement, setTriggers, select, active, items, dropdownPosition, activeIndex, activeTrigger } = useSmartSuggest(props.triggers);
 
watch(() => props.triggers, () => {
    setTriggers(props.triggers);
}, { immediate: true });
 
const container = ref<HTMLDivElement>();
function updateTextArea() {
    if (! container.value || ! container.value.firstElementChild) {
        setInputElement(undefined);
        return;
    }
 
    setInputElement(container.value.firstElementChild as AcceptedInputType);
}
onMounted(updateTextArea);
onUpdated(updateTextArea);
onUnmounted(updateTextArea);
 
const emits = defineEmits<{
    /**
     * (event) Emitted when an item is selected
     */
    (e: 'select', item: Item): void;
 
    /**
     * (event) Emitted when the dropdown is opened or closed
     */
    (e: 'open'): void;
 
    /**
     * (event) Emitted when the dropdown is opened or closed
     */
    (e: 'close'): void;
}>();
 
watch(active, (active) => {
    if (active) {
        emits('open');
    } else {
        emits('close');
    }
});
</script>
 
<template>
    <div
        ref="container"
        class="smart-suggest"
        v-bind="$attrs"
        style="position: relative;"
    >
        <slot />
 
        <slot
            v-if="active"
            name="dropdown"
            :position="dropdownPosition"
            :items="items"
            :active-index="activeIndex"
            :trigger="(activeTrigger as ActiveTrigger).trigger"
            :select="select"
        >
            <div
                class="smart-suggest-dropdown"
                :class="{
                    'smart-suggest-dropdown-top': dropdownPosition.toTop,
                }"
                style="position: absolute"
                :style="{
                    top: dropdownPosition.top + 'px',
                    left: dropdownPosition.left + 'px',
                    width: dropdownPosition.width + 'px',
                    height: dropdownPosition.height + 'px',
                }"
            >
                <div class="smart-suggest-items">
                    <template
                        v-for="item, index in items"
                        :key="item.value"
                    >
                        <slot
                            name="item"
                            :item="item"
                            :active="index === activeIndex"
                            :trigger="(activeTrigger as ActiveTrigger).trigger"
                            :select="select"
                        >
                            <SmartSuggestItem
                                :item="item"
                                :active="index === activeIndex"
                                @select="select(item)"
                            />
                        </slot>
                    </template>
 
                    <template v-if="items.length === 0">
                        <slot name="no-result">
                            <div class="smart-suggest-no-result">
                                No result
                            </div>
                        </slot>
                    </template>
                </div>
            </div>
        </slot>
    </div>
</template>
 
<style scoped>
.smart-suggest-dropdown {
    display: flex;
    flex-direction: column;
    justify-content: flex-start;
    color: black;
}
.smart-suggest-dropdown.smart-suggest-dropdown-top {
    display: flex;
    flex-direction: column;
    justify-content: flex-end;
}
 
.smart-suggest-items {
    background-color: white;
    border: 1px solid #ccc;
    border-radius: 5px;
    overflow: auto;
    z-index: 100;
    text-align: left;
}
 
.smart-suggest-no-result {
    padding: 5px;
}
</style>