From 7f2060e977df28fff6b8cd8f95c5463f427b57f8 Mon Sep 17 00:00:00 2001 From: Jochem Blok Date: Wed, 9 Oct 2024 21:42:00 +0200 Subject: [PATCH] Adds support for conditional steps (#171) * add support for conditional forms - # * add testcases - # * implement the change with a new addIf method - # * Update src/FormBuilder.php * Update FormBuilder.php --------- Co-authored-by: Jess Archer Co-authored-by: Taylor Otwell --- src/FormBuilder.php | 10 ++++++++ tests/Feature/FormTest.php | 48 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+) diff --git a/src/FormBuilder.php b/src/FormBuilder.php index 4e5bd5d2..03056a4a 100644 --- a/src/FormBuilder.php +++ b/src/FormBuilder.php @@ -32,6 +32,16 @@ public function add(Closure $step, ?string $name = null, bool $ignoreWhenReverti return $this; } + /** + * Add a new conditional step. + */ + public function addIf(Closure|bool $condition, Closure $step, ?string $name = null, bool $ignoreWhenReverting = false): self + { + $this->steps[] = new FormStep($step, $condition, $name, $ignoreWhenReverting); + + return $this; + } + /** * Run all of the given steps. * diff --git a/tests/Feature/FormTest.php b/tests/Feature/FormTest.php index 2bd1a4b0..442b3180 100644 --- a/tests/Feature/FormTest.php +++ b/tests/Feature/FormTest.php @@ -6,6 +6,7 @@ use function Laravel\Prompts\confirm; use function Laravel\Prompts\form; use function Laravel\Prompts\outro; +use function Laravel\Prompts\text; it('can run multiple steps', function () { Prompt::fake([ @@ -179,3 +180,50 @@ Prompt::assertOutputDoesntContain('This should not appear!'); }); + +it('can revert steps with conditions', function () { + Prompt::fake([ + 'L', 'u', 'k', 'e', Key::ENTER, // name + Key::DOWN, Key::ENTER, // JS + Key::CTRL_U, // revert + Key::UP, Key::ENTER, // PHP + '8', '.', '3', Key::ENTER, // version + Key::ENTER, + ]); + + $responses = form() + ->text('What is your name?') + ->select('What is your language?', ['PHP', 'JS']) + ->addIf(fn ($responses) => $responses[1] === 'PHP', fn ($responses) => text("Which version?")) + ->confirm('Are you sure?') + ->submit(); + + expect($responses)->toBe([ + 'Luke', + 'PHP', + '8.3', + true, + ]); +}); + +it('leaves skipped conditional field empty', function () { + Prompt::fake([ + 'L', 'u', 'k', 'e', Key::ENTER, // name + Key::DOWN, Key::ENTER, // JS + Key::ENTER, + ]); + + $responses = form() + ->text('What is your name?') + ->select('What is your language?', ['PHP', 'JS']) + ->addIf(fn ($responses) => $responses[1] === 'PHP', fn ($responses) => text("Which version?")) + ->confirm('Are you sure?') + ->submit(); + + expect($responses)->toBe([ + 'Luke', + 'JS', + null, + true, + ]); +});