Skip to content

Commit d302b88

Browse files
authored
feat(ModalDialog): support IsCenter when set IsDraggable to true (#4589)
* feat: 增加可拖动垂直居中功能 * chore: 更新样式与脚本 * test: 更新单元测试 * doc: 更新参数文档 * test: 更新单元测试 * refactor: 更改默认值 * test: 更新单元测试 * refactor: 补全括弧
1 parent e8e7bbf commit d302b88

File tree

4 files changed

+29
-6
lines changed

4 files changed

+29
-6
lines changed

src/BootstrapBlazor/Components/Modal/Modal.razor.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,16 @@ export function init(id, invoke, shownCallback, closeCallback) {
1717
Data.set(id, modal)
1818

1919
EventHandler.on(el, 'shown.bs.modal', () => {
20+
const dialog = el.querySelector('.modal-dialog');
21+
if (dialog.classList.contains('is-draggable-center')) {
22+
const width = dialog.offsetWidth / 2;
23+
const height = dialog.offsetHeight / 2;
24+
25+
dialog.style.setProperty("margin-left", `calc(50vw - ${width}px)`);
26+
dialog.style.setProperty("margin-top", `calc(50vh - ${height}px)`);
27+
dialog.classList.remove('is-draggable-center');
28+
}
29+
2030
invoke.invokeMethodAsync(shownCallback)
2131
})
2232
EventHandler.on(el, 'hidden.bs.modal', e => {

src/BootstrapBlazor/Components/Modal/Modal.razor.scss

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1-
.is-draggable .modal-header {
1+
.is-draggable .modal-header {
22
cursor: pointer;
33
}
44

5+
.is-draggable-center {
6+
visibility: hidden;
7+
}
8+
59
.modal-header {
610
padding: .5rem 1rem;
711
}

src/BootstrapBlazor/Components/Modal/ModalDialog.razor.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ public partial class ModalDialog : IHandlerException
2424
.AddClass("modal-dialog-scrollable", IsScrolling)
2525
.AddClass("modal-fullscreen", MaximizeStatus)
2626
.AddClass("is-draggable", IsDraggable)
27+
.AddClass("is-draggable-center", IsCentered && IsDraggable)
2728
.AddClass("d-none", !IsShown)
2829
.AddClass(Class, !string.IsNullOrEmpty(Class))
2930
.Build();
@@ -52,13 +53,13 @@ public partial class ModalDialog : IHandlerException
5253
public bool ShowResize { get; set; }
5354

5455
/// <summary>
55-
/// 获得/设置 弹窗大小
56+
/// 获得/设置 弹窗大小 默认为 <see cref="Size.ExtraExtraLarge"/>
5657
/// </summary>
5758
[Parameter]
5859
public Size Size { get; set; } = Size.ExtraExtraLarge;
5960

6061
/// <summary>
61-
/// 获得/设置 弹窗大小
62+
/// 获得/设置 弹窗大小 默认为 <see cref="FullScreenSize.None"/>
6263
/// </summary>
6364
[Parameter]
6465
public FullScreenSize FullScreenSize { get; set; }
@@ -67,10 +68,10 @@ public partial class ModalDialog : IHandlerException
6768
/// 获得/设置 是否垂直居中 默认为 true
6869
/// </summary>
6970
[Parameter]
70-
public bool IsCentered { get; set; }
71+
public bool IsCentered { get; set; } = true;
7172

7273
/// <summary>
73-
/// 获得/设置 是否弹窗正文超长时滚动
74+
/// 获得/设置 是否弹窗正文超长时滚动 默认为 false
7475
/// </summary>
7576
[Parameter]
7677
public bool IsScrolling { get; set; }
@@ -82,7 +83,7 @@ public partial class ModalDialog : IHandlerException
8283
public bool IsDraggable { get; set; }
8384

8485
/// <summary>
85-
/// 获得/设置 是否显示最大化按钮
86+
/// 获得/设置 是否显示最大化按钮 默认为 false
8687
/// </summary>
8788
[Parameter]
8889
public bool ShowMaximizeButton { get; set; }

test/UnitTest/Components/ModalDialogTest.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,15 @@ public void IsDraggable_Ok()
7575
});
7676
});
7777
Assert.Contains("is-draggable", cut.Markup);
78+
Assert.Contains("is-draggable-center", cut.Markup);
7879
Assert.Contains("test_class", cut.Markup);
80+
81+
var dialog = cut.FindComponent<ModalDialog>();
82+
dialog.SetParametersAndRender(pb =>
83+
{
84+
pb.Add(a => a.IsCentered, false);
85+
});
86+
Assert.DoesNotContain("is-draggable-center", cut.Markup);
7987
}
8088

8189
[Fact]

0 commit comments

Comments
 (0)