From 52622ba4ce8ba4b1ce53004b1f9f3478cf9849e8 Mon Sep 17 00:00:00 2001 From: Jess Archer Date: Fri, 22 Sep 2023 13:37:22 +1000 Subject: [PATCH] Allow `select` and `search` "required" message to be customized. --- src/SearchPrompt.php | 13 +++++++------ src/SelectPrompt.php | 13 +++++++------ src/helpers.php | 10 ++++++---- tests/Feature/SearchPromptTest.php | 6 ++++++ tests/Feature/SelectPromptTest.php | 14 ++++++++++++++ 5 files changed, 40 insertions(+), 16 deletions(-) diff --git a/src/SearchPrompt.php b/src/SearchPrompt.php index b86f5169..645ce1af 100644 --- a/src/SearchPrompt.php +++ b/src/SearchPrompt.php @@ -3,6 +3,7 @@ namespace Laravel\Prompts; use Closure; +use InvalidArgumentException; class SearchPrompt extends Prompt { @@ -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. * @@ -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(); diff --git a/src/SelectPrompt.php b/src/SelectPrompt.php index a61389a6..d04d7429 100644 --- a/src/SelectPrompt.php +++ b/src/SelectPrompt.php @@ -4,6 +4,7 @@ use Closure; use Illuminate\Support\Collection; +use InvalidArgumentException; class SelectPrompt extends Prompt { @@ -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. * @@ -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(); diff --git a/src/helpers.php b/src/helpers.php index 796f804d..5ffb94ae 100644 --- a/src/helpers.php +++ b/src/helpers.php @@ -25,10 +25,11 @@ function password(string $label, string $placeholder = '', bool|string $required * Prompt the user to select an option. * * @param array|Collection $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(); } /** @@ -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 $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(); } /** diff --git a/tests/Feature/SearchPromptTest.php b/tests/Feature/SearchPromptTest.php index 407284d1..63803db2 100644 --- a/tests/Feature/SearchPromptTest.php +++ b/tests/Feature/SearchPromptTest.php @@ -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.'); diff --git a/tests/Feature/SelectPromptTest.php b/tests/Feature/SelectPromptTest.php index fc784626..922aacf6 100644 --- a/tests/Feature/SelectPromptTest.php +++ b/tests/Feature/SelectPromptTest.php @@ -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.');