-
Notifications
You must be signed in to change notification settings - Fork 0
/
DxModalPopup.cs
51 lines (44 loc) · 2.22 KB
/
DxModalPopup.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
using DevExpress.Blazor;
using Microsoft.AspNetCore.Components;
namespace DynamicPopup.Components {
public partial class DxModalPopup : DxPopup {
TaskCompletionSource? popupCompletion;
[Parameter]
public RenderFragment BodyContent { get; set; } = new(builder => { });
protected void CreateBodyContent(Type? componentType, Dictionary<string, object>? attributes) {
BodyContent = new RenderFragment(builder => {
if (componentType != null) {
int sequence = 0;
builder.OpenComponent<CascadingValue<DxModalPopup>>(sequence++);
builder.AddMultipleAttributes(sequence++, new Dictionary<string, object>() {
{ nameof(CascadingValue<DxModalPopup>.Value), this },
{ nameof(CascadingValue<DxModalPopup>.IsFixed), true },
{ nameof(CascadingValue<DxModalPopup>.ChildContent), new RenderFragment(childBuilder => {
childBuilder.OpenComponent(0, componentType);
if (attributes != null) {
childBuilder.AddMultipleAttributes(1, attributes);
}
childBuilder.CloseComponent();
})}
});
builder.CloseComponent();
}
});
}
public DxModalPopup() {
this.BodyContentTemplate = new RenderFragment<IPopupElementInfo>(target => BodyContent);
this.Closed = EventCallback.Factory.Create<PopupClosedEventArgs>(this, (e) => {
OnClosed(e);
});
}
protected void OnClosed(PopupClosedEventArgs e) {
popupCompletion?.TrySetResult();
}
public async Task ShowModal<TComponent>(Dictionary<string, object>? attributes = null) where TComponent : ComponentBase {
CreateBodyContent(typeof(TComponent), attributes);
popupCompletion = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
await modalService.ShowModal<TComponent>(attributes);
await popupCompletion.Task;
}
}
}