Skip to content

Commit d0d4f72

Browse files
authored
Auto-focus "Yes" button now handled in Blazor (#1261)
Moved auto-focus logic from JS to Blazor using ElementReference and FocusAsync. Updated ConfirmDialog to use @ref and OnAfterRenderAsync for focusing. Removed autoFocusYesButton handling from JS.
1 parent b41f295 commit d0d4f72

3 files changed

Lines changed: 14 additions & 16 deletions

File tree

blazorbootstrap/Components/ConfirmDialog/ConfirmDialog.razor

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
}
4242
@if (!string.IsNullOrWhiteSpace(yesButtonText))
4343
{
44-
<button id="bb-confirm-@Id" class="@yesButtonColor px-4" type="button" @onclick="OnYesClick">@yesButtonText</button>
44+
<button @ref="@yesButtonElement" class="@yesButtonColor px-4" type="button" @onclick="OnYesClick">@yesButtonText</button>
4545
}
4646
</div>
4747
</div>

blazorbootstrap/Components/ConfirmDialog/ConfirmDialog.razor.cs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,9 @@ public partial class ConfirmDialog : BlazorBootstrapComponentBase
55
#region Fields and Constants
66

77
private Type? childComponent;
8-
98
private string? dialogCssClass;
109
private bool dismissable;
1110
private string? headerCssClass;
12-
1311
private bool isVisible;
1412
private string? message1;
1513
private string? message2;
@@ -18,20 +16,27 @@ public partial class ConfirmDialog : BlazorBootstrapComponentBase
1816
private string? noButtonText;
1917
private Dictionary<string, object>? parameters;
2018
private string? scrollable;
21-
19+
private bool shouldAutoFocusYesButton;
2220
private bool showBackdrop;
23-
2421
private TaskCompletionSource<bool>? taskCompletionSource;
25-
2622
private string? title;
2723
private string? verticallyCentered;
24+
private ElementReference yesButtonElement;
2825
private string? yesButtonColor;
2926
private string? yesButtonText;
3027

3128
#endregion
3229

3330
#region Methods
3431

32+
protected override async Task OnAfterRenderAsync(bool firstRender)
33+
{
34+
if(isVisible && shouldAutoFocusYesButton)
35+
await yesButtonElement.FocusAsync();
36+
37+
await base.OnAfterRenderAsync(firstRender);
38+
}
39+
3540
/// <summary>
3641
/// Shows confirm dialog.
3742
/// </summary>
@@ -116,13 +121,13 @@ private Task<bool> Show(string title, string? message1, string? message2, Type?
116121
modalSize = confirmDialogOptions.Size.ToDialogSizeClass();
117122
yesButtonColor = $"{BootstrapClass.Button} {confirmDialogOptions.YesButtonColor.ToButtonColorClass()}";
118123
yesButtonText = confirmDialogOptions.YesButtonText;
119-
124+
shouldAutoFocusYesButton = confirmDialogOptions.AutoFocusYesButton;
120125
isVisible = true;
121126
showBackdrop = true;
122127

123128
StateHasChanged();
124129

125-
Task.Run(async () => await SafeInvokeVoidAsync("window.blazorBootstrap.confirmDialog.show", Id, confirmDialogOptions.AutoFocusYesButton));
130+
Task.Run(async () => await SafeInvokeVoidAsync("window.blazorBootstrap.confirmDialog.show", Id));
126131

127132
return task;
128133
}

blazorbootstrap/wwwroot/blazor.bootstrap.js

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -223,21 +223,14 @@ window.blazorBootstrap = {
223223
}
224224
},
225225
confirmDialog: {
226-
show: (elementId, autoFocusYesButton) => {
226+
show: (elementId) => {
227227
let confirmDialogEl = document.getElementById(elementId);
228228
if (confirmDialogEl != null)
229229
setTimeout(() => confirmDialogEl.classList.add('show'), 90); // added delay for server
230230

231231
let bodyEl = document.getElementsByTagName('body');
232232
if (bodyEl.length > 0)
233233
bodyEl[0].style['overflow'] = 'hidden';
234-
235-
if (!autoFocusYesButton)
236-
return;
237-
238-
let yesButtonEl = document.getElementById(`bb-confirm-${elementId}`);
239-
if (yesButtonEl)
240-
yesButtonEl.focus();
241234
},
242235
hide: (elementId) => {
243236
let confirmDialogEl = document.getElementById(elementId);

0 commit comments

Comments
 (0)