Skip to content

Commit

Permalink
Work in progress
Browse files Browse the repository at this point in the history
  • Loading branch information
lukeraymonddowning committed Mar 25, 2024
1 parent f586204 commit af34036
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 29 deletions.
30 changes: 11 additions & 19 deletions playground/form.php
Original file line number Diff line number Diff line change
@@ -1,20 +1,12 @@
<?php

use Laravel\Prompts\FormBuilder;
use function Laravel\Prompts\confirm;
use function Laravel\Prompts\form;
use function Laravel\Prompts\info;
use function Laravel\Prompts\intro;
use function Laravel\Prompts\multiselect;
use function Laravel\Prompts\note;
use function Laravel\Prompts\password;
use function Laravel\Prompts\pause;
use function Laravel\Prompts\select;
use function Laravel\Prompts\spin;
use function Laravel\Prompts\suggest;
use function Laravel\Prompts\text;

require __DIR__ . '/../vendor/autoload.php';
require __DIR__.'/../vendor/autoload.php';

$responses = form()
->intro('Welcome to Laravel')
Expand All @@ -32,24 +24,24 @@
'Taylor Otwell',
'Tim MacDonald',
],
validate: fn($value) => match (true) {
!$value => 'Please enter your name.',
validate: fn ($value) => match (true) {
! $value => 'Please enter your name.',
default => null,
},
)
->text(
label: 'Where should we create your project?',
placeholder: 'E.g. ./laravel',
validate: fn($value) => match (true) {
!$value => 'Please enter a path',
validate: fn ($value) => match (true) {
! $value => 'Please enter a path',
$value[0] !== '.' => 'Please enter a relative path',
default => null,
},
name: 'path'
)
->when(
fn(array $responses) => $responses['path'] === './laravel',
fn(array $responses) => info("This is a Laravel project!"),
fn (array $responses) => $responses['path'] === './laravel',
fn (array $responses) => info('This is a Laravel project!'),
ignoreWhenReverting: true,
)
->pause()
Expand All @@ -58,8 +50,8 @@
$moreResponses = form()
->password(
label: 'Provide a password',
validate: fn($value) => match (true) {
!$value => 'Please enter a password.',
validate: fn ($value) => match (true) {
! $value => 'Please enter a password.',
strlen($value) < 5 => 'Password should have at least 5 characters.',
default => null,
},
Expand Down Expand Up @@ -92,13 +84,13 @@
);

if ($install) {
spin(fn() => sleep(3), 'Installing dependencies...');
spin(fn () => sleep(3), 'Installing dependencies...');
}

return $install;
}, name: 'install')
->confirm('Finish installation?')
->add(fn($responses) => note(<<<EOT
->add(fn ($responses) => note(<<<EOT
Installation complete!
To get started, run:
Expand Down
7 changes: 4 additions & 3 deletions src/FormBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function add(Closure $step, ?string $name = null, bool $ignoreWhenReverti
/**
* Add a step if the given condition evaluates to true.
*
* @param bool|(Closure(array<mixed>): bool) $condition
* @param bool|(Closure(array<mixed>): bool) $condition
*/
public function when(bool|Closure $condition, Closure $step, ?string $name = null, bool $ignoreWhenReverting = false): self
{
Expand All @@ -57,6 +57,7 @@ public function submit(): array

if ($wasReverted && $index > 0 && $step->shouldIgnoreWhenReverting($this->responses)) {
$index--;

continue;
}

Expand Down Expand Up @@ -260,8 +261,8 @@ public function progress(string $label, iterable|int $steps, ?Closure $callback
/**
* Execute the given prompt passing the given arguments.
*
* @param callable-string $prompt
* @param array<mixed> $arguments
* @param callable-string $prompt
* @param array<mixed> $arguments
*/
protected function runPrompt(string $prompt, array $arguments, bool $ignoreWhenReverting = false): self
{
Expand Down
4 changes: 2 additions & 2 deletions src/FormStep.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function run(array $responses, mixed $previousResponse): mixed
/**
* Whether the step should run based on the given condition.
*
* @param array<mixed> $responses
* @param array<mixed> $responses
*/
protected function shouldRun(array $responses): bool
{
Expand All @@ -46,7 +46,7 @@ protected function shouldRun(array $responses): bool
/**
* Whether this step should be skipped over when a subsequent step is reverted.
*
* @param array<mixed> $responses
* @param array<mixed> $responses
*/
public function shouldIgnoreWhenReverting(array $responses): bool
{
Expand Down
7 changes: 2 additions & 5 deletions tests/Feature/FormTest.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
<?php

use Laravel\Prompts\FormBuilder;
use Laravel\Prompts\Key;
use Laravel\Prompts\Prompt;

use function Laravel\Prompts\confirm;
use function Laravel\Prompts\form;
use function Laravel\Prompts\outro;
use function Laravel\Prompts\select;
use function Laravel\Prompts\text;

it('can run multiple steps', function () {
Prompt::fake([
Expand Down Expand Up @@ -120,7 +117,7 @@
]);

form()
->when(true, fn () => confirm("Are you sure?"), name: 'confirm')
->when(true, fn () => confirm('Are you sure?'), name: 'confirm')
->when(
fn (array $responses) => $responses['confirm'],
fn (array $responses) => outro('This should display')
Expand All @@ -132,7 +129,7 @@
->submit();

form()
->confirm("Are you sure?", name: 'confirm')
->confirm('Are you sure?', name: 'confirm')
->when(
fn (array $responses) => $responses['confirm'],
fn () => outro('This should not display')
Expand Down

0 comments on commit af34036

Please sign in to comment.