Skip to content

Commit 158d588

Browse files
authored
Stepper: PreventStepChangeAsync (#166)
* Stepper PreventStepChangeAsync * Fix Null Reference Exceptions * Loading and StaticContent Parameters
1 parent 2449441 commit 158d588

File tree

3 files changed

+76
-5
lines changed

3 files changed

+76
-5
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,11 @@
5353

5454

5555
</MudStack>
56+
@if (Loading)
57+
{
58+
<MudProgressLinear Indeterminate="true" Color="@Color" />
59+
}
60+
@StaticContent
5661
<MudStack Class="mud-width-full" Justify="Justify.SpaceBetween">
5762
<div class="@ContentClassname" style="@ContentStyle">
5863
@ChildContent

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

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,18 @@ protected string GetDashClassname(MudStep step)
103103
[Parameter]
104104
public bool MobileView { get; set; }
105105

106+
/// <summary>
107+
/// If true, a linear loading indicator shows under the header.
108+
/// </summary>
109+
[Parameter]
110+
public bool Loading { get; set; }
111+
112+
/// <summary>
113+
/// A static content that always show with all steps.
114+
/// </summary>
115+
[Parameter]
116+
public RenderFragment StaticContent { get; set; }
117+
106118
/// <summary>
107119
/// If true, action buttons have icons instead of text to gain more space.
108120
/// </summary>
@@ -156,9 +168,13 @@ protected string GetDashClassname(MudStep step)
156168
[Parameter]
157169
public EventCallback<int> ActiveStepChanged { get; set; }
158170

171+
[Obsolete("Use PreventStepChangeAsync instead.")]
159172
[Parameter]
160173
public Func<StepChangeDirection, bool> PreventStepChange { get; set; }
161174

175+
[Parameter]
176+
public Func<StepChangeDirection, Task<bool>> PreventStepChangeAsync { get; set; }
177+
162178
List<MudStep> _steps = new();
163179
List<MudStep> _allSteps = new();
164180
public List<MudStep> Steps
@@ -219,6 +235,16 @@ public async Task SetActiveIndex(int count, bool firstCompleted = false, bool sk
219235
return;
220236
}
221237

238+
if (PreventStepChangeAsync != null)
239+
{
240+
var result = await PreventStepChangeAsync.Invoke(stepChangeDirection);
241+
if (result == true)
242+
{
243+
return;
244+
}
245+
}
246+
247+
222248
int backupActiveIndex = ActiveIndex;
223249
if (_animate != null)
224250
{
@@ -297,6 +323,15 @@ public async Task CompleteStep(int index, bool moveToNextStep = true)
297323
{
298324
return;
299325
}
326+
327+
if (PreventStepChangeAsync != null)
328+
{
329+
var result = await PreventStepChangeAsync.Invoke(stepChangeDirection);
330+
if (result == true)
331+
{
332+
return;
333+
}
334+
}
300335
}
301336

302337
Steps[index].SetStatus(StepStatus.Completed);
@@ -320,6 +355,15 @@ public async Task SkipStep(int index, bool moveToNextStep = true)
320355
{
321356
return;
322357
}
358+
359+
if (PreventStepChangeAsync != null)
360+
{
361+
var result = await PreventStepChangeAsync.Invoke(stepChangeDirection);
362+
if (result == true)
363+
{
364+
return;
365+
}
366+
}
323367
}
324368

325369
Steps[index].SetStatus(StepStatus.Skipped);

ComponentViewer.Docs/Pages/Examples/StepperExample1.razor

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,17 @@
88
<MudStepper @ref="_stepper" ContentStyle="min-height: 400px" Linear="_linear" Vertical="_vertical" Color="_color" Variant="_variant"
99
DisableAnimation="_disableAnimation" DisablePreviousButton="_disablePreviousButton" DisableNextButton="_disableNextButton"
1010
DisableSkipButton="_disableSkipButton" DisableStepResultIndicator="_disableStepResultIndicator" HeaderTextView="_headerTextView"
11-
PreventStepChange="new Func<StepChangeDirection, bool>(CheckChange)" LocalizedStrings="GetLocalizedStrings()"
12-
MobileView="_mobileView" IconActionButtons="_iconActionButtons">
11+
PreventStepChangeAsync="new Func<StepChangeDirection, Task<bool>>(CheckChange)" LocalizedStrings="GetLocalizedStrings()"
12+
MobileView="_mobileView" IconActionButtons="_iconActionButtons" Loading="_loading">
13+
<StaticContent>
14+
@if (_showStaticContent)
15+
{
16+
<MudStack Row="true" AlignItems="AlignItems.Center">
17+
<MudAvatar Color="_color">ST</MudAvatar>
18+
<MudText>This is a static content which shows with each step.</MudText>
19+
</MudStack>
20+
}
21+
</StaticContent>
1322
<ChildContent>
1423
<MudStep Title="Customer Info" StatusChanged="StatusChanged">
1524
<MudForm @ref="_form">
@@ -71,6 +80,7 @@
7180
<MudSwitchM3 @bind-Checked="_addResultStep" Color="Color.Primary" Label="Has Result Step" />
7281
<MudSwitchM3 @bind-Checked="_checkValidationBeforeComplete" Color="Color.Primary" Label="Check Validation Before Complete Step" />
7382
<MudSwitchM3 @bind-Checked="_customLocalization" Color="Color.Primary" Label="Custom Localization (German)" />
83+
<MudSwitchM3 @bind-Checked="_showStaticContent" Color="Color.Primary" Label="Show Some Static Content" />
7484
<MudSelect @bind-Value="_variant" Variant="Variant.Outlined" Label="Variant">
7585
@foreach (Variant item in Enum.GetValues<Variant>())
7686
{
@@ -113,8 +123,10 @@
113123
bool _customLocalization = false;
114124
Color _color = Color.Primary;
115125
int _activeIndex = 0;
126+
bool _loading;
127+
bool _showStaticContent = false;
116128

117-
private bool CheckChange(StepChangeDirection direction)
129+
private async Task<bool> CheckChange(StepChangeDirection direction)
118130
{
119131
if (_checkValidationBeforeComplete == true)
120132
{
@@ -125,12 +137,22 @@
125137
}
126138
if (_stepper.GetActiveIndex() == 0)
127139
{
128-
_form.Validate();
140+
_loading = true;
141+
StateHasChanged();
142+
await Task.Delay(1000);
143+
await _form.Validate();
144+
_loading = false;
145+
StateHasChanged();
129146
return !_form.IsValid;
130147
}
131148
else if (_stepper.GetActiveIndex() == 2)
132149
{
133-
_form2.Validate();
150+
_loading = true;
151+
StateHasChanged();
152+
await Task.Delay(1000);
153+
await _form2.Validate();
154+
_loading = false;
155+
StateHasChanged();
134156
return !_form2.IsValid;
135157
}
136158
else

0 commit comments

Comments
 (0)