|
1 | 1 | <template> |
2 | | - <div class="flex"> |
3 | | - <input |
4 | | - type="text" |
5 | | - :class="getInputClass()" |
6 | | - :readonly="isDisabled()" |
7 | | - v-model="command" |
8 | | - v-on:keyup.enter="install()" |
9 | | - placeholder="Type in vendor/package OR composer require vendor/package" |
10 | | - /> |
11 | | - <button |
12 | | - type="submit" |
13 | | - :class="getButtonClass()" |
14 | | - :disabled="isDisabled()" |
15 | | - v-text="getButtonText()" |
16 | | - v-on:click="install()" |
17 | | - ></button> |
| 2 | + <div class="relative"> |
| 3 | + <div class="flex"> |
| 4 | + <input |
| 5 | + type="text" |
| 6 | + :class="getInputClass()" |
| 7 | + :readonly="isDisabled()" |
| 8 | + v-model="command" |
| 9 | + v-on:keyup.enter="install()" |
| 10 | + placeholder="Type in vendor/package OR composer require vendor/package" |
| 11 | + /> |
| 12 | + <button |
| 13 | + type="submit" |
| 14 | + :class="getButtonClass()" |
| 15 | + :disabled="isDisabled()" |
| 16 | + v-text="getButtonText()" |
| 17 | + v-on:click="install()" |
| 18 | + ></button> |
| 19 | + </div> |
| 20 | + <div class="border absolute w-full" v-if="getSuggestions().length > 0"> |
| 21 | + <div v-for="suggestion in getSuggestions()" class="bg-white hover:bg-gray-200 p-2 cursor-pointer" v-on:click="suggestionSelect(suggestion)"> |
| 22 | + <div v-text="suggestion.name"></div> |
| 23 | + <div v-text="suggestion.description" class="text-xs text-gray-600"></div> |
| 24 | + </div> |
| 25 | + </div> |
18 | 26 | </div> |
19 | 27 | </template> |
20 | 28 |
|
21 | 29 | <script> |
| 30 | + import debounce from 'lodash/debounce'; |
| 31 | +
|
22 | 32 | export default { |
23 | 33 | data() { |
24 | 34 | return { |
25 | 35 | buttonText: 'Install', |
26 | 36 | command: '', |
| 37 | + selectedSuggestion: null, |
27 | 38 | requirement: { |
28 | 39 | name: '', |
29 | 40 | version: null, |
|
34 | 45 | }; |
35 | 46 | }, |
36 | 47 |
|
| 48 | + watch: { |
| 49 | + command(value, oldValue) { |
| 50 | + if (this.selectedSuggestion !== null) { |
| 51 | + this.selectedSuggestion = null; |
| 52 | + return; |
| 53 | + } |
| 54 | +
|
| 55 | + if (oldValue.trim() !== value.trim()) { |
| 56 | + this.debouncedSearchSuggestions(value); |
| 57 | + } |
| 58 | + }, |
| 59 | + }, |
| 60 | +
|
| 61 | + created() { |
| 62 | + this.debouncedSearchSuggestions = debounce(this.searchSuggestions, 500); |
| 63 | + }, |
| 64 | +
|
37 | 65 | methods: { |
38 | 66 | async install() { |
39 | 67 | this.sanitizeInput(); |
|
60 | 88 | this.forgetInput(); |
61 | 89 | }, |
62 | 90 |
|
| 91 | + async searchSuggestions(query) { |
| 92 | + if (query === '') { |
| 93 | + await this.$store.dispatch('clearRequirementSuggestions'); |
| 94 | + return; |
| 95 | + } |
| 96 | +
|
| 97 | + // Ignore search if fully qualified console command |
| 98 | + if (query.includes('composer require')) { |
| 99 | + return; |
| 100 | + } |
| 101 | +
|
| 102 | + // Ignore search if version already specified |
| 103 | + if (query.includes(':')) { |
| 104 | + return; |
| 105 | + } |
| 106 | +
|
| 107 | + // Ignore search if adding flags like --dev |
| 108 | + if (query.includes('--')) { |
| 109 | + return; |
| 110 | + } |
| 111 | +
|
| 112 | + await this.$store.dispatch('collectRequirementSuggestions', { |
| 113 | + query: query, |
| 114 | + }); |
| 115 | + }, |
| 116 | +
|
| 117 | + suggestionSelect(suggestion) { |
| 118 | + this.selectedSuggestion = suggestion; |
| 119 | + this.command = suggestion.name; |
| 120 | + this.$store.dispatch('clearRequirementSuggestions'); |
| 121 | + }, |
| 122 | +
|
| 123 | + getSuggestions() { |
| 124 | + // Need to avoid bug with quick full delete command with holding backspace button |
| 125 | + if (this.command === '') { |
| 126 | + return []; |
| 127 | + } |
| 128 | +
|
| 129 | + return this.$store.state.requirementSuggestions; |
| 130 | + }, |
| 131 | +
|
63 | 132 | isDisabled() { |
64 | 133 | return this.$store.state.isInstallerLocked |
65 | 134 | || this.$store.getters.getActiveJobs().length > 0; |
|
0 commit comments