Skip to content

Commit

Permalink
Merge pull request #78 from Bird87ZA/add-has-step-checks
Browse files Browse the repository at this point in the history
Add has step checks
  • Loading branch information
freekmurze authored Apr 5, 2023
2 parents bea0d60 + 9c5d5e1 commit 5ff6ef2
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 0 deletions.
7 changes: 7 additions & 0 deletions docs/usage/navigating-steps.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,13 @@ You can also call it in your view.
</div>
```

You can also check if a next or previous step exists directly from the step component.

```blade
$this->hasNextStep();
$this->hasPreviousStep();
```

## Start at a specific step

If you want the wizard to display a specific step when it is rendered first, you can pass the step name to the `show-step` property.
Expand Down
10 changes: 10 additions & 0 deletions src/Components/StepComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,16 @@ public function showStep(string $stepName)
$this->emitUp('showStep', $stepName, $this->state()->currentStep());
}

public function hasPreviousStep()
{
return $this->allStepNames[0] !== Livewire::getAlias(static::class);
}

public function hasNextStep()
{
return end($this->allStepNames) !== Livewire::getAlias(static::class);
}

public function stepInfo(): array
{
return [];
Expand Down
28 changes: 28 additions & 0 deletions tests/WizardTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,3 +142,31 @@

assertMatchesHtmlSnapshot($navigationHtml);
});

it('has the correct has step states', function () {
// Set up the step names array to match the expected steps
$stepNames = [
Livewire::getAlias(FirstStepComponent::class),
Livewire::getAlias(SecondStepComponent::class),
Livewire::getAlias(ThirdStepComponent::class),
];

// Create instances of each step component and set the allStepNames property on them
$this->firstStep = new FirstStepComponent();
$this->firstStep->allStepNames = $stepNames;

$this->secondStep = new SecondStepComponent();
$this->secondStep->allStepNames = $stepNames;

$this->thirdStep = new ThirdStepComponent();
$this->thirdStep->allStepNames = $stepNames;

expect($this->firstStep->hasPreviousStep())->toBeFalse();
expect($this->firstStep->hasNextStep())->toBeTrue();

expect($this->secondStep->hasPreviousStep())->toBeTrue();
expect($this->secondStep->hasNextStep())->toBeTrue();

expect($this->thirdStep->hasPreviousStep())->toBeTrue();
expect($this->thirdStep->hasNextStep())->toBeFalse();
});

0 comments on commit 5ff6ef2

Please sign in to comment.