Skip to content

Commit d91c222

Browse files
committed
Suggestions: Do not rely on content to detect exact matches
Extending classes might adjust content beforehand and then the first `li` won't necessarily include a `button`. fixes #265
1 parent 847036a commit d91c222

File tree

1 file changed

+7
-10
lines changed

1 file changed

+7
-10
lines changed

src/Control/SearchBar/Suggestions.php

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
use ipl\Html\BaseHtmlElement;
99
use ipl\Html\FormattedString;
1010
use ipl\Html\FormElement\ButtonElement;
11-
use ipl\Html\FormElement\InputElement;
1211
use ipl\Html\HtmlElement;
1312
use ipl\Html\Text;
1413
use 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

Comments
 (0)