Skip to content

Commit

Permalink
Accept collections
Browse files Browse the repository at this point in the history
  • Loading branch information
jessarcher committed Jul 25, 2023
1 parent 7824131 commit d10cab8
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 15 deletions.
25 changes: 21 additions & 4 deletions src/MultiSelectPrompt.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Laravel\Prompts;

use Closure;
use Illuminate\Support\Collection;

class MultiSelectPrompt extends Prompt
{
Expand All @@ -11,6 +12,20 @@ class MultiSelectPrompt extends Prompt
*/
public int $highlighted = 0;

/**
* The options for the multi-select prompt.
*
* @var array<int|string, string>
*/
public array $options;

/**
* The default values the multi-select prompt.
*
* @var array<int|string, string>
*/
public array $default;

/**
* The selected values.
*
Expand All @@ -21,17 +36,19 @@ class MultiSelectPrompt extends Prompt
/**
* Create a new SelectPrompt instance.
*
* @param array<int|string, string> $options
* @param array<int|string> $default
* @param array<int|string, string>|Collection<int|string, string> $options
* @param array<int|string>|Collection<int, int|string> $default
*/
public function __construct(
public string $label,
public array $options,
public array $default = [],
array|Collection $options,
array|Collection $default = [],
public int $scroll = 5,
public bool|string $required = false,
public ?Closure $validate = null,
) {
$this->options = $options instanceof Collection ? $options->all() : $options;
$this->default = $default instanceof Collection ? $default->all() : $default;
$this->values = $this->default;

$this->on('key', fn ($key) => match ($key) {
Expand Down
14 changes: 12 additions & 2 deletions src/SelectPrompt.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Laravel\Prompts;

use Closure;
use Illuminate\Support\Collection;

class SelectPrompt extends Prompt
{
Expand All @@ -11,18 +12,27 @@ class SelectPrompt extends Prompt
*/
public int $highlighted = 0;

/**
* The options for the select prompt.
*
* @var array<int|string, string>
*/
public array $options;

/**
* Create a new SelectPrompt instance.
*
* @param array<int|string, string> $options
* @param array<int|string, string>|Collection<int|string, string> $options
*/
public function __construct(
public string $label,
public array $options,
array|Collection $options,
public int|string|null $default = null,
public int $scroll = 5,
public ?Closure $validate = null,
) {
$this->options = $options instanceof Collection ? $options->all() : $options;

if ($this->default) {
if (array_is_list($this->options)) {
$this->highlighted = array_search($this->default, $this->options);
Expand Down
14 changes: 12 additions & 2 deletions src/SuggestPrompt.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Laravel\Prompts;

use Closure;
use Illuminate\Support\Collection;
use InvalidArgumentException;

class SuggestPrompt extends Prompt
Expand All @@ -14,6 +15,13 @@ class SuggestPrompt extends Prompt
*/
public ?int $highlighted = null;

/**
* The options for the suggest prompt.
*
* @var array<string>|Closure(string): array<string>
*/
public array|Closure $options;

/**
* The cache of matches.
*
Expand All @@ -24,17 +32,19 @@ class SuggestPrompt extends Prompt
/**
* Create a new SuggestPrompt instance.
*
* @param array<string>|Closure(string): array<string> $options
* @param array<string>|Collection<int, string>|Closure(string): array<string> $options
*/
public function __construct(
public string $label,
public array|Closure $options,
array|Collection|Closure $options,
public string $placeholder = '',
public string $default = '',
public int $scroll = 5,
public bool|string $required = false,
public ?Closure $validate = null,
) {
$this->options = $options instanceof Collection ? $options->all() : $options;

$this->trackTypedValue($default);

$this->on('key', fn ($key) => match ($key) {
Expand Down
15 changes: 8 additions & 7 deletions src/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Laravel\Prompts;

use Closure;
use Illuminate\Support\Collection;

/**
* Prompt the user for text input.
Expand All @@ -23,21 +24,21 @@ function password(string $label, string $placeholder = '', bool|string $required
/**
* Prompt the user to select an option.
*
* @param array<int|string, string> $options
* @param array<int|string, string>|Collection<int|string, string> $options
*/
function select(string $label, array $options, int|string $default = null, int $scroll = 5, Closure $validate = null): int|string
function select(string $label, array|Collection $options, int|string $default = null, int $scroll = 5, Closure $validate = null): int|string
{
return (new SelectPrompt($label, $options, $default, $scroll, $validate))->prompt();
}

/**
* Prompt the user to select multiple options.
*
* @param array<int|string, string> $options
* @param array<int|string> $default
* @param array<int|string, string>|Collection<int|string, string> $options
* @param array<int|string>|Collection<int, 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
function multiselect(string $label, array|Collection $options, array|Collection $default = [], int $scroll = 5, bool|string $required = false, Closure $validate = null): array
{
return (new MultiSelectPrompt($label, $options, $default, $scroll, $required, $validate))->prompt();
}
Expand All @@ -53,9 +54,9 @@ function confirm(string $label, bool $default = true, string $yes = 'Yes', strin
/**
* Prompt the user for text input with auto-completion.
*
* @param array<string>|Closure(string): array<string> $options
* @param array<string>|Collection<int, string>|Closure(string): array<string> $options
*/
function suggest(string $label, array|Closure $options, string $placeholder = '', string $default = '', int $scroll = 5, bool|string $required = false, Closure $validate = null): string
function suggest(string $label, array|Collection|Closure $options, string $placeholder = '', string $default = '', int $scroll = 5, bool|string $required = false, Closure $validate = null): string
{
return (new SuggestPrompt($label, $options, $placeholder, $default, $scroll, $required, $validate))->prompt();
}
Expand Down
16 changes: 16 additions & 0 deletions tests/Feature/MultiselectPromptTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,22 @@
expect($result)->toBe(['green']);
});

it('accepts collections', function () {
Prompt::fake([Key::ENTER]);

$result = multiselect(
label: 'What are your favorite colors?',
options: collect([
'Red',
'Green',
'Blue',
]),
default: collect(['Green'])
);

expect($result)->toBe(['Green']);
});

it('validates', function () {
Prompt::fake([Key::ENTER, Key::SPACE, 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 @@ -50,6 +50,21 @@
expect($result)->toBe(2);
});

it('accepts a collection', function () {
Prompt::fake([Key::DOWN, Key::ENTER]);

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

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

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

Expand Down
12 changes: 12 additions & 0 deletions tests/Feature/SuggestPromptTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,18 @@
expect($result)->toBe('Green');
});

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

$result = suggest('What is your favorite color?', collect([
'Red',
'Green',
'Blue',
]));

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

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

Expand Down

0 comments on commit d10cab8

Please sign in to comment.