Skip to content

Commit

Permalink
Merge pull request #108 from bangnokia/main
Browse files Browse the repository at this point in the history
add forStepClass method to get state more convenient
  • Loading branch information
freekmurze authored Feb 21, 2024
2 parents 9c80e5a + 518053a commit 0f020ed
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 2 deletions.
2 changes: 2 additions & 0 deletions docs/usage/accessing-state.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@ On that state object you can call these methods:

- `all()`: returns an array containing the values of all public properties of all steps in the wizard. The key of the items in the array is the name of the step. Optionally, you can pass is a key name to let the function only return the value for that key.
- `forStep($stepname)`: returns the values of all public properties of the given step.
- `forStepClass(StepComponent::class)`: similar to `forStep`, but you can pass in the class name of the step component.
- `currentStep()`: returns an array containing the values of all public properties of the current step. The result is almost identical to Livewire's native `all()` method, but with some internal properties filtered out.

```php
// in a step component

$this->state()->all(); // returns all state from all steps in the wizard
$this->state()->forStep('delivery-address-step'); // returns all state of the given step
$this->state()->forStepClass(App\Livewire\DevliveryAddressStep::class);
$this->state()->currentStep(); // returns all state of the current step
```

Expand Down
8 changes: 8 additions & 0 deletions src/Support/State.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Spatie\LivewireWizard\Support;

use Illuminate\Support\Arr;
use Livewire\Mechanisms\ComponentRegistry;

class State
{
Expand Down Expand Up @@ -34,6 +35,13 @@ public function forStep(string $stepName): array
return $state;
}

public function forStepClass(string $stepClass): array
{
$stepName = app(ComponentRegistry::class)->getName($stepClass);

return $this->forStep($stepName);
}

public function get(string $key)
{
return Arr::get($this->allState, $key);
Expand Down
3 changes: 3 additions & 0 deletions tests/StateClassTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,8 @@
$livewireComponent = $testableLivewire->instance();
$state = $livewireComponent->state()->forStep('first-step');
expect($state['order'])->toBe(1029);

$state = $livewireComponent->state()->forStepClass(FirstStepComponent::class);
expect($state['order'])->toBe(1029);
});
});
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>

0 comments on commit 0f020ed

Please sign in to comment.