Skip to content

Commit 833ca29

Browse files
authored
Disable auto focus on the yes button (#648)
* Disable auto focus on the yes button - docs and demos update.
1 parent f2d6a60 commit 833ca29

File tree

8 files changed

+86
-5
lines changed

8 files changed

+86
-5
lines changed

BlazorBootstrap.Demo.RCL/Pages/ConfirmDialog/ConfirmDialogDocumentation.razor

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,15 @@
5757
<div class="mb-3"></div>
5858
<Demo Type="typeof(ConfirmDialog_Demo_07_Vertically_Centered_02)" Tabs="true" />
5959

60+
<SectionHeading Size="HeadingSize.H2" Text="Disable auto focus on the yes button" PageUrl="@pageUrl" HashTagName="Disable-auto-focus-on-the-yes-button" />
61+
<Callout Type="CalloutType.Info" HideHeading="true">
62+
By default, auto focus on the <b>"Yes"</b> button is enabled.
63+
</Callout>
64+
<div class="my-3">
65+
To disabe the autofocus, set <Badge Color="BadgeColor.Light">AutoFocusYesButton = false</Badge> on the <b>ConfirmDialogOptions</b>.
66+
</div>
67+
<Demo Type="typeof(ConfirmDialog_Demo_08_Disable_AutoFocus_Yes_Button)" Tabs="true" />
68+
6069
@code {
6170
private string pageUrl = "/confirm-dialog";
6271
private string title = "Blazor Confirm Dialog Component";

BlazorBootstrap.Demo.RCL/Pages/ConfirmDialog/ConfirmDialog_Demo_01_Examples.razor

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,4 @@
2121
// do something
2222
}
2323
}
24-
}
24+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<ConfirmDialog @ref="dialog" />
2+
3+
<Button Color="ButtonColor.Primary" @onclick="ShowDialogAsync"> Launch Confirm Dialog </Button>
4+
5+
@code {
6+
private ConfirmDialog dialog = default!;
7+
8+
private async Task ShowDialogAsync()
9+
{
10+
var confirmation = await dialog.ShowAsync<LongContentDemoComponent>(
11+
title: "Confirm dialog title",
12+
confirmDialogOptions: new ConfirmDialogOptions { AutoFocusYesButton = false }
13+
);
14+
15+
if (confirmation)
16+
{
17+
// do something
18+
}
19+
else
20+
{
21+
// do something
22+
}
23+
}
24+
}

BlazorBootstrap.Demo.RCL/Pages/Index.razor

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@
8787
</div>
8888
<div class="col-sm-4 mb-2">
8989
<a class="d-block pe-lg-4 text-decoration-none lh-sm" href="/confirm-dialog">
90-
<h4 class="mb-0 fs-5 fw-semibold"><Icon Name="IconName.QuestionDiamondFill" class="me-2" /> Confirm Dialog</h4>
90+
<h4 class="mb-0 fs-5 fw-semibold"><Icon Name="IconName.QuestionDiamondFill" class="me-2" /> Confirm Dialog<Badge Color="BadgeColor.Success">Updated</Badge></h4>
9191
</a>
9292
</div>
9393
<div class="col-sm-4 mb-2">

blazorbootstrap/Components/ConfirmDialog/ConfirmDialog.razor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ private Task<bool> Show(string title, string? message1, string? message2, Type?
138138

139139
StateHasChanged();
140140

141-
QueueAfterRenderAction(async () => { await JS.InvokeVoidAsync("window.blazorBootstrap.confirmDialog.show", ElementId); }, new RenderPriority());
141+
QueueAfterRenderAction(async () => { await JS.InvokeVoidAsync("window.blazorBootstrap.confirmDialog.show", ElementId, confirmDialogOptions.AutoFocusYesButton); }, new RenderPriority());
142142

143143
return task;
144144
}

blazorbootstrap/Models/ConfirmDialogOptions.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ public class ConfirmDialogOptions
44
{
55
#region Properties, Indexers
66

7+
/// <summary>
8+
/// Determines whether to focus on the yes button or not.
9+
/// </summary>
10+
public bool AutoFocusYesButton { get; set; } = true;
11+
712
/// <summary>
813
/// Additional CSS class for the dialog (div.modal-dialog element).
914
/// </summary>

blazorbootstrap/wwwroot/blazor.bootstrap.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ window.blazorBootstrap = {
211211
}
212212
},
213213
confirmDialog: {
214-
show: (elementId) => {
214+
show: (elementId, autoFocusYesButton) => {
215215
let confirmDialogEl = document.getElementById(elementId);
216216
if (confirmDialogEl != null)
217217
setTimeout(() => confirmDialogEl.classList.add('show'), 90); // added delay for server
@@ -220,6 +220,9 @@ window.blazorBootstrap = {
220220
if (bodyEl.length > 0)
221221
bodyEl[0].style['overflow'] = 'hidden';
222222

223+
if (!autoFocusYesButton)
224+
return;
225+
223226
let yesButtonEl = document.getElementById(`bb-confirm-${elementId}`);
224227
if (yesButtonEl)
225228
yesButtonEl.focus();

docs/docs/05-components/confirm-dialog.mdx

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -379,4 +379,44 @@ You can also create a scrollable dialog that allows scroll the dialog body by up
379379
}
380380
```
381381

382-
[See Confirm Dialog demo here.](https://demos.blazorbootstrap.com/confirm-dialog#vertically-centered)
382+
[See demo here.](https://demos.blazorbootstrap.com/confirm-dialog#vertically-centered)
383+
384+
### Disable auto focus on the yes button
385+
386+
:::info
387+
By default, auto focus on the **"Yes"** button is enabled.
388+
:::
389+
390+
To disabe the autofocus, set `AutoFocusYesButton = false` on the ConfirmDialogOptions.
391+
392+
393+
```cshtml {} showLineNumbers
394+
<ConfirmDialog @ref="dialog" />
395+
396+
<Button Color="ButtonColor.Primary" @onclick="ShowDialogAsync"> Launch Confirm Dialog </Button>
397+
```
398+
399+
```cshtml {} showLineNumbers
400+
@code {
401+
private ConfirmDialog dialog = default!;
402+
403+
private async Task ShowDialogAsync()
404+
{
405+
var confirmation = await dialog.ShowAsync<LongContentDemoComponent>(
406+
title: "Confirm dialog title",
407+
confirmDialogOptions: new ConfirmDialogOptions { AutoFocusYesButton = false }
408+
);
409+
410+
if (confirmation)
411+
{
412+
// do something
413+
}
414+
else
415+
{
416+
// do something
417+
}
418+
}
419+
}
420+
```
421+
422+
[See demo here.](https://demos.blazorbootstrap.com/confirm-dialog#disable-auto-focus-on-the-yes-button)

0 commit comments

Comments
 (0)