7
7
:attribute =" {{ json_encode ($attribute ) } }"
8
8
:validations =" '{{ $validations } } '"
9
9
:value =" {{ json_encode ($lookUpEntityData ) } }"
10
+ can-add-new =" {{ $canAddNew ?? false } }"
10
11
>
11
12
<div class =" relative inline-block w-full" >
12
13
<!-- Input Container -->
28
29
type =" text/x-template"
29
30
id =" v-lookup-component-template"
30
31
>
31
- < div class = " relative" >
32
+ < div
33
+ class = " relative"
34
+ ref= " lookup"
35
+ >
32
36
< div
33
37
class = " relative inline-block w-full"
34
38
@click = " toggle"
35
39
>
36
40
<!-- Input Container -->
37
41
< div class = " relative flex items-center justify-between rounded border border-gray-200 p-2 hover:border-gray-400 focus:border-gray-400 dark:border-gray-800 dark:text-gray-300" >
38
42
<!-- Selected Item or Placeholder Text -->
39
- @ {{ selectedItem ? selectedItem : " @lang (' admin::app.components.attributes.lookup.click-to-add' )" }}
40
-
43
+ < span
44
+ class = " overflow-hidden text-ellipsis"
45
+ : title= " selectedItem?.name"
46
+ >
47
+ @ {{ selectedItem? .name !== " " ? selectedItem? .name : " @lang (' admin::app.components.attributes.lookup.click-to-add' )" }}
48
+ < / span>
41
49
<!-- Icons Container -->
42
50
< div class = " flex items-center gap-2" >
43
51
<!-- Close Icon -->
44
52
< i
45
- v- if = " entityId && ! isSearching"
53
+ v- if = " (selectedItem?.name) && ! isSearching"
46
54
class = " icon-cross-large cursor-pointer text-2xl text-gray-600"
47
55
@click = " remove"
48
56
>< / i>
@@ -60,9 +68,10 @@ class="text-2xl text-gray-600"
60
68
< input
61
69
type= " hidden"
62
70
: name= " attribute['code']"
63
- v- model= " entityId "
71
+ v- model= " selectedItem.id "
64
72
/ >
65
-
73
+
74
+ <!-- Popup Box -->
66
75
< div
67
76
v- if = " showPopup"
68
77
class = " absolute top-full z-10 mt-1 flex w-full origin-top transform flex-col gap-2 rounded-lg border border-gray-200 bg-white p-2 shadow-lg transition-transform dark:border-gray-900 dark:bg-gray-800"
@@ -94,22 +103,31 @@ class="relative"
94
103
95
104
<!-- Results List -->
96
105
< ul class = " max-h-40 divide-y divide-gray-100 overflow-y-auto" >
97
- < template v- for = " result in searchedResults" >
106
+ < li
107
+ v- for = " item in filteredResults"
108
+ : key= " item.id"
109
+ class = " flex cursor-pointer gap-2 p-2 transition-colors hover:bg-blue-100 dark:text-gray-300 dark:hover:bg-gray-900"
110
+ @click = " handleResult(result)"
111
+ >
112
+ <!-- Entity Name -->
113
+ < span> @ {{ item .name }}< / span>
114
+ < / li>
115
+
116
+ < template v- if = " filteredResults.length === 0" >
117
+ < li class = " px-4 py-2 text-center text-gray-500" >
118
+ @lang (' admin::app.components.attributes.lookup.no-result-found' )
119
+ < / li>
120
+
98
121
< li
99
- class = " flex cursor-pointer gap-2 p-2 transition-colors hover:bg-blue-100 dark:text-gray-300 dark:hover:bg-gray-900"
100
- @click = " handleResult(result)"
122
+ v- if = " searchTerm.length > 2 && canAddNew"
123
+ @click = " handleResult({ id: '', name: searchTerm })"
124
+ class = " cursor-pointer border-t border-gray-800 px-4 py-2 text-gray-500 hover:bg-brandColor hover:text-white dark:border-gray-300 dark:text-gray-400 dark:hover:bg-gray-900 dark:hover:text-white"
101
125
>
102
- <!-- Entity Name -->
103
- < span> @ {{ result .name }}< / span>
104
- < / li>
126
+ < i class = " icon-add text-md" >< / i>
127
+
128
+ @lang (' admin::app.components.lookup.add-as-new' )
129
+ < / li>
105
130
< / template>
106
-
107
- < li
108
- v- if = " searchedResults.length === 0"
109
- class = " px-4 py-2 text-center text-gray-500"
110
- >
111
- @lang (' admin::app.components.attributes.lookup.no-result-found' )
112
- < / li>
113
131
< / ul>
114
132
< / div>
115
133
< / div>
@@ -119,7 +137,7 @@ class="px-4 py-2 text-center text-gray-500"
119
137
app .component (' v-lookup-component' , {
120
138
template: ' #v-lookup-component-template' ,
121
139
122
- props: [' validations' , ' attribute' , ' value' ],
140
+ props: [' validations' , ' attribute' , ' value' , ' canAddNew ' ],
123
141
124
142
data () {
125
143
return {
@@ -129,16 +147,25 @@ class="px-4 py-2 text-center text-gray-500"
129
147
130
148
searchedResults: [],
131
149
132
- selectedItem: null ,
133
-
134
- entityId: null ,
150
+ selectedItem: {
151
+ id: ' ' ,
152
+ name: ' '
153
+ },
135
154
136
155
searchRoute: ` {{ route (' admin.settings.attributes.lookup' ) } } /${ this .attribute .lookup_type } ` ,
137
156
138
157
isSearching: false ,
139
158
};
140
159
},
141
160
161
+ mounted () {
162
+ if (this .value ) {
163
+ this .getLookUpEntity ();
164
+ }
165
+
166
+ window .addEventListener (' click' , this .handleFocusOut );
167
+ },
168
+
142
169
watch: {
143
170
searchTerm (newVal , oldVal ) {
144
171
this .search ();
@@ -151,16 +178,17 @@ class="px-4 py-2 text-center text-gray-500"
151
178
},
152
179
},
153
180
154
- mounted () {
155
- if (this .value ) {
156
- this .getLookUpEntity ();
181
+ computed: {
182
+ /**
183
+ * Filter the searchedResults based on the search query.
184
+ *
185
+ * @return {Array}
186
+ */
187
+ filteredResults () {
188
+ return this .searchedResults .filter (item =>
189
+ item .name .toLowerCase ().includes (this .searchTerm .toLowerCase ())
190
+ );
157
191
}
158
-
159
- window .addEventListener (' click' , this .handleFocusOut );
160
- },
161
-
162
- beforeDestroy () {
163
- window .removeEventListener (' click' , this .handleFocusOut );
164
192
},
165
193
166
194
methods: {
@@ -176,6 +204,8 @@ class="px-4 py-2 text-center text-gray-500"
176
204
if (this .searchTerm .length <= 2 ) {
177
205
this .searchedResults = [];
178
206
207
+ this .isSearching = false ;
208
+
179
209
return ;
180
210
}
181
211
@@ -198,41 +228,37 @@ class="px-4 py-2 text-center text-gray-500"
198
228
.then (response => {
199
229
const [result ] = response .data ;
200
230
201
- this .entityId = result .id ;
202
-
203
- this .selectedItem = result .name ;
231
+ this .selectedItem = result;
204
232
})
205
233
.catch (error => {});
206
234
},
207
235
208
236
handleResult (result ) {
209
- this .showPopup = ! this . showPopup ;
237
+ this .showPopup = false ;
210
238
211
- this .entityId = result .id ;
212
-
213
- this .selectedItem = result .name ;
214
-
215
- this .searchTerm = " " ;
239
+ this .selectedItem = result;
216
240
217
- this .searchedResults = [] ;
241
+ this .searchTerm = ' ' ;
218
242
219
243
this .$emit (' lookup-added' , result);
220
244
},
221
245
222
246
handleFocusOut (e ) {
223
- if (! this .$el .contains (e .target )) {
247
+ const lookup = this .$refs .lookup ;
248
+
249
+ if (
250
+ lookup &&
251
+ ! lookup .contains (event .target )
252
+ ) {
224
253
this .showPopup = false ;
225
254
}
226
255
},
227
256
228
257
remove () {
229
- this .entityId = null ;
230
-
231
- this .selectedItem = null ;
232
-
233
- this .searchTerm = ' ' ;
234
-
235
- this .searchedResults = [];
258
+ this .selectedItem = {
259
+ id: ' ' ,
260
+ name: ' '
261
+ };
236
262
237
263
this .$emit (' lookup-removed' );
238
264
},
0 commit comments