diff --git a/src/SearchPrompt.php b/src/SearchPrompt.php index 56889156..d5b59f70 100644 --- a/src/SearchPrompt.php +++ b/src/SearchPrompt.php @@ -126,7 +126,9 @@ public function value(): int|string|null return null; } - return array_keys($this->matches)[$this->highlighted]; + return array_is_list($this->matches) + ? $this->matches[$this->highlighted] + : array_keys($this->matches)[$this->highlighted]; } /** diff --git a/tests/Feature/SearchPromptTest.php b/tests/Feature/SearchPromptTest.php index 65123aa7..52f50a47 100644 --- a/tests/Feature/SearchPromptTest.php +++ b/tests/Feature/SearchPromptTest.php @@ -23,7 +23,25 @@ expect($result)->toBe('blue'); }); -it('can return integer keys', function () { +it('returns the value when a list is passed', function () { + Prompt::fake(['u', 'e', Key::DOWN, Key::ENTER]); + + $result = search( + label: 'What is your favorite color?', + options: fn (string $value) => array_values(array_filter( + [ + 'Red', + 'Green', + 'Blue', + ], + fn ($option) => str_contains(strtolower($option), strtolower($value)), + )), + ); + + expect($result)->toBe('Blue'); +}); + +it('returns the key when an associative array is passed', function () { Prompt::fake(['u', 'e', Key::DOWN, Key::ENTER]); $result = search(