Skip to content

Commit

Permalink
Fix return type for integer keys in associative arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
jessarcher committed Jul 12, 2023
1 parent c725c2d commit f9d63fd
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 6 deletions.
4 changes: 2 additions & 2 deletions src/MultiSelectPrompt.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class MultiSelectPrompt extends Prompt
* Create a new SelectPrompt instance.
*
* @param array<int|string, string> $options
* @param array<string> $default
* @param array<int|string> $default
*/
public function __construct(
public string $label,
Expand All @@ -46,7 +46,7 @@ public function __construct(
/**
* Get the selected values.
*
* @return array<string>
* @return array<int|string>
*/
public function value(): array
{
Expand Down
2 changes: 1 addition & 1 deletion src/SelectPrompt.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function __construct(
/**
* Get the selected value.
*/
public function value(): string
public function value(): int|string
{
if (array_is_list($this->options)) {
return $this->options[$this->highlighted] ?? null;
Expand Down
6 changes: 3 additions & 3 deletions src/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ function password(string $label, string $placeholder = '', bool|string $required
*
* @param array<int|string, string> $options
*/
function select(string $label, array $options, ?string $default = null, int $scroll = 5, ?Closure $validate = null): string
function select(string $label, array $options, int|string|null $default = null, int $scroll = 5, ?Closure $validate = null): int|string
{
return (new SelectPrompt($label, $options, $default, $scroll, $validate))->prompt();
}
Expand All @@ -34,8 +34,8 @@ function select(string $label, array $options, ?string $default = null, int $scr
* Prompt the user to select multiple options.
*
* @param array<int|string, string> $options
* @param array<string> $default
* @return array<string>
* @param array<int|string> $default
* @return array<int|string>
*/
function multiselect(string $label, array $options, array $default = [], int $scroll = 5, bool|string $required = false, ?Closure $validate = null): array
{
Expand Down
15 changes: 15 additions & 0 deletions tests/Feature/MultiselectPromptTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,21 @@
expect($result)->toBe(['green', 'blue']);
});

it('accepts an associate array with integer keys', function () {
Prompt::fake([Key::DOWN, Key::SPACE, Key::DOWN, Key::SPACE, Key::ENTER]);

$result = multiselect(
label: 'What are your favorite colors?',
options: [
1 => 'Red',
2 => 'Green',
3 => 'Blue',
]
);

expect($result)->toBe([2, 3]);
});

it('accepts default values when the options are labels', function () {
Prompt::fake([Key::ENTER]);

Expand Down
15 changes: 15 additions & 0 deletions tests/Feature/SelectPromptTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,21 @@
expect($result)->toBe('green');
});

it('accepts an associate array with integer keys', function () {
Prompt::fake([Key::DOWN, Key::ENTER]);

$result = select(
label: 'What is your favorite color?',
options: [
1 => 'Red',
2 => 'Green',
3 => 'Blue',
]
);

expect($result)->toBe(2);
});

it('accepts default values when the options are labels', function () {
Prompt::fake([Key::ENTER]);

Expand Down

0 comments on commit f9d63fd

Please sign in to comment.