diff --git a/packages/schemas/resources/js/components/wizard.js b/packages/schemas/resources/js/components/wizard.js index 76a5884117a..a86ea0fb928 100644 --- a/packages/schemas/resources/js/components/wizard.js +++ b/packages/schemas/resources/js/components/wizard.js @@ -48,6 +48,23 @@ export default function wizardSchemaComponent({ this.scroll() }, + goToStep(stepKey) { + const stepIndex = this.getStepIndex(stepKey) + + if (stepIndex <= -1) { + return + } + + if (!isSkippable && stepIndex > this.getStepIndex(this.step)) { + return + } + + this.step = stepKey + + this.autofocusFields() + this.scroll() + }, + scroll() { this.$nextTick(() => { this.$refs.header?.children[ diff --git a/packages/schemas/resources/views/components/wizard.blade.php b/packages/schemas/resources/views/components/wizard.blade.php index 28bc1ae9aa9..24f49ed2b74 100644 --- a/packages/schemas/resources/views/components/wizard.blade.php +++ b/packages/schemas/resources/views/components/wizard.blade.php @@ -18,6 +18,7 @@ stepQueryStringKey: @js($getStepQueryStringKey()), })" x-on:next-wizard-step.window="if ($event.detail.key === @js($key)) goToNextStep()" + x-on:wizard-go-to-step.window="($event.detail.key === @js($key)) && goToStep($event.detail.step)" wire:ignore.self {{ $attributes diff --git a/packages/schemas/src/Components/Wizard.php b/packages/schemas/src/Components/Wizard.php index 680676cbdf6..417468395aa 100644 --- a/packages/schemas/src/Components/Wizard.php +++ b/packages/schemas/src/Components/Wizard.php @@ -128,6 +128,37 @@ public function previousStep(int $currentStepIndex): void $this->currentStepIndex($currentStepIndex - 1); } + #[ExposedLivewireMethod] + public function goToStep(string $step): void + { + $steps = array_values( + $this->getChildSchema()->getComponents() + ); + + foreach ($steps as $index => $wizardStep) { + if ($wizardStep->getKey() !== $step) { + continue; + } + + if (! $this->isSkippable() && $index > $this->getCurrentStepIndex()) { + return; + } + + $this->currentStepIndex($index); + + /** @var HasSchemas&LivewireComponent $livewire */ + $livewire = $this->getLivewire(); + + $livewire->dispatch( + 'wizard-go-to-step', + key: $this->getKey(), + step: $step, + ); + + return; + } + } + public function getNextAction(): Action { $action = Action::make($this->getNextActionName())