Skip to content

Commit 692d46c

Browse files
ArgoZhangice6
andauthored
feat(Drawer): add IsKeyboard parameter (#5207)
* feat: 增加 IsKeyboard 支持 * refactor: DrawerOptions support IsKeyboard * doc: update sample code * refactor: 更改默认值为 false * doc: 增加 ESC 按键示例 * test: 更新单元测试 * chore: bump version beta06 Co-Authored-By: ice6 <[email protected]> * chore: bump version 9.3.0 --------- Co-authored-by: ice6 <[email protected]>
1 parent c4f78b4 commit 692d46c

File tree

12 files changed

+84
-8
lines changed

12 files changed

+84
-8
lines changed

src/BootstrapBlazor.Server/Components/Samples/Drawers.razor

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
<button type="button" class="btn btn-primary" @onclick="@(e => IsOpen = true)">@Localizer["Open"]</button>
1717
</section>
1818

19-
<Drawer Placement="@DrawerAlign" IsOpen="@IsOpen" AllowResize="true">
19+
<Drawer Placement="@DrawerAlign" @bind-IsOpen="@IsOpen" AllowResize="true">
2020
<div class="d-flex justify-content-center align-items-center flex-column" style="height: 290px;">
2121
<p>@Localizer["Content"]</p>
2222
<button type="button" class="btn btn-primary" @onclick="@(e => IsOpen = false)">@Localizer["Close"]</button>
@@ -35,7 +35,7 @@
3535
</Drawer>
3636
</DemoBlock>
3737

38-
<DemoBlock Title="@Localizer["NoBackdropTitle"]" Introduction="@Localizer["NoBackdropIntro"]" Name="IsOpen">
38+
<DemoBlock Title="@Localizer["NoBackdropTitle"]" Introduction="@Localizer["NoBackdropIntro"]" Name="ShowBackdrop">
3939
<section ignore>
4040
<button type="button" class="btn btn-primary" @onclick="@OpenNoBackdropDrawer">@Localizer["Open"]</button>
4141
</section>
@@ -47,6 +47,18 @@
4747
</Drawer>
4848
</DemoBlock>
4949

50+
<DemoBlock Title="@Localizer["IsKeyboardTitle"]" Introduction="@Localizer["IsKeyboardIntro"]" Name="IsKeyboard">
51+
<section ignore>
52+
<button type="button" class="btn btn-primary" @onclick="@OpenKeyboardDrawer">@Localizer["Open"]</button>
53+
</section>
54+
<Drawer Placement="Placement.Left" @bind-IsOpen="@IsKeyboardOpen" IsKeyboard="true">
55+
<div class="d-flex justify-content-center align-items-center flex-column" style="height: 290px;">
56+
<p>@Localizer["Content"]</p>
57+
<button type="button" class="btn btn-primary" @onclick="@(e => IsKeyboardOpen = false)">@Localizer["Close"]</button>
58+
</div>
59+
</Drawer>
60+
</DemoBlock>
61+
5062
<DemoBlock Title="@Localizer["DrawerServiceTitle"]" Introduction="@Localizer["DrawerServiceIntro"]" Name="DrawerService">
5163
<Button OnClickWithoutRender="@DrawerServiceShow" Text="@Localizer["Open"]"></Button>
5264
</DemoBlock>

src/BootstrapBlazor.Server/Components/Samples/Drawers.razor.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ private void OpenDrawer()
5353

5454
private void OpenNoBackdropDrawer() => IsShowBackdropOpen = true;
5555

56+
private bool IsKeyboardOpen { get; set; }
57+
58+
private void OpenKeyboardDrawer() => IsKeyboardOpen = true;
59+
5660
private async Task DrawerServiceShow() => await DrawerService.Show(new DrawerOption()
5761
{
5862
Placement = Placement.Right,

src/BootstrapBlazor.Server/Locales/en-US.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -892,6 +892,8 @@
892892
"NoBackdropIntro": "By setting the <code>ShowBackdrop=\"false\"</code> do not show the backdrop",
893893
"DrawerServiceTitle": "Drawer Service",
894894
"DrawerServiceIntro": "Open the drawer pop-up window by calling the <code>DrawerService</code> service",
895+
"IsKeyboardTitle": "ESC",
896+
"IsKeyboardIntro": "By default, the component uses the <kbd>ESC</kbd> key to close the drawer popup. You can enable this function by <code>IsKeyboard=\"true\"</code>",
895897
"Open": "click me to open",
896898
"Content": "The layout, components, etc. in the drawer are fully customizable",
897899
"Close": "close the drawer",

src/BootstrapBlazor.Server/Locales/zh-CN.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -892,6 +892,8 @@
892892
"NoBackdropIntro": "通过设置 <code>ShowBackdrop=\"false\"</code> 不显示遮罩,这种模式下可以与抽屉下面网页元素进行交互操作",
893893
"DrawerServiceTitle": "调用服务打开抽屉",
894894
"DrawerServiceIntro": "通过调用 <code>DrawerService</code> 服务打开抽屉弹窗",
895+
"IsKeyboardTitle": "ESC 按键支持",
896+
"IsKeyboardIntro": "组件默认使用 <kbd>ESC</kbd> 按键关闭抽屉弹窗,可通过 <code>IsKeyboard=\"true\"</code> 开启此功能",
895897
"Open": "点我打开",
896898
"Content": "抽屉内布局、组件等完全可以自定义",
897899
"Close": "关闭抽屉",

src/BootstrapBlazor/BootstrapBlazor.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk.Razor">
22

33
<PropertyGroup>
4-
<Version>9.2.9-beta05</Version>
4+
<Version>9.3.0</Version>
55
</PropertyGroup>
66

77
<ItemGroup>

src/BootstrapBlazor/Components/Drawer/Drawer.razor

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
@namespace BootstrapBlazor.Components
22
@inherits BootstrapModuleComponentBase
3-
@attribute [BootstrapModuleAutoLoader]
3+
@attribute [BootstrapModuleAutoLoader(JSObjectReference = true)]
44

5-
<div @attributes="@AdditionalAttributes" tabindex="-1" class="@ClassString" style="@StyleString" id="@Id">
5+
<div @attributes="@AdditionalAttributes" tabindex="-1" class="@ClassString" style="@StyleString" id="@Id" data-bb-keyboard="@KeyboardString">
66
@if (ShowBackdrop)
77
{
88
<div tabindex="-1" class="drawer-backdrop" @onclick="@OnContainerClick">

src/BootstrapBlazor/Components/Drawer/Drawer.razor.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,14 @@ public partial class Drawer
119119
[Parameter]
120120
public object? BodyContext { get; set; }
121121

122+
/// <summary>
123+
/// 获得/设置 是否支持键盘 ESC 关闭当前弹窗 默认 false
124+
/// </summary>
125+
[Parameter]
126+
public bool IsKeyboard { get; set; }
127+
128+
private string? KeyboardString => IsKeyboard ? "true" : null;
129+
122130
/// <summary>
123131
/// <inheritdoc/>
124132
/// </summary>
@@ -134,6 +142,12 @@ protected override async Task OnAfterRenderAsync(bool firstRender)
134142
}
135143
}
136144

145+
/// <summary>
146+
/// <inheritdoc/>
147+
/// </summary>
148+
/// <returns></returns>
149+
protected override Task InvokeInitAsync() => InvokeVoidAsync("init", Id, Interop, nameof(Close));
150+
137151
/// <summary>
138152
/// 点击背景遮罩方法
139153
/// </summary>
@@ -153,6 +167,7 @@ public async Task OnContainerClick()
153167
/// 关闭抽屉方法
154168
/// </summary>
155169
/// <returns></returns>
170+
[JSInvokable]
156171
public async Task Close()
157172
{
158173
IsOpen = false;

src/BootstrapBlazor/Components/Drawer/Drawer.razor.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import Data from "../../modules/data.js"
22
import Drag from "../../modules/drag.js"
3+
import EventHandler from "../../modules/event-handler.js"
34

45
const initDrag = el => {
56
let originX = 0
@@ -60,7 +61,7 @@ const initDrag = el => {
6061
)
6162
}
6263

63-
export function init(id) {
64+
export function init(id, invoke, method) {
6465
const el = document.getElementById(id);
6566
if (el === null) {
6667
return;
@@ -73,6 +74,15 @@ export function init(id) {
7374
Data.set(id, dw)
7475

7576
initDrag(el);
77+
78+
EventHandler.on(el, 'keyup', async e => {
79+
if (e.key === 'Escape') {
80+
const supportESC = el.getAttribute('data-bb-keyboard') === 'true';
81+
if (supportESC) {
82+
await invoke.invokeMethodAsync(method);
83+
}
84+
}
85+
});
7686
}
7787

7888
export function execute(id, open) {
@@ -125,7 +135,9 @@ export function dispose(id) {
125135
const dw = Data.get(id)
126136
Data.remove(id);
127137

128-
const { el, body } = dw
138+
const { el, body } = dw;
139+
EventHandler.off(el, 'keyup');
140+
129141
if (el.classList.contains('show')) {
130142
el.classList.remove('show')
131143

src/BootstrapBlazor/Components/Drawer/DrawerContainer.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ private Dictionary<string, object> GetParameters(DrawerOption option)
6565
var parameters = new Dictionary<string, object>()
6666
{
6767
[nameof(Drawer.IsOpen)] = true,
68+
[nameof(Drawer.IsKeyboard)] = option.IsKeyboard,
6869
[nameof(Drawer.IsBackdrop)] = option.IsBackdrop,
6970
[nameof(Drawer.ShowBackdrop)] = option.ShowBackdrop,
7071
[nameof(Drawer.Placement)] = option.Placement,

src/BootstrapBlazor/Components/Drawer/DrawerOption.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ public class DrawerOption
2525
/// </summary>
2626
public string? Height { get; set; }
2727

28+
/// <summary>
29+
/// 获得/设置 是否支持键盘 ESC 关闭当前弹窗 默认 false
30+
/// </summary>
31+
public bool IsKeyboard { get; set; }
32+
2833
/// <summary>
2934
/// 获得/设置 点击遮罩是否关闭抽屉 默认为 false
3035
/// </summary>

0 commit comments

Comments
 (0)