Skip to content

Commit

Permalink
Fix multisearch list handling (laravel#132)
Browse files Browse the repository at this point in the history
  • Loading branch information
jessarcher authored Apr 12, 2024
1 parent 4a1c974 commit e6f70bc
Show file tree
Hide file tree
Showing 3 changed files with 175 additions and 32 deletions.
38 changes: 30 additions & 8 deletions src/MultiSearchPrompt.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ class MultiSearchPrompt extends Prompt
*/
protected ?array $matches = null;

/**
* Whether the matches are initially a list.
*/
protected bool $isList;

/**
* The selected values.
*
Expand Down Expand Up @@ -96,16 +101,25 @@ public function matches(): array
return $this->matches;
}

if (strlen($this->typedValue) === 0) {
$matches = ($this->options)($this->typedValue);
$matches = ($this->options)($this->typedValue);

if (! isset($this->isList) && count($matches) > 0) {
// This needs to be captured the first time we receive matches so
// we know what we're dealing with later if matches is empty.
$this->isList = array_is_list($matches);
}

if (! isset($this->isList)) {
return $this->matches = [];
}

return $this->matches = [
...array_diff($this->values, $matches),
...$matches,
];
if (strlen($this->typedValue) > 0) {
return $this->matches = $matches;
}

return $this->matches = ($this->options)($this->typedValue);
return $this->matches = $this->isList
? [...array_diff(array_values($this->values), $matches), ...$matches]
: array_diff($this->values, $matches) + $matches;
}

/**
Expand All @@ -123,7 +137,7 @@ public function visible(): array
*/
protected function toggleHighlighted(): void
{
if (array_is_list($this->matches)) {
if ($this->isList()) {
$label = $this->matches[$this->highlighted];
$key = $label;
} else {
Expand Down Expand Up @@ -165,4 +179,12 @@ public function labels(): array
{
return array_values($this->values);
}

/**
* Whether the matches are initially a list.
*/
public function isList(): bool
{
return $this->isList;
}
}
4 changes: 2 additions & 2 deletions src/Themes/Default/MultiSearchPromptRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ protected function renderOptions(MultiSearchPrompt $prompt): string
->map(function ($label, $key) use ($prompt) {
$index = array_search($key, array_keys($prompt->matches()));
$active = $index === $prompt->highlighted;
$selected = array_is_list($prompt->visible())
$selected = $prompt->isList()
? in_array($label, $prompt->value())
: in_array($key, $prompt->value());

Expand Down Expand Up @@ -156,7 +156,7 @@ protected function getInfoText(MultiSearchPrompt $prompt): string
$info = count($prompt->value()).' selected';

$hiddenCount = count($prompt->value()) - collect($prompt->matches())
->filter(fn ($label, $key) => in_array(array_is_list($prompt->matches()) ? $label : $key, $prompt->value()))
->filter(fn ($label, $key) => in_array($prompt->isList() ? $label : $key, $prompt->value()))
->count();

if ($hiddenCount > 0) {
Expand Down
Loading

0 comments on commit e6f70bc

Please sign in to comment.