|
28 | 28 |
|
29 | 29 | @code{ |
30 | 30 |
|
31 | | - [Parameter] public RenderFragment ChildContent { get; set; } |
| 31 | + [Parameter] public RenderFragment ChildContent { get; set; } = null!; |
32 | 32 |
|
33 | 33 | /// <summary> |
34 | 34 | /// The unique idetifier of the dialog. This field is required when using the dialog as a service. |
35 | 35 | /// </summary> |
36 | | - [Parameter] public string Id { get; set; } |
| 36 | + [Parameter] public string? Id { get; set; } |
37 | 37 |
|
38 | 38 | /// <summary> |
39 | 39 | /// The base z-index of the dialog when shown. Default: 1050 |
|
44 | 44 | /// Use this to show/hide the dialog as a simple component. |
45 | 45 | /// </summary> |
46 | 46 | [Parameter] public bool? IsShowing { get; set; } |
| 47 | + |
| 48 | + /// <summary> |
| 49 | + /// Used for two-way binding of the <see cref="IsShowing" /> parameter. |
| 50 | + /// </summary> |
| 51 | + [Parameter] public EventCallback<bool?> IsShowingChanged { get; set; } |
47 | 52 | private bool isShowing; |
48 | 53 |
|
49 | 54 | /// <summary> |
|
94 | 99 | /// <summary> |
95 | 100 | /// Adds a custom css class to the wrapper of the dialog. |
96 | 101 | /// </summary> |
97 | | - [Parameter] public string CssClass { get; set; } |
| 102 | + [Parameter] public string? CssClass { get; set; } |
98 | 103 |
|
99 | 104 | /// <summary> |
100 | 105 | /// An event that is triggered after the dialog renders. |
|
118 | 123 |
|
119 | 124 |
|
120 | 125 | private bool? _forcedPreventNavigation; |
121 | | - protected TaskCompletionSource<object> taskCompletionSource; |
122 | | - internal Action<object> OnDialogHide; |
| 126 | + protected TaskCompletionSource<object?>? taskCompletionSource; |
| 127 | + internal Action<object?> OnDialogHide = null!; |
123 | 128 | private DotNetObjectReference<Dialog>? dotNetObjectReference; |
124 | 129 |
|
125 | | - protected void NotifyDialogHidden(object result) => OnDialogHide?.Invoke(result); |
| 130 | + protected async Task NotifyDialogHidden(object? result) |
| 131 | + { |
| 132 | + OnDialogHide?.Invoke(result); |
| 133 | + if(IsShowingChanged.HasDelegate) |
| 134 | + { |
| 135 | + await IsShowingChanged.InvokeAsync(false); |
| 136 | + } |
| 137 | + } |
126 | 138 |
|
127 | 139 | protected string ContentWrapperCssClass |
128 | 140 | { |
|
175 | 187 | { |
176 | 188 | dialogStore.Register(this); |
177 | 189 | } |
178 | | - OnDialogHide = (object result) => |
| 190 | + OnDialogHide = (object? result) => |
179 | 191 | { |
180 | 192 | if (taskCompletionSource != null) |
181 | 193 | { |
|
219 | 231 | locationChangingHandler.RemoveLocationChangingHandler(this); |
220 | 232 | if (OnBeforeHide.HasDelegate) |
221 | 233 | { |
222 | | - var args = new DialogBeforeHideEventArgs(this); |
| 234 | + var args = new DialogBeforeHideEventArgs(this, default, triggeredFromKeyboard: false, triggeredFromCloseButton: false); |
223 | 235 | await OnBeforeHide.InvokeAsync(args); |
224 | 236 | } |
225 | 237 | this.isShowing = false; |
226 | 238 | if (OnAfterHide.HasDelegate) |
227 | 239 | { |
228 | | - var args = new DialogAfterHideEventArgs(this); |
| 240 | + var args = new DialogAfterHideEventArgs(this, triggeredFromKeyboard: false, triggeredFromCloseButton: false); |
229 | 241 | await OnAfterHide.InvokeAsync(args); |
230 | 242 | } |
231 | 243 |
|
|
266 | 278 | await this.Show<object>(input); |
267 | 279 | } |
268 | 280 |
|
269 | | - public async Task<TResult?> Show<TResult>(object input) |
| 281 | + public async Task<TResult?> Show<TResult>(object? input) |
270 | 282 | { |
271 | 283 | this.Input = input; |
272 | 284 | var result = await ShowInternal(); |
|
298 | 310 |
|
299 | 311 | await jsRuntime.InvokeVoidAsync("blazorDialog.registerShownDialog", dotNetObjectReference, this.KeyboardCloseEnabled, this.KeyboardCloseKey); |
300 | 312 |
|
301 | | - taskCompletionSource = new TaskCompletionSource<object>(); |
| 313 | + taskCompletionSource = new TaskCompletionSource<object?>(); |
302 | 314 | return await taskCompletionSource.Task; |
303 | 315 | } |
304 | 316 |
|
305 | 317 |
|
306 | | - public async Task Hide<TResult>(TResult result) |
| 318 | + public async Task Hide<TResult>(TResult? result) |
| 319 | + { |
| 320 | + await Hide(result, triggeredFromKeyboard: false, triggeredFromCloseButton: false); |
| 321 | + } |
| 322 | + |
| 323 | + protected async Task Hide<TResult>(TResult? result, bool triggeredFromKeyboard = false, bool triggeredFromCloseButton = false) |
307 | 324 | { |
308 | | - locationChangingHandler.RemoveLocationChangingHandler(this); |
309 | 325 | if (OnBeforeHide.HasDelegate) |
310 | 326 | { |
311 | | - var args = new DialogBeforeHideEventArgs(this); |
| 327 | + var args = new DialogBeforeHideEventArgs(this, result, triggeredFromKeyboard, triggeredFromCloseButton); |
312 | 328 | await OnBeforeHide.InvokeAsync(args); |
| 329 | + if (args.PreventHide) return; |
313 | 330 | } |
| 331 | + locationChangingHandler.RemoveLocationChangingHandler(this); |
314 | 332 | this.isShowing = false; |
315 | | - NotifyDialogHidden(result); |
| 333 | + await NotifyDialogHidden(result); |
316 | 334 | await InvokeAsync(() => StateHasChanged()); |
317 | 335 | if (OnAfterHide.HasDelegate) |
318 | 336 | { |
319 | | - var args = new DialogAfterHideEventArgs(this); |
| 337 | + var args = new DialogAfterHideEventArgs(this, triggeredFromKeyboard, triggeredFromCloseButton); |
320 | 338 | await OnAfterHide.InvokeAsync(args); |
321 | 339 | } |
322 | 340 |
|
323 | 341 | await jsRuntime.InvokeVoidAsync("blazorDialog.unregisterShownDialog", dotNetObjectReference); |
324 | 342 | } |
325 | 343 |
|
| 344 | + internal async Task HideFromKeyboard() |
| 345 | + { |
| 346 | + await this.Hide<object>(null, triggeredFromKeyboard: true); |
| 347 | + } |
| 348 | + |
| 349 | + internal async Task HideFromCloseButton() |
| 350 | + { |
| 351 | + await this.Hide<object>(null, triggeredFromCloseButton: true); |
| 352 | + } |
| 353 | + |
326 | 354 | public async Task Hide() |
327 | 355 | { |
328 | 356 | await this.Hide<object>(null); |
|
367 | 395 | [JSInvokable] |
368 | 396 | public async Task HideFromKeyPress() |
369 | 397 | { |
370 | | - await this.Hide(); |
| 398 | + await this.HideFromKeyboard(); |
371 | 399 | } |
372 | 400 | } |
0 commit comments