All files / components/common SelectWithSearch.vue

100% Statements 2/2
100% Branches 0/0
100% Functions 0/0
100% Lines 2/2

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                                                                                      2x                                                                                                 2x                                                                                                                                
<template>
  <div class="position-relative">
    <div class="d-flex flex-column align-items-center">
      <BPagination
        v-model="page"
        aria-controls="my-table"
        class="m-0 mb-3"
        :class="page < 2 && total < currentPerPage && 'd-none'"
        :total-rows="total"
        :per-page="currentPerPage"
      ></BPagination>
      <div class="custom-select" :tabindex="tabindex">
        <div class="text-ellipsis selected" @click="open = !open">
          <input
            type="text"
            v-model.trim="text"
            :placeholder="placeholder || 'Search...'"
            class="p-0"
            v-on:input="$emit('setSearchText', text)"
          />
        </div>
      </div>
    </div>
    <ul
      class="block items p-1 position-absolute w-100"
      v-if="data.length"
      :class="open ? 'd-flex flex-column align-self-end' : 'd-none'"
    >
      <vueCustomScrollbar
        class="scroll w-100 overflow-auto"
        :settings="scrollSettings"
      >
        <li
          v-for="(option, i) of data"
          :key="i"
          v-on:click="clickOnItem(option)"
        >
          <slot name="item" :item="option"></slot>
        </li>
      </vueCustomScrollbar>
    </ul>
  </div>
</template>
 
<script>
export default {
  name: "SelectWithSearch",
  props: {
    data: Array,
    tabindex: {
      type: Number,
      required: false,
      default: 0,
    },
    placeholder: {
      type: String,
    },
    currentPerPage: {
      type: Number,
      required: true,
      default: 10,
    },
    total: {
      type: Number,
      required: true,
      default: 0,
    },
  },
  data: () => ({
    selected: null,
    open: false,
    text: "",
    scrollSettings: {
      suppressScrollX: true,
      wheelPropagation: false,
    },
    page: 1,
  }),
  watch: {
    data: function () {
      this.selected = this.data.length > 0 ? this.data[0] : null;
    },
    page: function () {
      this.$emit("setPage", this.page);
    },
  },
  methods: {
    clickOnItem: function (item) {
      this.$emit("clickItem", item);
      this.open = false;
    },
  },
};
</script>
 
<style scoped>
input {
  border: none;
  outline: none;
}
 
.scroll {
  max-height: 40vh;
}
 
.scroll > li {
  display: flex;
  z-index: 1;
}
 
.custom-select {
  position: relative;
  width: 100%;
}
 
.custom-select .selected {
  border-radius: 6px;
  cursor: pointer;
  user-select: none;
}
 
.custom-select .selected:after {
  position: absolute;
  content: "";
  top: 22px;
  right: 1em;
  width: 0;
  height: 0;
  border: 5px solid transparent;
  border-color: #fff transparent transparent transparent;
}
 
.items {
  color: #000;
  border-radius: 0px 0px 6px 6px;
  overflow: hidden;
  border-right: 1px solid #ad8225;
  border-left: 1px solid #ad8225;
  border-bottom: 1px solid #ad8225;
  position: absolute;
  background-color: #fff;
  left: 0;
  right: 0;
  top: calc(100% + 2px);
  z-index: 1;
}
 
.custom-select .items div {
  color: #000;
  cursor: pointer;
  user-select: none;
}
 
li:hover {
  background-color: #ad8225;
}
</style>