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

Adds support for getting progress on individual steps #105

Closed
wants to merge 1 commit into from
Closed
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
4 changes: 2 additions & 2 deletions src/Components/Concerns/MountsWizard.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

trait MountsWizard
{
public function mountMountsWizard(?string $showStep = null, array $initialState = null)
public function mountMountsWizard(?string $showStep = null, ?array $initialState = null)
{
$stepName = $showStep ?? $this->currentStepName ?? $this->stepNames()->first();

Expand All @@ -21,6 +21,6 @@ public function mountMountsWizard(?string $showStep = null, array $initialState

if (! is_a($this->stateClass(), State::class, true)) {
throw InvalidStateClassName::doesNotExtendState(static::class, $this->stateClass());
};
}
}
}
17 changes: 16 additions & 1 deletion src/Components/Concerns/StepAware.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ trait StepAware
{
public array $steps = [];

public float $progress = 0;

public function bootedStepAware()
{
$currentFound = false;
Expand All @@ -24,7 +26,6 @@ public function bootedStepAware()

$status = $currentFound ? StepStatus::Next : StepStatus::Previous;


if ($stepName === $currentStepName) {
$currentFound = true;
$status = StepStatus::Current;
Expand All @@ -33,5 +34,19 @@ public function bootedStepAware()
return new Step($stepName, $info, $status);
})
->toArray();

$this->getProgress();
}

public function getProgress(): float
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of calculating the percentage of the progress, I would just let the component be aware of the stepnumber and total number of steps.

Formatting this info as a percentage is then a task for the user of the package, and not the package itself.

{
$steps = collect($this->steps);
$totalSteps = $steps->count() - 1;

$index = $steps->search(fn (Step $step) => $step->stepName === $this->getName());

return $this->progress = $totalSteps > 0
? round(abs($index / $totalSteps), 2)
: 0;
}
}
2 changes: 1 addition & 1 deletion tests/TestSupport/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ protected function setUp(): void

config()->set('app.key', '6rE9Nz59bGRbeMATftriyQjrpF7DcOQm');

View::addNamespace('test', __DIR__ . '/resources/views');
View::addNamespace('test', __DIR__.'/resources/views');

$this
->registerLivewireComponents()
Expand Down
40 changes: 40 additions & 0 deletions tests/WizardTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Spatie\LivewireWizard\Exceptions\NoNextStep;
use Spatie\LivewireWizard\Exceptions\NoPreviousStep;
use Spatie\LivewireWizard\Exceptions\StepDoesNotExist;
use Spatie\LivewireWizard\Support\Step;
use Spatie\LivewireWizard\Tests\TestSupport\Components\MyWizardComponent;
use Spatie\LivewireWizard\Tests\TestSupport\Components\Steps\FirstStepComponent;
use Spatie\LivewireWizard\Tests\TestSupport\Components\Steps\SecondStepComponent;
Expand Down Expand Up @@ -185,3 +186,42 @@
expect($this->thirdStep->hasPreviousStep())->toBeTrue();
expect($this->thirdStep->hasNextStep())->toBeFalse();
});

it('can properly determine the step progress', function () {
// Set up the step names array to match the expected steps
$stepNames = [
app(ComponentRegistry::class)->getName(FirstStepComponent::class),
app(ComponentRegistry::class)->getName(SecondStepComponent::class),
app(ComponentRegistry::class)->getName(ThirdStepComponent::class),
];

// Setup components
$this->firstStep = new FirstStepComponent();
$this->firstStep->allStepNames = $stepNames;
$this->firstStep->setName('first-step');
$this->firstStep->bootedStepAware();

$this->secondStep = new SecondStepComponent();
$this->secondStep->allStepNames = $stepNames;
$this->secondStep->setName('second-step');
$this->secondStep->bootedStepAware();

$this->thirdStep = new ThirdStepComponent();
$this->thirdStep->allStepNames = $stepNames;
$this->thirdStep->setName('third-step');
$this->thirdStep->bootedStepAware();

// Get progress
$firstStepProgress = $this->firstStep->getProgress();
$secondStepProgress = $this->secondStep->getProgress();
$thirdStepProgress = $this->thirdStep->getProgress();

expect($this->firstStep->progress)->toBe(0.0);
expect($firstStepProgress)->toBe(0.0);

expect($this->secondStep->progress)->toBe(0.5);
expect($secondStepProgress)->toBe(0.5);

expect($this->thirdStep->progress)->toBe(1.0);
expect($thirdStepProgress)->toBe(1.0);
});
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<html><body><div id="navigation">
This is navigation
<ul>
<!-- __BLOCK__ --> In step
<!--[if BLOCK]><![endif]--> In step
<li class="" wire:click="showStep('first-step')">First step</li>
In step
<li class="text-bold">Second step</li>
In step
<li class="">Third step</li>
<!-- __ENDBLOCK__ -->
<!--[if ENDBLOCK]><![endif]-->
</ul>
</div></body></html>