diff --git a/src/Livewire/Concerns/WithSteps.php b/src/Livewire/Concerns/WithSteps.php index 46818fc6..98c8dab4 100644 --- a/src/Livewire/Concerns/WithSteps.php +++ b/src/Livewire/Concerns/WithSteps.php @@ -2,34 +2,37 @@ namespace Aerni\LivewireForms\Livewire\Concerns; +use Statamic\Fields\Section; use Livewire\Attributes\Computed; use Aerni\LivewireForms\Form\Step; -use Aerni\LivewireForms\Form\Section; +use Illuminate\Support\Collection; use Aerni\LivewireForms\Enums\StepStatus; use Aerni\LivewireForms\Exceptions\StepDoesNotExist; -use Illuminate\Support\Collection; trait WithSteps { - // TODO: Do we really need a synth or can we just use a computed property? - public Collection $steps; + public int $currentStep = 1; - public function mountWithSteps(): void + #[Computed] + public function steps(): Collection { - $this->steps = $this->steps(); - } + $currentFound = false; - protected function steps(): Collection - { return $this->form->blueprint()->tabs()->first()->sections() - ->filter(fn ($section) => $section->fields()->all()->isNotEmpty()) + ->filter(fn (Section $section) => $section->fields()->all()->isNotEmpty()) ->values() - ->mapWithKeys(function ($section, $index) { + ->mapWithKeys(function (Section $section, int $index) use (&$currentFound) { $number = $index + 1; + $status = $currentFound ? StepStatus::Next : StepStatus::Previous; + + if ($number === $this->currentStep) { + $currentFound = true; + $status = StepStatus::Current; + } return [$number => new Step( number: $number, - status: $number === 1 ? StepStatus::Current : StepStatus::Next, + status: $status, fields: $this->fields->intersectByKeys($section->fields()->all()), display: $section->display(), instructions: $section->instructions(), @@ -46,7 +49,7 @@ public function previousStep(): void { $previousStep = $this->steps->before(fn (Step $step) => $step->isCurrent()); - throw_unless($previousStep, StepDoesNotExist::noPreviousStep($this->currentStep()->number)); + throw_unless($previousStep, StepDoesNotExist::noPreviousStep($this->currentStep)); $this->showStep($previousStep->number); } @@ -55,41 +58,33 @@ public function nextStep(): void { $nextStep = $this->steps->after(fn (Step $step) => $step->isCurrent()); - throw_unless($nextStep, StepDoesNotExist::noNextStep($this->currentStep()->number)); + throw_unless($nextStep, StepDoesNotExist::noNextStep($this->currentStep)); $this->showStep($nextStep->number); } - public function showStep(int $stepNumber): void + public function showStep(int $step): void { - $step = $this->steps->get($stepNumber); - - throw_unless($step, StepDoesNotExist::stepNotFound($step->number)); - - if ($step->isCurrent()) { + if ($step === $this->currentStep) { return; } - $step->status = StepStatus::Current; + throw_unless($this->steps->has($step), StepDoesNotExist::stepNotFound($step)); - $this->steps - ->filter(fn (Step $step) => $step->number < $stepNumber) - ->each(fn (Step $step) => $step->status = StepStatus::Previous); + $this->currentStep = $step; - $this->steps - ->filter(fn (Step $step) => $step->number > $stepNumber) - ->each(fn (Step $step) => $step->status = StepStatus::Next); + unset($this->steps); } #[Computed] public function hasPreviousStep(): bool { - return (bool) $this->steps->before(fn ($step) => $step->isCurrent()); + return (bool) $this->steps->before(fn (Step $step) => $step->isCurrent()); } #[Computed] public function hasNextStep(): bool { - return (bool) $this->steps->after(fn ($step) => $step->isCurrent()); + return (bool) $this->steps->after(fn (Step $step) => $step->isCurrent()); } } diff --git a/src/Livewire/Synthesizers/StepSynth.php b/src/Livewire/Synthesizers/StepSynth.php deleted file mode 100644 index 74f61c0d..00000000 --- a/src/Livewire/Synthesizers/StepSynth.php +++ /dev/null @@ -1,46 +0,0 @@ -toArray(); - - foreach ($data as $key => $child) { - $data[$key] = $dehydrateChild($key, $child); - } - - return [ - $data, - ['class' => get_class($target)], - ]; - } - - public function hydrate($value, $meta, $hydrateChild) - { - foreach ($value as $key => $child) { - $value[$key] = $hydrateChild($key, $child); - } - - return new Step( - number: $value['number'], - fields: $value['fields'], - display: $value['display'], - instructions: $value['instructions'], - status: StepStatus::from($value['status']), - ); - } -} diff --git a/src/ServiceProvider.php b/src/ServiceProvider.php index 0cba61ee..a219e0c9 100644 --- a/src/ServiceProvider.php +++ b/src/ServiceProvider.php @@ -65,7 +65,6 @@ protected function bootLivewire(): self Livewire::component('wizard-form', WizardForm::class); Livewire::propertySynthesizer(FieldSynth::class); Livewire::propertySynthesizer(RuleSynth::class); - Livewire::propertySynthesizer(StepSynth::class); return $this; }