77 :attribute =" {{ json_encode ($attribute ) } }"
88 :validations =" '{{ $validations } } '"
99 :value =" {{ json_encode ($lookUpEntityData ) } }"
10+ can-add-new =" {{ $canAddNew ?? false } }"
1011 >
1112 <div class =" relative inline-block w-full" >
1213 <!-- Input Container -->
2829 type =" text/x-template"
2930 id =" v-lookup-component-template"
3031 >
31- < div class = " relative" >
32+ < div
33+ class = " relative"
34+ ref= " lookup"
35+ >
3236 < div
3337 class = " relative inline-block w-full"
3438 @click = " toggle"
3539 >
3640 <!-- Input Container -->
3741 < 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" >
3842 <!-- 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>
4149 <!-- Icons Container -->
4250 < div class = " flex items-center gap-2" >
4351 <!-- Close Icon -->
4452 < i
45- v- if = " entityId && ! isSearching"
53+ v- if = " (selectedItem?.name) && ! isSearching"
4654 class = " icon-cross-large cursor-pointer text-2xl text-gray-600"
4755 @click = " remove"
4856 >< / i>
@@ -60,9 +68,10 @@ class="text-2xl text-gray-600"
6068 < input
6169 type= " hidden"
6270 : name= " attribute['code']"
63- v- model= " entityId "
71+ v- model= " selectedItem.id "
6472 / >
65-
73+
74+ <!-- Popup Box -->
6675 < div
6776 v- if = " showPopup"
6877 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"
94103
95104 <!-- Results List -->
96105 < 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+
98121 < 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"
101125 >
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>
105130 < / 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>
113131 < / ul>
114132 < / div>
115133 < / div>
@@ -119,7 +137,7 @@ class="px-4 py-2 text-center text-gray-500"
119137 app .component (' v-lookup-component' , {
120138 template: ' #v-lookup-component-template' ,
121139
122- props: [' validations' , ' attribute' , ' value' ],
140+ props: [' validations' , ' attribute' , ' value' , ' canAddNew ' ],
123141
124142 data () {
125143 return {
@@ -129,16 +147,25 @@ class="px-4 py-2 text-center text-gray-500"
129147
130148 searchedResults: [],
131149
132- selectedItem: null ,
133-
134- entityId: null ,
150+ selectedItem: {
151+ id: ' ' ,
152+ name: ' '
153+ },
135154
136155 searchRoute: ` {{ route (' admin.settings.attributes.lookup' ) } } /${ this .attribute .lookup_type } ` ,
137156
138157 isSearching: false ,
139158 };
140159 },
141160
161+ mounted () {
162+ if (this .value ) {
163+ this .getLookUpEntity ();
164+ }
165+
166+ window .addEventListener (' click' , this .handleFocusOut );
167+ },
168+
142169 watch: {
143170 searchTerm (newVal , oldVal ) {
144171 this .search ();
@@ -151,16 +178,17 @@ class="px-4 py-2 text-center text-gray-500"
151178 },
152179 },
153180
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+ );
157191 }
158-
159- window .addEventListener (' click' , this .handleFocusOut );
160- },
161-
162- beforeDestroy () {
163- window .removeEventListener (' click' , this .handleFocusOut );
164192 },
165193
166194 methods: {
@@ -176,6 +204,8 @@ class="px-4 py-2 text-center text-gray-500"
176204 if (this .searchTerm .length <= 2 ) {
177205 this .searchedResults = [];
178206
207+ this .isSearching = false ;
208+
179209 return ;
180210 }
181211
@@ -198,41 +228,37 @@ class="px-4 py-2 text-center text-gray-500"
198228 .then (response => {
199229 const [result ] = response .data ;
200230
201- this .entityId = result .id ;
202-
203- this .selectedItem = result .name ;
231+ this .selectedItem = result;
204232 })
205233 .catch (error => {});
206234 },
207235
208236 handleResult (result ) {
209- this .showPopup = ! this . showPopup ;
237+ this .showPopup = false ;
210238
211- this .entityId = result .id ;
212-
213- this .selectedItem = result .name ;
214-
215- this .searchTerm = " " ;
239+ this .selectedItem = result;
216240
217- this .searchedResults = [] ;
241+ this .searchTerm = ' ' ;
218242
219243 this .$emit (' lookup-added' , result);
220244 },
221245
222246 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+ ) {
224253 this .showPopup = false ;
225254 }
226255 },
227256
228257 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+ };
236262
237263 this .$emit (' lookup-removed' );
238264 },
0 commit comments