88use ipl \Html \BaseHtmlElement ;
99use ipl \Html \FormattedString ;
1010use ipl \Html \FormElement \ButtonElement ;
11- use ipl \Html \FormElement \InputElement ;
1211use ipl \Html \HtmlElement ;
1312use ipl \Html \Text ;
1413use ipl \Stdlib \Contract \Paginatable ;
@@ -242,11 +241,17 @@ protected function assemble()
242241 $ data = new LimitIterator (new IteratorIterator ($ this ->data ), 0 , self ::DEFAULT_LIMIT );
243242 }
244243
244+ $ noDefault = null ;
245245 foreach ($ data as $ term => $ meta ) {
246246 if (is_int ($ term )) {
247247 $ term = $ meta ;
248248 }
249249
250+ if ($ this ->searchTerm ) {
251+ // Only the first exact match will set this to true, any other to false
252+ $ noDefault = $ noDefault === null && $ term === $ this ->searchTerm ;
253+ }
254+
250255 $ attributes = [
251256 'type ' => 'button ' ,
252257 'tabindex ' => -1 ,
@@ -290,14 +295,6 @@ protected function assemble()
290295 $ this ->getAttributes ()->add ('class ' , 'has-more ' );
291296 }
292297
293- $ showDefault = true ;
294- if ($ this ->searchTerm && $ this ->count () === 1 ) {
295- // The default option is only shown if the user's input does not result in an exact match
296- $ input = $ this ->getFirst ('li ' )->getFirst ('button ' );
297- $ showDefault = $ input ->getContent () != $ this ->searchTerm
298- && $ input ->getAttributes ()->get ('data-search ' )->getValue () != $ this ->searchTerm ;
299- }
300-
301298 if ($ this ->type === 'column ' && ! $ this ->isEmpty () && ! $ this ->getFirst ('li ' )->getAttributes ()->has ('class ' )) {
302299 // The column title is only added if there are any suggestions and the first item is not a title already
303300 $ this ->prependHtml (new HtmlElement (
@@ -307,7 +304,7 @@ protected function assemble()
307304 ));
308305 }
309306
310- if ($ showDefault ) {
307+ if (! $ noDefault ) {
311308 $ this ->assembleDefault ();
312309 }
313310
0 commit comments