Skip to content

Commit 6616a6f

Browse files
authored
feat(MaskService): add Selector parameter (#5361)
* feat: 增加 Selector 参数支持样式选择 * feat: 更新 JavaScript 交互 * feat: 支持 Selector 参数 * test: 更新单元测试
1 parent 56e3600 commit 6616a6f

File tree

4 files changed

+30
-15
lines changed

4 files changed

+30
-15
lines changed

src/BootstrapBlazor/Components/Mask/Mask.razor.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,12 @@ protected override async Task OnAfterRenderAsync(bool firstRender)
4343

4444
if (!firstRender)
4545
{
46-
await InvokeVoidAsync("update", Id, new { Show = _options != null, _options?.ContainerId });
46+
await InvokeVoidAsync("update", Id, new
47+
{
48+
Show = _options != null,
49+
_options?.ContainerId,
50+
_options?.Selector
51+
});
4752
}
4853
}
4954

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,17 @@
11
export function update(id, options) {
22
const mask = document.getElementById(id);
33
if (mask) {
4-
const { show, containerId } = options;
4+
const { show } = options;
55
const el = document.querySelector(`[data-bb-mask="${id}"]`);
6-
if (containerId) {
7-
const container = document.getElementById(containerId);
8-
if (container) {
9-
const position = container.style.getPropertyValue('position');
10-
if (position === '' || position === 'static') {
11-
container.style.setProperty('position', 'relative');
12-
}
13-
14-
if (show) {
15-
el.style.setProperty('--bb-mask-position', 'absolute');
16-
container.appendChild(el);
17-
}
6+
const container = getContainerBySelector(options);
7+
if (container) {
8+
const position = container.style.getPropertyValue('position');
9+
if (position === '' || position === 'static') {
10+
container.style.setProperty('position', 'relative');
11+
}
12+
if (show) {
13+
el.style.setProperty('--bb-mask-position', 'absolute');
14+
container.appendChild(el);
1815
}
1916
}
2017
else {
@@ -31,3 +28,10 @@
3128
}
3229
}
3330
}
31+
32+
const getContainerBySelector = options => {
33+
const selector = getContainerById(options.containerId) ?? options.selector;
34+
return selector ? document.querySelector(selector) : null;
35+
}
36+
37+
const getContainerById = id => id ? `#${id}` : null;

src/BootstrapBlazor/Components/Mask/MaskOption.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,9 @@ public class MaskOption
3434
/// 获得/设置 遮罩父容器 id 默认 null 未设置
3535
/// </summary>
3636
public string? ContainerId { get; set; }
37+
38+
/// <summary>
39+
/// 获得/设置 遮罩父容器选择器 Selector 默认 null 未设置
40+
/// </summary>
41+
public string? Selector { get; set; }
3742
}

test/UnitTest/Services/MaskServiceTest.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@ await maskService.Show(new MaskOption()
6666
Opacity = 0.5f,
6767
ZIndex = 1050,
6868
ChildContent = builder => builder.AddContent(0, "test-mask-content"),
69-
ContainerId = "test-9527"
69+
ContainerId = "test-9527",
70+
Selector = "test-mask-selector"
7071
});
7172
});
7273
});

0 commit comments

Comments
 (0)