Skip to content

Commit

Permalink
Allow returning values from the search component
Browse files Browse the repository at this point in the history
  • Loading branch information
jessarcher committed Jul 23, 2023
1 parent 0d18a05 commit 745bf8b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
4 changes: 3 additions & 1 deletion src/SearchPrompt.php
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}

/**
Expand Down
20 changes: 19 additions & 1 deletion tests/Feature/SearchPromptTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit 745bf8b

Please sign in to comment.