Skip to content

Commit 51bbe95

Browse files
authored
Stepper: TargetIndex Parameter While PreventStepChange (#339)
* Stepper Target Index * Fix Build * Fix Tests * Fix One More Test
1 parent 19777b1 commit 51bbe95

File tree

4 files changed

+29
-18
lines changed

4 files changed

+29
-18
lines changed

CodeBeam.MudBlazor.Extensions.UnitTests.Viewer/TestComponents/SelectExtended/StepperTest1.razor

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
<MudStepper @ref="_stepper" ContentStyle="min-height: 400px" Linear="_linear" Vertical="_vertical" Color="_color" Variant="_variant"
1111
DisableAnimation="_disableAnimation" DisablePreviousButton="_disablePreviousButton" DisableNextButton="_disableNextButton"
1212
DisableSkipButton="_disableSkipButton" DisableStepResultIndicator="_disableStepResultIndicator" HeaderTextView="_headerTextView"
13-
PreventStepChangeAsync="new Func<StepChangeDirection, Task<bool>>(CheckChange)" LocalizedStrings="GetLocalizedStrings()"
13+
PreventStepChangeAsync="new Func<StepChangeDirection, int, Task<bool>>(CheckChange)" LocalizedStrings="GetLocalizedStrings()"
1414
MobileView="_mobileView" IconActionButtons="_iconActionButtons" Loading="_loading">
1515
<StaticContent>
1616
@if (_showStaticContent)
@@ -126,7 +126,7 @@
126126
[Parameter]
127127
public int CheckChangeCount { get; set; }
128128

129-
private async Task<bool> CheckChange(StepChangeDirection direction)
129+
private async Task<bool> CheckChange(StepChangeDirection direction, int targetIndex)
130130
{
131131
CheckChangeCount += 1;
132132
if (_checkValidationBeforeComplete == true)

CodeBeam.MudBlazor.Extensions.UnitTests/Components/StepperTests.cs

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,14 @@ public async Task StepperPreventStepChangeDirectionIsForwardWhenCompletingStepOn
2222
{
2323
// Arrange
2424
var lastStepChangeDirection = StepChangeDirection.None;
25+
int _targetIndex = 0;
2526
var stepper = Context.RenderComponent<MudStepper>(
2627
ComponentParameterFactory.Parameter(
2728
nameof(MudStepper.PreventStepChangeAsync),
28-
new Func<StepChangeDirection, Task<bool>>((direction) =>
29+
new Func<StepChangeDirection, int, Task<bool>>((direction, targetIndex) =>
2930
{
3031
lastStepChangeDirection = direction;
32+
_targetIndex = targetIndex;
3133
return Task.FromResult(false);
3234
})
3335
)
@@ -48,12 +50,14 @@ public async Task StepperPreventStepChangeDirectionIsForwardWhenSkippingStepOneO
4850
{
4951
// Arrange
5052
var lastStepChangeDirection = StepChangeDirection.None;
53+
int _targetIndex = 0;
5154
var stepper = Context.RenderComponent<MudStepper>(
5255
ComponentParameterFactory.Parameter(
5356
nameof(MudStepper.PreventStepChangeAsync),
54-
new Func<StepChangeDirection, Task<bool>>((direction) =>
57+
new Func<StepChangeDirection, int, Task<bool>>((direction, targetIndex) =>
5558
{
5659
lastStepChangeDirection = direction;
60+
_targetIndex = targetIndex;
5761
return Task.FromResult(false);
5862
})
5963
)
@@ -74,12 +78,14 @@ public async Task StepperPreventStepChangeDirectionIsForwardWhenChangingFromStep
7478
{
7579
// Arrange
7680
var lastStepChangeDirection = StepChangeDirection.None;
81+
int _targetIndex = 0;
7782
var stepper = Context.RenderComponent<MudStepper>(
7883
ComponentParameterFactory.Parameter(
7984
nameof(MudStepper.PreventStepChangeAsync),
80-
new Func<StepChangeDirection, Task<bool>>((direction) =>
85+
new Func<StepChangeDirection, int, Task<bool>>((direction, targetIndex) =>
8186
{
8287
lastStepChangeDirection = direction;
88+
_targetIndex = targetIndex;
8389
return Task.FromResult(false);
8490
})
8591
)
@@ -104,12 +110,14 @@ public async Task StepperPreventStepChangeDirectionIsBackwardWhenChangingFromSte
104110
{
105111
// Arrange
106112
var lastStepChangeDirection = StepChangeDirection.None;
113+
int _targetIndex = 0;
107114
var stepper = Context.RenderComponent<MudStepper>(
108115
ComponentParameterFactory.Parameter(
109116
nameof(MudStepper.PreventStepChangeAsync),
110-
new Func<StepChangeDirection, Task<bool>>((direction) =>
117+
new Func<StepChangeDirection, int, Task<bool>>((direction, targetIndex) =>
111118
{
112119
lastStepChangeDirection = direction;
120+
_targetIndex = targetIndex;
113121
return Task.FromResult(false);
114122
})
115123
)
@@ -137,7 +145,7 @@ public async Task StepperPreventStepChangeDirectionIsNoneWhenChangingToTheSameSt
137145
var stepper = Context.RenderComponent<MudStepper>(
138146
ComponentParameterFactory.Parameter(
139147
nameof(MudStepper.PreventStepChangeAsync),
140-
new Func<StepChangeDirection, Task<bool>>((direction) =>
148+
new Func<StepChangeDirection, int, Task<bool>>((direction, targetIndex) =>
141149
{
142150
lastStepChangeDirection = direction;
143151
return Task.FromResult(false);
@@ -164,7 +172,7 @@ public async Task StepperPreventStepChangeIsInvokedWhenCompletingActiveStepTest(
164172
var stepper = Context.RenderComponent<MudStepper>(
165173
ComponentParameterFactory.Parameter(
166174
nameof(MudStepper.PreventStepChangeAsync),
167-
new Func<StepChangeDirection, Task<bool>>((direction) =>
175+
new Func<StepChangeDirection, int, Task<bool>>((direction, targetIndex) =>
168176
{
169177
preventStepChangeWasInvoked = true;
170178
return Task.FromResult(false);
@@ -194,7 +202,7 @@ public async Task StepperPreventStepChangeIsInvokedWhenSkippingActiveStepTest()
194202
var stepper = Context.RenderComponent<MudStepper>(
195203
ComponentParameterFactory.Parameter(
196204
nameof(MudStepper.PreventStepChangeAsync),
197-
new Func<StepChangeDirection, Task<bool>>((direction) =>
205+
new Func<StepChangeDirection, int, Task<bool>>((direction, targetIndex) =>
198206
{
199207
preventStepChangeWasInvoked = true;
200208
return Task.FromResult(false);
@@ -224,7 +232,7 @@ public async Task StepperPreventStepChangeIsNotInvokedWhenCompletingNonActiveSte
224232
var stepper = Context.RenderComponent<MudStepper>(
225233
ComponentParameterFactory.Parameter(
226234
nameof(MudStepper.PreventStepChangeAsync),
227-
new Func<StepChangeDirection, Task<bool>>((direction) =>
235+
new Func<StepChangeDirection, int, Task<bool>>((direction, targetIndex) =>
228236
{
229237
preventStepChangeWasInvoked = true;
230238
return Task.FromResult(false);
@@ -254,7 +262,7 @@ public async Task StepperPreventStepChangeIsNotInvokedWhenSkippingNonActiveStepT
254262
var stepper = Context.RenderComponent<MudStepper>(
255263
ComponentParameterFactory.Parameter(
256264
nameof(MudStepper.PreventStepChangeAsync),
257-
new Func<StepChangeDirection, Task<bool>>((direction) =>
265+
new Func<StepChangeDirection, int, Task<bool>>((direction, targetIndex) =>
258266
{
259267
preventStepChangeWasInvoked = true;
260268
return Task.FromResult(false);

CodeBeam.MudBlazor.Extensions/Components/Stepper/MudStepper.razor.cs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -258,8 +258,11 @@ internal int ActiveIndex
258258
[Parameter]
259259
public Func<StepChangeDirection, bool> PreventStepChange { get; set; }
260260

261+
/// <summary>
262+
/// Runs a task to prevent step change. Has change direction (backwards or forwards) and target index and returns a bool value.
263+
/// </summary>
261264
[Parameter]
262-
public Func<StepChangeDirection, Task<bool>> PreventStepChangeAsync { get; set; }
265+
public Func<StepChangeDirection, int, Task<bool>> PreventStepChangeAsync { get; set; }
263266

264267
List<MudStep> _steps = new();
265268
List<MudStep> _allSteps = new();
@@ -318,7 +321,7 @@ public async Task SetActiveIndex(int count, bool firstCompleted = false, bool sk
318321

319322
if (skipPreventProcess == false && PreventStepChangeAsync != null)
320323
{
321-
var result = await PreventStepChangeAsync.Invoke(stepChangeDirection);
324+
var result = await PreventStepChangeAsync.Invoke(stepChangeDirection, ActiveIndex + count);
322325
if (result == true)
323326
{
324327
return;
@@ -377,7 +380,7 @@ public async Task SetActiveStepByIndex(int index, bool firstCompleted = false, b
377380

378381
if (skipPreventProcess == false && PreventStepChangeAsync != null)
379382
{
380-
var result = await PreventStepChangeAsync.Invoke(stepChangeDirection);
383+
var result = await PreventStepChangeAsync.Invoke(stepChangeDirection, index);
381384
if (result == true)
382385
{
383386
return;
@@ -416,7 +419,7 @@ public async Task CompleteStep(int index, bool moveToNextStep = true)
416419

417420
if (PreventStepChangeAsync != null)
418421
{
419-
var result = await PreventStepChangeAsync.Invoke(stepChangeDirection);
422+
var result = await PreventStepChangeAsync.Invoke(stepChangeDirection, index + 1);
420423
if (result == true)
421424
{
422425
return;
@@ -448,7 +451,7 @@ public async Task SkipStep(int index, bool moveToNextStep = true)
448451

449452
if (PreventStepChangeAsync != null)
450453
{
451-
var result = await PreventStepChangeAsync.Invoke(stepChangeDirection);
454+
var result = await PreventStepChangeAsync.Invoke(stepChangeDirection, index + 1);
452455
if (result == true)
453456
{
454457
return;

ComponentViewer.Docs/Pages/Examples/StepperExample1.razor

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<MudStepper @ref="_stepper" Class="mud-width-full" ContentStyle="min-height: 400px" Linear="_linear" Vertical="_vertical" Color="_color" Variant="_variant"
99
DisableAnimation="_disableAnimation" DisablePreviousButton="_disablePreviousButton" DisableNextButton="_disableNextButton"
1010
DisableSkipButton="_disableSkipButton" DisableStepResultIndicator="_disableStepResultIndicator" HeaderBadgeView="_headerBadgeView"
11-
HeaderTextView="_headerTextView" PreventStepChangeAsync="new Func<StepChangeDirection, Task<bool>>(CheckChange)" LocalizedStrings="GetLocalizedStrings()"
11+
HeaderTextView="_headerTextView" PreventStepChangeAsync="new Func<StepChangeDirection, int, Task<bool>>(CheckChange)" LocalizedStrings="GetLocalizedStrings()"
1212
MobileView="_mobileView" IconActionButtons="_iconActionButtons" Loading="_loading" HeaderSize="_headerSize" HeaderIcon="false">
1313
<StaticContent>
1414
@if (_showStaticContent)
@@ -147,7 +147,7 @@
147147
bool _vertical = false;
148148
Size _headerSize = Size.Medium;
149149

150-
private async Task<bool> CheckChange(StepChangeDirection direction)
150+
private async Task<bool> CheckChange(StepChangeDirection direction, int targetIndex)
151151
{
152152
if (_checkValidationBeforeComplete == true)
153153
{

0 commit comments

Comments
 (0)