|
4 | 4 | @typeparam TReturnValue |
5 | 5 |
|
6 | 6 | <MudDialogEx> |
7 | | - <DialogContent> |
8 | | - @if (Value is not null && editContext is not null) |
9 | | - { |
10 | | - <EditForm EditContext="@editContext"> |
11 | | - <DataAnnotationsValidator /> |
12 | | - @FormContent(Value) |
13 | | - </EditForm> |
14 | | - } |
15 | | - else if (hasError) |
16 | | - { |
17 | | - <MudAlert Severity="Severity.Error">@(errorMessage ?? "Unknown error")</MudAlert> |
18 | | - } |
19 | | - else |
20 | | - { |
21 | | - <MudProgressLinear Indeterminate="true" Color="Color.Primary" /> |
22 | | - } |
23 | | - </DialogContent> |
24 | | - <DialogActions> |
25 | | - <MudButton OnClick="OnCancelAsync_Internal" Variant="Variant.Outlined">@CancelButtonText</MudButton> |
26 | | - @if (Value is not null && editContext is not null && SubmitButtonShown) |
27 | | - { |
28 | | - <MudButton Color="Color.Primary" OnClick="OnSubmitAsync_Internal" Variant="Variant.Filled" Class="ml-4">@SubmitButtonText</MudButton> |
29 | | - } |
30 | | - </DialogActions> |
| 7 | + <DialogContent> |
| 8 | + @if (Value is not null && editContext is not null) |
| 9 | + { |
| 10 | + <EditForm EditContext="@editContext"> |
| 11 | + <DataAnnotationsValidator /> |
| 12 | + @FormContent(Value) |
| 13 | + </EditForm> |
| 14 | + } |
| 15 | + else if (hasError) |
| 16 | + { |
| 17 | + <MudAlert Severity="Severity.Error">@(errorMessage ?? "Unknown error")</MudAlert> |
| 18 | + } |
| 19 | + else |
| 20 | + { |
| 21 | + <MudProgressLinear Indeterminate="true" Color="Color.Primary" /> |
| 22 | + } |
| 23 | + </DialogContent> |
| 24 | + <DialogActions> |
| 25 | + @if (CustomDialogActions is not null) |
| 26 | + { |
| 27 | + @CustomDialogActions |
| 28 | + } |
| 29 | + <MudButton OnClick="OnCancelAsync_Internal" Variant="Variant.Outlined" Class="ml-4">@CancelButtonText</MudButton> |
| 30 | + @if (Value is not null && editContext is not null) |
| 31 | + { |
| 32 | + <MudButton Color="Color.Primary" OnClick="OnSubmitAsync_Internal" Variant="Variant.Filled" Disabled="@SubmitButtonDisabled" Class="ml-4">@SubmitButtonText</MudButton> |
| 33 | + } |
| 34 | + </DialogActions> |
31 | 35 | </MudDialogEx> |
32 | 36 |
|
33 | 37 | @code |
34 | 38 | { |
35 | | - [CascadingParameter] |
36 | | - IMudDialogInstance DialogInstance { get; set; } = default!; |
| 39 | + [CascadingParameter] |
| 40 | + IMudDialogInstance DialogInstance { get; set; } = default!; |
37 | 41 |
|
38 | | - [Parameter, EditorRequired] |
39 | | - public TValue? Value { get; set; } |
| 42 | + [Parameter, EditorRequired] |
| 43 | + public TValue? Value { get; set; } |
40 | 44 |
|
41 | | - [Parameter, EditorRequired] |
42 | | - public RenderFragment<TValue> FormContent { get; set; } = default!; |
| 45 | + [Parameter, EditorRequired] |
| 46 | + public RenderFragment<TValue> FormContent { get; set; } = default!; |
43 | 47 |
|
44 | | - [Parameter] |
45 | | - public EventCallback<EditFormLoadArgs> OnLoad { get; set; } |
| 48 | + [Parameter] |
| 49 | + public RenderFragment? CustomDialogActions { get; set; } |
46 | 50 |
|
47 | | - [Parameter] |
48 | | - public EventCallback<EditFormSubmitArgs<TValue, TReturnValue>> OnSubmit { get; set; } |
| 51 | + [Parameter] |
| 52 | + public EventCallback<EditFormLoadArgs> OnLoad { get; set; } |
49 | 53 |
|
50 | | - [Parameter] |
51 | | - public EventCallback OnCancel { get; set; } |
| 54 | + [Parameter] |
| 55 | + public EventCallback<EditFormSubmitArgs<TValue, TReturnValue>> OnSubmit { get; set; } |
52 | 56 |
|
53 | | - [Parameter] |
54 | | - public bool SubmitButtonShown { get; set; } = true; |
| 57 | + [Parameter] |
| 58 | + public EventCallback OnCancel { get; set; } |
55 | 59 |
|
56 | | - [Parameter] |
57 | | - public string SubmitButtonText { get; set; } = "Submit"; |
| 60 | + [Parameter] |
| 61 | + public bool SubmitButtonDisabled { get; set; } |
58 | 62 |
|
59 | | - [Parameter] |
60 | | - public string CancelButtonText { get; set; } = "Cancel"; |
| 63 | + [Parameter] |
| 64 | + public string SubmitButtonText { get; set; } = "Submit"; |
61 | 65 |
|
62 | | - private EditContext? editContext; |
| 66 | + [Parameter] |
| 67 | + public string CancelButtonText { get; set; } = "Cancel"; |
63 | 68 |
|
64 | | - private bool hasError; |
65 | | - private string? errorMessage; |
| 69 | + private EditContext? editContext; |
66 | 70 |
|
67 | | - public bool Validate() |
68 | | - { |
69 | | - ArgumentNullException.ThrowIfNull(editContext); |
| 71 | + private bool hasError; |
| 72 | + private string? errorMessage; |
70 | 73 |
|
71 | | - return editContext.Validate(); |
72 | | - } |
| 74 | + public bool Validate() |
| 75 | + { |
| 76 | + ArgumentNullException.ThrowIfNull(editContext); |
73 | 77 |
|
74 | | - protected override async Task OnInitializedAsync() |
75 | | - { |
76 | | - if (!OnLoad.HasDelegate) |
77 | | - return; |
| 78 | + return editContext.Validate(); |
| 79 | + } |
78 | 80 |
|
79 | | - EditFormLoadArgs args = new(); |
80 | | - await OnLoad.InvokeAsync(args); |
| 81 | + protected override async Task OnInitializedAsync() |
| 82 | + { |
| 83 | + if (!OnLoad.HasDelegate) |
| 84 | + return; |
81 | 85 |
|
82 | | - if (!args.HasError) |
83 | | - return; |
| 86 | + EditFormLoadArgs args = new(); |
| 87 | + await OnLoad.InvokeAsync(args); |
84 | 88 |
|
85 | | - hasError = true; |
86 | | - errorMessage = args.ErrorMessage; |
87 | | - } |
| 89 | + if (!args.HasError) |
| 90 | + return; |
88 | 91 |
|
89 | | - // todo check how this behaves when a parameter is updated!!! |
90 | | - protected override void OnParametersSet() |
91 | | - { |
92 | | - if (Value is null) |
93 | | - return; |
| 92 | + hasError = true; |
| 93 | + errorMessage = args.ErrorMessage; |
| 94 | + } |
94 | 95 |
|
95 | | - editContext = new EditContext(Value); |
96 | | - } |
| 96 | + // todo check how this behaves when a parameter is updated!!! |
| 97 | + protected override void OnParametersSet() |
| 98 | + { |
| 99 | + if (Value is null) |
| 100 | + return; |
97 | 101 |
|
98 | | - private async Task OnSubmitAsync_Internal() |
99 | | - { |
100 | | - ArgumentNullException.ThrowIfNull(Value); |
101 | | - ArgumentNullException.ThrowIfNull(editContext); |
| 102 | + editContext = new EditContext(Value); |
| 103 | + } |
102 | 104 |
|
103 | | - EditFormSubmitArgs<TValue, TReturnValue> args = new(Value, editContext); |
| 105 | + private async Task OnSubmitAsync_Internal() |
| 106 | + { |
| 107 | + ArgumentNullException.ThrowIfNull(Value); |
| 108 | + ArgumentNullException.ThrowIfNull(editContext); |
104 | 109 |
|
105 | | - if (OnSubmit.HasDelegate) |
106 | | - await OnSubmit.InvokeAsync(args); |
| 110 | + EditFormSubmitArgs<TValue, TReturnValue> args = new(Value, editContext); |
107 | 111 |
|
108 | | - if (args.Cancel) |
109 | | - return; |
| 112 | + if (OnSubmit.HasDelegate) |
| 113 | + await OnSubmit.InvokeAsync(args); |
110 | 114 |
|
111 | | - DialogInstance.Close(DialogResult.Ok(args.ReturnValue)); |
112 | | - } |
| 115 | + if (args.Cancel) |
| 116 | + return; |
113 | 117 |
|
114 | | - private async Task OnCancelAsync_Internal() |
115 | | - { |
116 | | - if (OnCancel.HasDelegate) |
117 | | - await OnCancel.InvokeAsync(); |
| 118 | + DialogInstance.Close(DialogResult.Ok(args.ReturnValue)); |
| 119 | + } |
118 | 120 |
|
119 | | - DialogInstance.Cancel(); |
120 | | - } |
| 121 | + private async Task OnCancelAsync_Internal() |
| 122 | + { |
| 123 | + if (OnCancel.HasDelegate) |
| 124 | + await OnCancel.InvokeAsync(); |
| 125 | + |
| 126 | + DialogInstance.Cancel(); |
| 127 | + } |
121 | 128 | } |
0 commit comments