Reset wizard? #69
-
Is there a way to reset the wizard? At the end of the wizard my action is completed but short of a full page reload I don't see how to set the wizard back to the initial state and on the first step so it's ready to be called again. Am I just missing something? |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 1 reply
-
The $this->emitUp('showStep', $stepName, $this->state()->currentStep()); I think if you pass an empty array it would just reset all state. |
Beta Was this translation helpful? Give feedback.
-
@ju5t that didn't quite work, but got me on the right path to a solution. For anybody who stumbles on this, the So I added a listener for a reset event that calls the class MyWizardComponent extends WizardComponent
{
// This is ugly... have to override the $listeners to add our listener
protected $listeners = [
'previousStep',
'nextStep',
'resetWizard',
'showStep',
];
public function resetWizard()
{
// Get the first step from the collection
$firstStep = $this->stepNames()->first();
// Create an array of step names with empty states
$initialState = $this->stepNames()->mapWithKeys(function ($step) {
return [$step => []];
})->all();
$this->mountMountsWizard($firstStep, $initialState);
}
// ... rest of class
} |
Beta Was this translation helpful? Give feedback.
-
Hi, I have question |
Beta Was this translation helpful? Give feedback.
-
Thanks! Worked for me, just needed to update using livewire v3: use Livewire\Attributes\On;
use Spatie\LivewireWizard\Components\WizardComponent;
abstract class CustomWizardComponent extends WizardComponent
{
protected function getListeners(): array
{
return [...parent::getListeners(), 'resetWizard'];
}
#[On('resetWizard')]
public function resetWizard()
{
// Get the first step from the collection
$firstStep = $this->stepNames()->first();
// Create an array of step names with empty states
$initialState = $this->stepNames()->mapWithKeys(function ($step) {
return [$step => []];
})->all();
$this->mountMountsWizard($firstStep, $initialState);
}
} |
Beta Was this translation helpful? Give feedback.
@ju5t that didn't quite work, but got me on the right path to a solution.
For anybody who stumbles on this, the
mountMountsWizard
function inside theMountsWizard
trait is what is responsible for setting up the steps and states. so I can use it to reset the state.So I added a listener for a reset event that calls the
mountMountsWizard()
function.