|
1 | 1 | <template>
|
2 |
| - <div id="myApplication"> |
3 |
| - <p> |
4 |
| - THIS WORKS |
5 |
| - <a href="https://code.visualstudio.com/" target="_blank">VSCode</a> |
6 |
| - + |
7 |
| - <a href="https://github.com/johnsoncodehk/volar" target="_blank">Volar</a> |
8 |
| - </p> |
9 |
| - |
10 |
| - <p> |
11 |
| - <a href="https://vitejs.dev/guide/features.html" target="_blank"> |
12 |
| - Vite Documentation |
13 |
| - </a> |
14 |
| - | |
15 |
| - <a href="https://v3.vuejs.org/" target="_blank">Vue 3 Documentation</a> |
16 |
| - </p> |
| 2 | + <div class="relative"> |
| 3 | + <input |
| 4 | + :value="value" |
| 5 | + @input="handleInput" |
| 6 | + :placeholder="placeholder" |
| 7 | + ref="input" |
| 8 | + tabindex="0" |
| 9 | + :class="inputClass" |
| 10 | + /> |
| 11 | + <span |
| 12 | + v-if="value" |
| 13 | + @click.prevent="reset()" |
| 14 | + class="absolute inset-y-0 right-0 pr-3 flex items-center cursor-pointer" |
| 15 | + > |
| 16 | + x |
| 17 | + </span> |
| 18 | + <div |
| 19 | + v-show="value && showOptions" |
| 20 | + @click.self="handleSelf()" |
| 21 | + @focusout="showOptions = false" |
| 22 | + tabindex="0" |
| 23 | + :class="dropdownClass" |
| 24 | + > |
| 25 | + <ul class="py-1"> |
| 26 | + <li |
| 27 | + v-for="searchResult in searchResults" |
| 28 | + :key="searchResult.OPEID" |
| 29 | + @click="handleClick(searchResult)" |
| 30 | + class="px-3 py-2 cursor-pointer hover:bg-gray-200" |
| 31 | + > |
| 32 | + {{ searchResult.college_name }} |
| 33 | + </li> |
| 34 | + <li v-if="!searchResults.length" class="px-3 py-2 text-center"> |
| 35 | + We search when you stop typing |
| 36 | + </li> |
| 37 | + </ul> |
| 38 | + </div> |
17 | 39 | </div>
|
18 | 40 | </template>
|
19 | 41 |
|
20 |
| -<script setup> |
| 42 | +<script> |
| 43 | +export default { |
| 44 | + props: { |
| 45 | + value: { |
| 46 | + type: String, |
| 47 | + required: false, |
| 48 | + }, |
| 49 | + placeholder: { |
| 50 | + type: String, |
| 51 | + required: false, |
| 52 | + default: "Enter text here.", |
| 53 | + }, |
| 54 | + data: { |
| 55 | + type: Array, |
| 56 | + required: true, |
| 57 | + }, |
| 58 | + inputClass: { |
| 59 | + type: String, |
| 60 | + required: false, |
| 61 | + default: |
| 62 | + "px-3 py-3 placeholder-blueGray-300 text-blueGray-600 relative bg-white bg-white rounded text-sm border-0 shadow outline-none focus:outline-none focus:ring w-full", |
| 63 | + }, |
| 64 | + dropdownClass: { |
| 65 | + type: String, |
| 66 | + required: false, |
| 67 | + default: |
| 68 | + "absolute w-full z-50 bg-white border border-gray-300 mt-1 mh-48 overflow-hidden overflow-y-scroll rounded-md shadow-md", |
| 69 | + }, |
| 70 | + }, |
| 71 | + data() { |
| 72 | + return { |
| 73 | + showOptions: false, |
| 74 | + chosenOption: "", |
| 75 | + searchTerm: "", |
| 76 | + }; |
| 77 | + }, |
| 78 | + computed: { |
| 79 | + searchResults() { |
| 80 | + if (this.data) { |
| 81 | + return this.data.slice(0, 10).filter((item) => { |
| 82 | + return item |
| 83 | + }); |
| 84 | + } |
| 85 | + }, |
| 86 | + }, |
| 87 | + methods: { |
| 88 | + reset() { |
| 89 | + this.$emit("input", ""); |
| 90 | + this.chosenOption = ""; |
| 91 | + }, |
| 92 | + handleInput(evt) { |
| 93 | + this.$emit("input", evt.target.value); |
| 94 | + this.searchTerm = evt.target.value; |
| 95 | + this.showOptions = true; |
| 96 | + }, |
| 97 | + handleClick(item) { |
| 98 | + this.$emit("input", item.college_name); |
| 99 | + this.$emit("chosen", item); |
| 100 | + this.chosenOption = item.college_name; |
| 101 | + this.showOptions = false; |
| 102 | + this.$refs.input.focus(); |
| 103 | + } |
| 104 | + }, |
| 105 | +}; |
21 | 106 | </script>
|
0 commit comments