Skip to content

Commit b587197

Browse files
feat(ContextMenu): context menu should be invisible while scrolling (#7822)
* Fix: context menu should be invisible while scrolling #7821 This is a prposed solution, based on logical deduction. It is hard for me to test this. * Fix: resolved issues on #7822 (review) #7821 * refactor: update parameter to IsInvisibleWhenTouchMove * refactor: 增加标签防止被裁剪 * revert: 代码撤销 * feat: 增加 ontouchmove 事件回调 * refactor: 增加 close 方法 * test: 增加单元测试 * chore: bump version 10.5.0 * test: 更新单元测试 --------- Co-authored-by: Argo Zhang <argo@live.ca>
1 parent a46488a commit b587197

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed

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>10.5.0-beta04</Version>
4+
<Version>10.5.0</Version>
55
</PropertyGroup>
66

77
<ItemGroup>

src/BootstrapBlazor/Components/ContextMenu/ContextMenuTrigger.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,13 @@ public class ContextMenuTrigger : BootstrapComponentBase
4545
[Parameter]
4646
public int? OnTouchDelay { get; set; }
4747

48+
/// <summary>
49+
/// <para lang="zh">标记滚动时上下文菜单是否应不可见。默认值为 false。</para>
50+
/// <para lang="en">Flags whether the context menu should be invisible while scrolling. Default is false.</para>
51+
/// </summary>
52+
[Parameter]
53+
public bool IsInvisibleWhenTouchMove { get; set; }
54+
4855
[Inject, NotNull]
4956
private IOptionsMonitor<BootstrapBlazorOptions>? Options { get; set; }
5057

@@ -73,6 +80,10 @@ protected override void BuildRenderTree(RenderTreeBuilder builder)
7380
builder.AddAttribute(30, "oncontextmenu", EventCallback.Factory.Create<MouseEventArgs>(this, OnContextMenu));
7481
builder.AddAttribute(35, "ontouchstart", EventCallback.Factory.Create<TouchEventArgs>(this, OnTouchStart));
7582
builder.AddAttribute(36, "ontouchend", EventCallback.Factory.Create<TouchEventArgs>(this, OnTouchEnd));
83+
if (IsInvisibleWhenTouchMove)
84+
{
85+
builder.AddAttribute(37, "ontouchmove", EventCallback.Factory.Create<TouchEventArgs>(this, OnTouchMove));
86+
}
7687
builder.AddEventPreventDefaultAttribute(40, "oncontextmenu", true);
7788
builder.AddContent(50, ChildContent);
7889
builder.CloseElement();
@@ -97,6 +108,8 @@ protected override void BuildRenderTree(RenderTreeBuilder builder)
97108
/// </summary>
98109
private bool IsBusy { get; set; }
99110

111+
[DynamicDependency(DynamicallyAccessedMemberTypes.PublicProperties, typeof(TouchEventArgs))]
112+
[DynamicDependency(DynamicallyAccessedMemberTypes.PublicProperties, typeof(TouchPoint))]
100113
private async Task OnTouchStart(TouchEventArgs e)
101114
{
102115
if (!IsBusy)
@@ -133,6 +146,14 @@ private async Task OnTouchStart(TouchEventArgs e)
133146
}
134147
}
135148

149+
private void OnTouchMove()
150+
{
151+
if (IsInvisibleWhenTouchMove)
152+
{
153+
IsTouchStarted = false;
154+
}
155+
}
156+
136157
private void OnTouchEnd()
137158
{
138159
IsTouchStarted = false;

test/UnitTest/Components/ContextMenuTest.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ public async Task ContextMenu_Ok()
2626
{
2727
pb.Add(a => a.WrapperTag, "div");
2828
pb.Add(a => a.ContextItem, foo);
29+
pb.Add(a => a.IsInvisibleWhenTouchMove, true);
2930
pb.AddChildContent(pb =>
3031
{
3132
pb.OpenElement(0, "div");
@@ -105,6 +106,9 @@ public async Task ContextMenu_Ok()
105106
await Task.Delay(500);
106107
row.TouchEnd();
107108
Assert.True(clicked);
109+
110+
// 触发 TouchMove 事件
111+
row.TouchMove();
108112
}
109113

110114
[Theory]

0 commit comments

Comments
 (0)