Skip to content

Commit f8e6782

Browse files
Merge pull request #20 from stavroskasidis/release/2.3.0
Expose dialog options as cascading parameter when using ComponentAsDialog
2 parents 9783a57 + 992d48a commit f8e6782

7 files changed

Lines changed: 170 additions & 25 deletions

File tree

BlazorDialog/BlazorDialog.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
<Copyright />
1313
<PackageTags>blazor blazor-component blazor-dialog dialog modal blazor-modal blazordialog blazormodaldialog blazormodal razor razor-components razorcomponents</PackageTags>
1414
<VersionSuffix>$(VersionSuffix)</VersionSuffix>
15-
<Version>2.2.0</Version>
15+
<Version>2.3.0</Version>
1616
<Version Condition=" '$(VersionSuffix)' != '' ">$(Version)-$(VersionSuffix)</Version>
1717
<Product>BlazorDialog</Product>
1818
</PropertyGroup>
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
@namespace BlazorDialog
2+
@implements IDisposable
3+
<CascadingValue Value="ComponentDialog.Options" IsFixed="true" Name="DialogOptions">
4+
@if (!ComponentDialog.Options.IsCustom)
5+
{
6+
<Dialog Id="@Key" Animation="ComponentDialog.Options.Animation" BaseZIndex="ComponentDialog.Options.BaseZIndex"
7+
Centered="ComponentDialog.Options.Centered" CssClass="@ComponentDialog.Options.CssClass" Size="ComponentDialog.Options.Size"
8+
OnAfterRender="EventUtil.AsNonRenderingEventCallback((bool firstLoad) => OnAfterDialogRender(firstLoad))"
9+
OnAfterHide="ComponentDialog.Options.OnAfterHide" OnAfterShow="ComponentDialog.Options.OnAfterShow"
10+
OnBeforeHide="ComponentDialog.Options.OnBeforeHide" OnBeforeShow="ComponentDialog.Options.OnBeforeShow">
11+
<DynamicComponent Type="ComponentDialog.Options.ComponentType" Parameters="ComponentDialog.Options.Parameters"></DynamicComponent>
12+
</Dialog>
13+
}
14+
else
15+
{
16+
<Dialog Id="@Key" IsCustom="ComponentDialog.Options.IsCustom"
17+
OnAfterRender="EventUtil.AsNonRenderingEventCallback((bool firstLoad) => OnAfterDialogRender(firstLoad))"
18+
OnAfterHide="ComponentDialog.Options.OnAfterHide" OnAfterShow="ComponentDialog.Options.OnAfterShow"
19+
OnBeforeHide="ComponentDialog.Options.OnBeforeHide" OnBeforeShow="ComponentDialog.Options.OnBeforeShow">
20+
<DynamicComponent Type="ComponentDialog.Options.ComponentType" Parameters="ComponentDialog.Options.Parameters"></DynamicComponent>
21+
</Dialog>
22+
}
23+
</CascadingValue>
24+
25+
@code {
26+
[Parameter, EditorRequired] public ComponentDialog ComponentDialog { get; set; }
27+
[Parameter, EditorRequired] public string Key { get; set; }
28+
29+
protected override void OnInitialized()
30+
{
31+
ComponentDialog.Options.OnOptionsChanged += Refresh;
32+
}
33+
34+
protected async Task Refresh()
35+
{
36+
await InvokeAsync(StateHasChanged);
37+
}
38+
39+
protected async Task OnAfterDialogRender(bool firstLoad)
40+
{
41+
if (firstLoad)
42+
{
43+
ComponentDialog.RenderTaskCompletionSource.SetResult();
44+
}
45+
if (ComponentDialog.Options.OnAfterRender != null)
46+
{
47+
await ComponentDialog.Options.OnAfterRender(firstLoad);
48+
}
49+
}
50+
51+
public void Dispose()
52+
{
53+
ComponentDialog.Options.OnOptionsChanged -= Refresh;
54+
}
55+
}

BlazorDialog/Components/DialogOutput.razor

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,7 @@
33
@implements IDisposable
44
@foreach (var dialog in store.GetComponentsAsDialogs())
55
{
6-
if (!dialog.Value.Options.IsCustom)
7-
{
8-
<Dialog Id="@dialog.Key" @key="dialog.Key" Animation="dialog.Value.Options.Animation" BaseZIndex="dialog.Value.Options.BaseZIndex"
9-
Centered="dialog.Value.Options.Centered" CssClass="@dialog.Value.Options.CssClass" Size="dialog.Value.Options.Size"
10-
OnAfterRender="EventUtil.AsNonRenderingEventCallback((bool firstLoad) => OnAfterDialogRender(firstLoad, dialog))">
11-
<DynamicComponent Type="dialog.Value.Options.ComponentType" Parameters="dialog.Value.Options.Parameters"></DynamicComponent>
12-
</Dialog>
13-
}
14-
else
15-
{
16-
<Dialog Id="@dialog.Key" @key="dialog.Key" IsCustom="dialog.Value.Options.IsCustom" OnAfterRender="EventUtil.AsNonRenderingEventCallback((bool firstLoad) => OnAfterDialogRender(firstLoad, dialog))">
17-
<DynamicComponent Type="dialog.Value.Options.ComponentType" Parameters="dialog.Value.Options.Parameters"></DynamicComponent>
18-
</Dialog>
19-
}
6+
<ComponentAsDialogContainer Key="@dialog.Key" ComponentDialog="dialog.Value" @key="dialog.Key" />
207
}
218

229
@code {
@@ -34,12 +21,4 @@
3421
{
3522
await InvokeAsync(StateHasChanged);
3623
}
37-
38-
protected async Task OnAfterDialogRender(bool firstLoad, KeyValuePair<string,ComponentDialog> dialog)
39-
{
40-
if (firstLoad)
41-
{
42-
dialog.Value.RenderTaskCompletionSource.SetResult();
43-
}
44-
}
45-
}
24+
}

BlazorDialog/IBlazorDialogStore.cs

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,85 @@ public ComponentAsDialogOptions(Type componentType)
5252
/// Allows you to set the dialog size. Ignored when the dialog is <see cref="Dialog.IsCustom" />.
5353
/// </summary>
5454
public DialogSize Size { get; set; } = DialogSize.Normal;
55+
56+
/// <summary>
57+
/// An event that is triggered before the dialog appears.
58+
/// </summary>
59+
public Func<DialogBeforeShowEventArgs, Task>? OnBeforeShow { get; set; }
60+
61+
/// <summary>
62+
/// An event that is triggered after the dialog appears.
63+
/// </summary>
64+
public Func<DialogAfterShowEventArgs, Task>? OnAfterShow { get; set; }
65+
66+
/// <summary>
67+
/// An event that is triggered before the dialog hides.
68+
/// </summary>
69+
public Func<DialogBeforeHideEventArgs, Task>? OnBeforeHide { get; protected set; }
70+
71+
/// <summary>
72+
/// An event that is triggered after the dialog hides.
73+
/// </summary>
74+
public Func<DialogAfterHideEventArgs, Task>? OnAfterHide { get; protected set; }
75+
76+
public Func<bool, Task>? OnAfterRender { get; protected set; }
77+
78+
internal event Func<Task> OnOptionsChanged;
79+
80+
///// <summary>
81+
///// An event that is triggered before the dialog appears.
82+
///// </summary>
83+
//public async Task SetOnBeforeShow(Func<DialogBeforeShowEventArgs, Task> func)
84+
//{
85+
// OnBeforeShow = func;
86+
// await RefreshDialog();
87+
//}
88+
89+
///// <summary>
90+
///// An event that is triggered after the dialog appears.
91+
///// </summary>
92+
//public async Task SetOnAfterShow(Func<DialogAfterShowEventArgs, Task> func)
93+
//{
94+
// OnAfterShow = func;
95+
// await RefreshDialog();
96+
//}
97+
98+
/// <summary>
99+
/// An event that is triggered before the dialog hides.
100+
/// </summary>
101+
public async Task SetOnBeforeHide(Func<DialogBeforeHideEventArgs, Task> func)
102+
{
103+
OnBeforeHide = func;
104+
await RefreshDialog();
105+
}
106+
107+
/// <summary>
108+
/// An event that is triggered after the dialog hides.
109+
/// </summary>
110+
public async Task SetOnAfterHide(Func<DialogAfterHideEventArgs, Task> func)
111+
{
112+
OnAfterHide = func;
113+
await RefreshDialog();
114+
}
115+
116+
/// <summary>
117+
/// An event that is triggered after the dialog hides.
118+
/// </summary>
119+
public async Task SetOnAfterRender(Func<bool, Task> func)
120+
{
121+
OnAfterRender = func;
122+
await RefreshDialog();
123+
}
124+
125+
/// <summary>
126+
/// Use to redraw the dialog if any options are changed.
127+
/// </summary>
128+
/// <returns></returns>
129+
public async Task RefreshDialog()
130+
{
131+
var result = OnOptionsChanged?.Invoke();
132+
if (result != null) await result;
133+
}
55134
}
56135

57136
public class ComponentDialog

README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,13 @@ Make sure that there is a call to `app.UseStaticFiles();` in your server project
8181
</details>
8282
8383
## Release Notes
84-
<details open="open"><summary>2.2</summary>
84+
<details open="open"><summary>2.3</summary>
85+
86+
>- Expose dialog options as cascading parameter when using ComponentAsDialog.
87+
</details>
88+
89+
90+
<details><summary>2.2</summary>
8591

8692
>- Fix for showing the same dialog with the dialog service more than once in the same async function.
8793
>- Upgrade MS packages dependecy to 6.0.26

TestApps/BlazorDialog.TestAppsCommon/ComponentDialog.razor

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
@code{
3939
[Parameter] public string Input { get; set; }
4040
[CascadingParameter(Name = "ParentDialog")] Dialog Dialog { get; set; }
41+
[CascadingParameter(Name = "DialogOptions")] ComponentAsDialogOptions DialogOptions { get; set; }
4142

4243
private string dialogResult;
4344
protected async Task ShowInputDialog()
@@ -50,4 +51,20 @@
5051
}
5152
});
5253
}
54+
55+
protected override async Task OnInitializedAsync()
56+
{
57+
await DialogOptions.SetOnAfterRender(async (e) =>
58+
{
59+
Console.WriteLine("OnAfterRender");
60+
});
61+
await DialogOptions.SetOnAfterHide(async (e) =>
62+
{
63+
Console.WriteLine("OnAfterHide");
64+
});
65+
await DialogOptions.SetOnBeforeHide(async (e) =>
66+
{
67+
Console.WriteLine("OnBeforeHide");
68+
});
69+
}
5370
}

TestApps/BlazorDialog.TestAppsCommon/ComponentsAsDialogsCommon.razor

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,15 @@
4747
Parameters = new()
4848
{
4949
{ nameof(ComponentDialog.Input), "(Simple Dialog) Are you sure?" }
50+
},
51+
52+
OnBeforeShow = async (e) =>
53+
{
54+
Console.WriteLine("OnBeforeShow");
55+
},
56+
OnAfterShow = async (e) =>
57+
{
58+
Console.WriteLine("OnAfterShow");
5059
}
5160
});
5261
}

0 commit comments

Comments
 (0)