Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add currentStepIndex to summary #31

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

ziming
Copy link
Contributor

@ziming ziming commented Oct 6, 2021

It can be very helpful to easily get the currentStepIndex from the $wizard variable in our blade view. where we can do things like

{{-- go previous step --}}
href="{{ route("wizard.{$wizard['slug']}::{$wizard['steps'][$wizard['currentStepIndex'] - 1]['slug']}.show") }}" 

I named it currentStepIndex instead of currentStep because it is 0 based. Also I think if you decide to add currentStep in the future (starting from 1), it will be less confusing

@ksassnowski
Copy link
Collaborator

Thanks for the PR! A couple of thoughts on this.

Your example suggests that you'd mainly use the currentStepIndex to determine the previous and next step of the wizard. This makes total sense, but at the same time this is also the only use case I can think of where you would have to know the current step index. If that's the case, I think it would be a better idea to simply include a next and previous key in the wizard data that contains the corresponding URLs.

I'd prefer the step index to stay an implementation detail and instead directly expose the functionality that people would use the index for.

Let me know what you think.

@ziming
Copy link
Contributor Author

ziming commented Oct 19, 2021

Hmm another possible use of currentStepIndex maybe the form wizard page want to show the current step number as sort of a progress indicator

{{ $wizard['currentStepIndex'] + 1 }}: {{ $wizard['steps'][$wizard['currentStepIndex']]['title'] }}

[Form Wizard Form Here]

If there is no currentStepIndex, the code could end up like this (feel free to convert it to its blade equivalent). While this is doable, it feels troublesome. I either have to loop through it or hard code the currentIndex value for that step blade view

for ($i = 0; $i < count($wizard['steps']); $i++) {

    if ($wizard['steps'][$i]['active']) {
        echo 'Step Number: ' . ($i + 1);
        break;
    }

}

For previous and next link wise. I think they may be better off as "static" links like

form-wizards/1/{{ $formWizardSlug }}/next
form-wizards/1/{{ $formWizardSlug }}/previous

Because it is likely people could have more advanced use cases and want to see what the user submitted in say step 2 (current step) before deciding to redirect to say step 3 or step 6

@ksassnowski
Copy link
Collaborator

I don't think static next and previous links like you suggested would work. They're relative links, meaning they always need to know what the currently active step is. That information isn't kept in the backend but instead inferred from the {step} parameter of the URL. So simply saying

/wizards/{wizard-slug}/{wizard-id}/next

wouldn't work since there's no way to know what next is relative to. This might work if you scope it below the step route like this

/wizard/{wizard-slug}/{wizard-id}/{step-slug}/next

but I'd have to think about this a little more.

In regards to numbering the steps in the view, you're example is actually exactly how I would suggest doing it. This way there doesn't have to be any custom syntax for it.

@ziming
Copy link
Contributor Author

ziming commented Oct 25, 2021

Hmm I just find it very inconvenient to have a loop just to output the current step number and title & may feel even more convoluted if we are showing like a progress bar of say --- 6 circles & checking which 1 to highlight & have link and which 1 to be greyed out and have no link. With currentStepIndex, it look much simpler

But it's your package. So it's your choice in the end :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants