Skip to content

Commit

Permalink
Fix type error in suggest with collection
Browse files Browse the repository at this point in the history
  • Loading branch information
macocci7 committed Apr 14, 2024
1 parent e6f70bc commit f3038ce
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/SuggestPrompt.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@ public function matches(): array
}

if ($this->options instanceof Closure) {
return $this->matches = array_values(($this->options)($this->value()));
$matches = ($this->options)($this->value());
return $this->matches = array_values($matches instanceof Collection ? $matches->toArray() : $matches);

Check failure on line 95 in src/SuggestPrompt.php

View workflow job for this annotation

GitHub Actions / tests / Static Analysis

Instanceof between array<string> and Illuminate\Support\Collection will always evaluate to false.
}

return $this->matches = array_values(array_filter($this->options, function ($option) {
Expand Down
20 changes: 20 additions & 0 deletions tests/Feature/SuggestPromptTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,26 @@
expect($result)->toBe('Blue');
});

it('accepts a callback returning a collection', function () {
Prompt::fake(['b', Key::TAB, Key::ENTER]);

$result = suggest(
label: 'What is your favorite color?',
options: fn ($value) => collect([
'Red',
'Green',
'Blue',
])->filter(
fn ($name) => str_contains(
strtoupper($name),
strtoupper($value)
)
)
);

expect($result)->toBe('Blue');
});

it('validates', function () {
Prompt::fake([Key::ENTER, 'X', Key::ENTER]);

Expand Down

0 comments on commit f3038ce

Please sign in to comment.