-
Notifications
You must be signed in to change notification settings - Fork 15
refactor(wizard): use computed signal for internal states #1855
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
Open
spliffone
wants to merge
1
commit into
main
Choose a base branch
from
refactor/wizard-migrate-to-computed
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+68
−56
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -39,6 +39,14 @@ interface StepItem { | |||||
| step: SiWizardStepComponent; | ||||||
| } | ||||||
|
|
||||||
| interface StepMetadata { | ||||||
| canActivate: boolean; | ||||||
| stateClass: string; | ||||||
| ariaDisabled: 'true' | 'false'; | ||||||
| ariaCurrent: 'step' | 'false'; | ||||||
| icon: string; | ||||||
| } | ||||||
|
|
||||||
| @Component({ | ||||||
| selector: 'si-wizard', | ||||||
| imports: [SiIconComponent, SiResizeObserverDirective, SiTranslatePipe, NgTemplateOutlet], | ||||||
|
|
@@ -240,31 +248,59 @@ export class SiWizardComponent { | |||||
| elementWarningFilled | ||||||
| }); | ||||||
|
|
||||||
| protected canActivate(stepIndex: number): boolean { | ||||||
| if (stepIndex < 0) { | ||||||
| return false; | ||||||
| } | ||||||
| // Can always activate previous steps | ||||||
| if (stepIndex < this.index) { | ||||||
| return true; | ||||||
| } | ||||||
| // We are already in the step. Nothing to activate. | ||||||
| if (stepIndex === this.index) { | ||||||
| return false; | ||||||
| } | ||||||
| // Fast-forward: check all steps if they are valid | ||||||
| for (let i = this.index; i < stepIndex; i++) { | ||||||
| const theStep = this.steps()[i]; | ||||||
| if (!theStep.isValid()) { | ||||||
| return false; | ||||||
| protected readonly stepsMetadata = computed((): StepMetadata[] => { | ||||||
| const index = this._index(); | ||||||
| const steps = this.steps(); | ||||||
| const stepFailedIcon = this.stepFailedIcon(); | ||||||
| const stepActiveIcon = this.stepActiveIcon(); | ||||||
| const stepIcon = this.stepIcon(); | ||||||
| const stepCompletedIcon = this.stepCompletedIcon(); | ||||||
|
|
||||||
| // O(N) pre-calculation: find the first invalid step from the current index | ||||||
| let firstInvalidIndex = steps.length; | ||||||
| for (let i = index; i < steps.length; i++) { | ||||||
| if (!steps[i].isValid()) { | ||||||
| firstInvalidIndex = i; | ||||||
| break; | ||||||
| } | ||||||
| } | ||||||
| return true; | ||||||
| } | ||||||
|
|
||||||
| return steps.map((step, stepIndex) => { | ||||||
| // canActivate: O(1) per step using pre-calculated firstInvalidIndex | ||||||
| let canActivate: boolean; | ||||||
| if (stepIndex < index) { | ||||||
| canActivate = true; | ||||||
| } else if (stepIndex === index) { | ||||||
| canActivate = false; | ||||||
| } else { | ||||||
| canActivate = firstInvalidIndex >= stepIndex; | ||||||
| } | ||||||
|
|
||||||
| // stateClass | ||||||
| const stateClass = this.getStateClass(stepIndex, canActivate); | ||||||
|
|
||||||
| // ariaDisabled | ||||||
| const ariaDisabled = !canActivate ? 'true' : 'false'; | ||||||
|
|
||||||
| // ariaCurrent | ||||||
| const ariaCurrent = stepIndex === index ? 'step' : 'false'; | ||||||
|
|
||||||
| // icon | ||||||
| let icon: string; | ||||||
| if (step.failed()) { | ||||||
| icon = stepFailedIcon; | ||||||
| } else { | ||||||
| const txtStyle = step.isActive() ? stepActiveIcon : stepIcon; | ||||||
| icon = stepIndex >= index ? txtStyle : stepCompletedIcon; | ||||||
| } | ||||||
|
|
||||||
| return { canActivate, stateClass, ariaDisabled, ariaCurrent, icon }; | ||||||
| }); | ||||||
| }); | ||||||
|
|
||||||
| protected activateStep(event: Event, stepIndex: number): void { | ||||||
| event.preventDefault(); | ||||||
| if (this.canActivate(stepIndex)) { | ||||||
| if (this.stepsMetadata()[stepIndex].canActivate) { | ||||||
| if (stepIndex > this.index) { | ||||||
| this.next(stepIndex - this.index); | ||||||
| } | ||||||
|
|
@@ -274,11 +310,11 @@ export class SiWizardComponent { | |||||
| } | ||||||
| } | ||||||
|
|
||||||
| protected getStateClass(stepIndex: number): string { | ||||||
| protected getStateClass(stepIndex: number, canActivate: boolean): string { | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| if (stepIndex === this.index) { | ||||||
| return 'active'; | ||||||
| } | ||||||
| if (!this.canActivate(stepIndex)) { | ||||||
| if (!canActivate) { | ||||||
| return 'disabled'; | ||||||
| } | ||||||
| if (stepIndex < this.index) { | ||||||
|
|
@@ -287,32 +323,15 @@ export class SiWizardComponent { | |||||
| return ''; | ||||||
| } | ||||||
|
|
||||||
| protected getAriaDisabled(stepIndex: number): string { | ||||||
| if (!this.canActivate(stepIndex)) { | ||||||
| return 'true'; | ||||||
| } | ||||||
| return 'false'; | ||||||
| } | ||||||
|
|
||||||
| protected getAriaCurrent(stepIndex: number): string { | ||||||
| if (stepIndex === this.index) { | ||||||
| return 'step'; | ||||||
| } | ||||||
| return 'false'; | ||||||
| } | ||||||
|
|
||||||
| /** | ||||||
| * Go to the next wizard step. | ||||||
| * @param delta - optional number of steps to move forward. | ||||||
| */ | ||||||
| next(delta: number = 1): void { | ||||||
| const steps = this.steps(); | ||||||
| if (this.index === steps.length - 1) { | ||||||
| return; | ||||||
| } | ||||||
| const stepIndex = this.index + delta; | ||||||
| const nextStep = steps[stepIndex]; | ||||||
| if (this.canActivate(stepIndex)) { | ||||||
| if (stepIndex < steps.length && this.stepsMetadata()[stepIndex].canActivate) { | ||||||
| const nextStep = steps[stepIndex]; | ||||||
| this.currentStep?.next.emit(); | ||||||
| if (this.currentStep?.isNextNavigable()) { | ||||||
| this.activate(nextStep); | ||||||
|
|
@@ -347,14 +366,6 @@ export class SiWizardComponent { | |||||
| } | ||||||
| } | ||||||
|
|
||||||
| protected getState(step: SiWizardStepComponent, stepIndex: number): string { | ||||||
| if (step.failed() === true) { | ||||||
| return this.stepFailedIcon(); | ||||||
| } | ||||||
| const txtStyle = step.isActive() ? this.stepActiveIcon() : this.stepIcon(); | ||||||
| return stepIndex >= this.index ? txtStyle : this.stepCompletedIcon(); | ||||||
| } | ||||||
|
|
||||||
| private activate(step: SiWizardStepComponent): void { | ||||||
| if (this.currentStep) { | ||||||
| this.currentStep.isActive.set(false); | ||||||
|
|
||||||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
here as well I guess you can use the original function to extract value