Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow select and search "required" message to be customized. #84

Merged
merged 1 commit into from
Sep 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions src/SearchPrompt.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Laravel\Prompts;

use Closure;
use InvalidArgumentException;

class SearchPrompt extends Prompt
{
Expand All @@ -20,11 +21,6 @@ class SearchPrompt extends Prompt
*/
public int $firstVisible = 0;

/**
* Whether user input is required.
*/
public bool|string $required = true;

/**
* The cached matches.
*
Expand All @@ -43,8 +39,13 @@ public function __construct(
public string $placeholder = '',
public int $scroll = 5,
public ?Closure $validate = null,
public string $hint = ''
public string $hint = '',
public bool|string $required = true,
) {
if ($this->required === false) {
throw new InvalidArgumentException('Argument [required] must be true or a string.');
}

$this->trackTypedValue(submit: false);

$this->reduceScrollingToFitTerminal();
Expand Down
13 changes: 7 additions & 6 deletions src/SelectPrompt.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

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

class SelectPrompt extends Prompt
{
Expand All @@ -26,11 +27,6 @@ class SelectPrompt extends Prompt
*/
public array $options;

/**
* Whether user input is required.
*/
public bool|string $required = true;

/**
* Create a new SelectPrompt instance.
*
Expand All @@ -42,8 +38,13 @@ public function __construct(
public int|string|null $default = null,
public int $scroll = 5,
public ?Closure $validate = null,
public string $hint = ''
public string $hint = '',
public bool|string $required = true,
) {
if ($this->required === false) {
throw new InvalidArgumentException('Argument [required] must be true or a string.');
}

$this->options = $options instanceof Collection ? $options->all() : $options;

$this->reduceScrollingToFitTerminal();
Expand Down
10 changes: 6 additions & 4 deletions src/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,11 @@ function password(string $label, string $placeholder = '', bool|string $required
* Prompt the user to select an option.
*
* @param array<int|string, string>|Collection<int|string, string> $options
* @param true|string $required
*/
function select(string $label, array|Collection $options, int|string $default = null, int $scroll = 5, Closure $validate = null, string $hint = ''): int|string
function select(string $label, array|Collection $options, int|string $default = null, int $scroll = 5, Closure $validate = null, string $hint = '', bool|string $required = true): int|string
{
return (new SelectPrompt($label, $options, $default, $scroll, $validate, $hint))->prompt();
return (new SelectPrompt($label, $options, $default, $scroll, $validate, $hint, $required))->prompt();
}

/**
Expand Down Expand Up @@ -65,10 +66,11 @@ function suggest(string $label, array|Collection|Closure $options, string $place
* Allow the user to search for an option.
*
* @param Closure(string): array<int|string, string> $options
* @param true|string $required
*/
function search(string $label, Closure $options, string $placeholder = '', int $scroll = 5, Closure $validate = null, string $hint = ''): int|string
function search(string $label, Closure $options, string $placeholder = '', int $scroll = 5, Closure $validate = null, string $hint = '', bool|string $required = true): int|string
{
return (new SearchPrompt($label, $options, $placeholder, $scroll, $validate, $hint))->prompt();
return (new SearchPrompt($label, $options, $placeholder, $scroll, $validate, $hint, $required))->prompt();
}

/**
Expand Down
6 changes: 6 additions & 0 deletions tests/Feature/SearchPromptTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,3 +123,9 @@

search('What is your favorite color?', fn () => []);
})->throws(NonInteractiveValidationException::class, 'Required.');

it('allows the required validation message to be customised when non-interactive', function () {
Prompt::interactive(false);

search('What is your favorite color?', fn () => [], required: 'The color is required.');
})->throws(NonInteractiveValidationException::class, 'The color is required.');
14 changes: 14 additions & 0 deletions tests/Feature/SelectPromptTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -263,3 +263,17 @@
validate: fn ($value) => $value === 'None' ? 'Required.' : null,
);
})->throws(NonInteractiveValidationException::class, 'Required.');

it('Allows the required validation message to be customised when non-interactive', function () {
Prompt::interactive(false);

select(
label: 'What is your favorite color?',
options: [
'Red',
'Green',
'Blue',
],
required: 'The color is required.',
);
})->throws(NonInteractiveValidationException::class, 'The color is required.');