Skip to content

Commit bed184a

Browse files
Merge pull request #69 from stavroskasidis/release/1.3
Release/1.3
2 parents 9e35347 + c6c3799 commit bed184a

File tree

8 files changed

+121
-11
lines changed

8 files changed

+121
-11
lines changed

BlazorContextMenu/BlazorContextMenu.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
<Copyright />
1919
<PackageTags>blazor blazor-component blazor-context-menu context-menu contextmenu menu blazor-menu blazorcontextmenu razor razor-components razorcomponents</PackageTags>
2020
<VersionSuffix>$(VersionSuffix)</VersionSuffix>
21-
<Version>1.2.0</Version>
21+
<Version>1.3.0</Version>
2222
<Version Condition=" '$(VersionSuffix)' != '' ">$(Version)-$(VersionSuffix)</Version>
2323
<Product>Blazor.ContextMenu</Product>
2424
</PropertyGroup>

BlazorContextMenu/Components/ContextMenu.razor

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,12 @@
103103
[Parameter]
104104
public EventCallback<MenuAppearingEventArgs> OnAppearing { get; set; }
105105

106+
/// <summary>
107+
/// A handler that is triggered before the menu is hidden. Can be used to prevent the menu from hiding.
108+
/// </summary>
109+
[Parameter]
110+
public EventCallback<MenuHidingEventArgs> OnHiding { get; set; }
111+
106112
/// <summary>
107113
/// Set to false if you want to close the menu programmatically. Default: true
108114
/// </summary>
@@ -266,10 +272,21 @@
266272
await InvokeAsync(() => StateHasChanged());
267273
}
268274

269-
internal async Task Hide()
275+
internal async Task<bool> Hide()
270276
{
277+
if (OnHiding.HasDelegate)
278+
{
279+
var eventArgs = new MenuHidingEventArgs(Id, TargetId, X, Y, Trigger, Data);
280+
await OnHiding.InvokeAsync(eventArgs);
281+
if (eventArgs.PreventHide)
282+
{
283+
return false;
284+
}
285+
}
286+
271287
IsShowing = false;
272288
await InvokeAsync(() => StateHasChanged());
289+
return true;
273290
}
274291

275292
internal string GetTarget()
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace BlazorContextMenu
6+
{
7+
public class MenuHidingEventArgs
8+
{
9+
public MenuHidingEventArgs(string contextMenuId, string contextMenuTargetId, string x, string y, ContextMenuTrigger trigger, object data)
10+
{
11+
ContextMenuId = contextMenuId;
12+
ContextMenuTargetId = contextMenuTargetId;
13+
ContextMenuTrigger = trigger;
14+
Data = data;
15+
X = x;
16+
Y = y;
17+
}
18+
19+
/// <summary>
20+
/// The <see cref="ContextMenuTrigger"/> that triggered this menu.
21+
/// </summary>
22+
public ContextMenuTrigger ContextMenuTrigger { get; protected set; }
23+
24+
/// <summary>
25+
/// The X position of the <see cref="ContextMenu"/>.
26+
/// </summary>
27+
public string X { get; set; }
28+
29+
/// <summary>
30+
/// The Y position of the <see cref="ContextMenu"/>.
31+
/// </summary>
32+
public string Y { get; set; }
33+
34+
/// <summary>
35+
/// The id of the <see cref="ContextMenu"/> that triggered this event.
36+
/// </summary>
37+
public string ContextMenuId { get; protected set; }
38+
39+
/// <summary>
40+
/// The id of the target element that the <see cref="ContextMenu"/> was triggered from.
41+
/// </summary>
42+
public string ContextMenuTargetId { get; protected set; }
43+
44+
/// <summary>
45+
/// If set to true, then the <see cref="ContextMenu"/> will not hide.
46+
/// </summary>
47+
public bool PreventHide { get; set; }
48+
49+
/// <summary>
50+
/// Extra data that were passed to the <see cref="ContextMenu"/>.
51+
/// </summary>
52+
public object Data { get; protected set; }
53+
}
54+
}

BlazorContextMenu/Services/InternalContextMenuHandler.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ namespace BlazorContextMenu
1111
public interface IInternalContextMenuHandler
1212
{
1313
bool ReferencePassedToJs { get; set; }
14-
Task HideMenu(string id);
14+
Task<bool> HideMenu(string id);
1515
Task ShowMenu(string id, string x, string y, string targetId = null, DotNetObjectReference<ContextMenuTrigger> trigger = null);
1616
}
1717

@@ -50,13 +50,15 @@ public async Task ShowMenu(string id, string x, string y, string targetId = null
5050
/// </summary>
5151
/// <param name="id">The id of the menu.</param>
5252
[JSInvokable]
53-
public async Task HideMenu(string id)
53+
public async Task<bool> HideMenu(string id)
5454
{
5555
var menu = _contextMenuStorage.GetMenu(id);
5656
if (menu != null)
5757
{
58-
await menu.Hide();
58+
return await menu.Hide();
5959
}
60+
61+
return true;
6062
}
6163
}
6264
}

BlazorContextMenu/wwwroot/blazorContextMenu.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -122,9 +122,12 @@
122122
if (menuElement && menuElement.dataset["autohide"] == "true") {
123123
var clickedInsideMenu = menuElement.contains(e.target);
124124
if (!clickedInsideMenu) {
125-
blazorContextMenu.Hide(openMenuId);
126-
openMenuId = null;
127-
openMenuTarget = null;
125+
blazorContextMenu.Hide(openMenuId).then(function (hideSuccessful) {
126+
if (hideSuccessful) {
127+
openMenuId = null;
128+
openMenuTarget = null;
129+
}
130+
});
128131
}
129132
}
130133
}
@@ -146,7 +149,7 @@
146149
}
147150

148151
blazorContextMenu.Hide = function (menuId) {
149-
menuHandlerReference.invokeMethodAsync('HideMenu', menuId);
152+
return menuHandlerReference.invokeMethodAsync('HideMenu', menuId);
150153
}
151154

152155
var subMenuTimeout = null;

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,12 @@ public class Startup
240240
241241
## Release Notes
242242

243-
<details open="open"><summary>1.2</summary>
243+
<details open="open"><summary>1.3</summary>
244+
245+
>- Added menu `OnHiding` event [#68](https://github.com/stavroskasidis/BlazorContextMenu/issues/68).
246+
</details>
247+
248+
<details><summary>1.2</summary>
244249

245250
>- Fix for [#65](https://github.com/stavroskasidis/BlazorContextMenu/issues/65).
246251
</details>

TestApps/BlazorContextMenu.TestAppsCommon/CommonIndex.razor

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@
6363
<p id="test11-trigger">Double click me (menu2 - with icons and sub-menus)</p>
6464
</ContextMenuTrigger>
6565

66+
<ContextMenuTrigger MenuId="menu4">
67+
<p id="test12-trigger">Right-Click me (menu4 unhideable)</p>
68+
</ContextMenuTrigger>
69+
6670

6771
<ContextMenu Id="menu1" Template="dark" OnAppearing="OnAppearingMenu1">
6872
<Item Id="menu1-item1" OnClick="FetchDataClick">Fetch Data</Item>
@@ -104,6 +108,11 @@
104108
<Item Id="menu3-item2" OnClick="OnClick">Item 2</Item>
105109
</ContextMenu>
106110

111+
<ContextMenu Id="menu4" OnHiding="OnHiding">
112+
<Item Id="menu4-item1" OnClick="OnClick">Item 1</Item>
113+
<Item Id="menu4-item2" OnClick="OnClick">Item 2</Item>
114+
</ContextMenu>
115+
107116
<div>
108117
<button id="showMenuBtn" @onclick="ShowMenuBtnOnClick">Show menu1 in 300x300 using handler</button>
109118
<button id="showNoAutoCloseBtn" @onclick="ShowNoAutoCloseBtnOnClick">Show menu1 in 300x300 using handler</button>
@@ -165,4 +174,9 @@
165174
e.PreventShow = true;
166175
}
167176
}
177+
178+
void OnHiding(MenuHidingEventArgs e)
179+
{
180+
e.PreventHide = true;
181+
}
168182
}

Tests/BlazorContextMenu.TestsCommon/CommonTests/TestAppIndexTests.cs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,21 @@ public async Task Menu2_MouseOverSubMenuAndThenToSecondSubMenu_SecondSubMenuOpen
333333
Assert.Equal(expectedDisplay, display);
334334
}
335335

336-
336+
[Fact]
337+
public async Task Menu4_TriggerAndClickOutside_MenuStaysOpen()
338+
{
339+
//Arrange
340+
var expectedDisplay = "block";
341+
342+
//Act
343+
await OpenContextMenuAt("test12-trigger", MouseButtonTrigger.Right);
344+
var headerElement = Browser.FindElement(By.Id("header"));
345+
headerElement.Click();
346+
347+
//Assert
348+
var menuElement = Browser.FindElement(By.Id("menu4"));
349+
var display = menuElement.GetCssValue("display");
350+
Assert.Equal(expectedDisplay, display);
351+
}
337352
}
338353
}

0 commit comments

Comments
 (0)