Skip to content

Commit 1f3493a

Browse files
tomashercegexyi
authored andcommitted
Fixed Button issue with null binding in Enabled property
1 parent 3b5ff1d commit 1f3493a

5 files changed

Lines changed: 91 additions & 2 deletions

File tree

src/Framework/Framework/Controls/ButtonBase.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ protected override void AddAttributesToRender(IHtmlWriter writer, IDotvvmRequest
7575
{
7676
writer.AddKnockoutDataBind("dotvvm-enable", this, EnabledProperty, () =>
7777
{
78-
if (!Enabled)
78+
if (GetValue(EnabledProperty) is not true)
7979
{
8080
writer.AddAttribute("disabled", "disabled");
8181
}
@@ -91,7 +91,7 @@ public bool ValidateCommand(DotvvmProperty targetProperty)
9191
{
9292
if (targetProperty == ClickProperty)
9393
{
94-
return Enabled && Visible;
94+
return GetValue(EnabledProperty) is true && GetValue(VisibleProperty) is true;
9595
}
9696
return false;
9797
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using DotVVM.Framework.ViewModel;
7+
using DotVVM.Framework.Hosting;
8+
9+
namespace DotVVM.Samples.Common.ViewModels.FeatureSamples.NullHandling
10+
{
11+
public class Button_EnabledViewModel : DotvvmViewModelBase
12+
{
13+
14+
public NullObject Null { get; set; } = null;
15+
16+
public int Value { get; set; }
17+
18+
public class NullObject
19+
{
20+
public bool Enabled { get; set; }
21+
}
22+
}
23+
24+
}
25+
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
@viewModel DotVVM.Samples.Common.ViewModels.FeatureSamples.NullHandling.Button_EnabledViewModel, DotVVM.Samples.Common
2+
3+
<!DOCTYPE html>
4+
5+
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
6+
<head>
7+
<meta charset="utf-8" />
8+
<title></title>
9+
</head>
10+
<body>
11+
12+
<h1>Repro for ButtonBase.Enabled bug</h1>
13+
14+
<dot:Button Enabled="{resource: Null.Enabled}" Text="resource binding" Click="{command: Value = Value + 1}" data-ui="resource-button" />
15+
16+
<dot:Button Enabled="{value: Null.Enabled}" Text="value binding" Click="{command: Value = Value + 1}" data-ui="value-button"/>
17+
18+
<a onclick="unlockButtons(); return false;" href="#">Unlock by JS</a>
19+
20+
<p class="result">{{value: Value}}</p>
21+
22+
<script type="text/javascript">
23+
function unlockButtons() {
24+
Array.from(document.querySelectorAll('input[type=button]')).forEach(b => b.disabled = null);
25+
}
26+
</script>
27+
</body>
28+
</html>
29+
30+

src/Samples/Tests/Abstractions/SamplesRouteUrls.designer.cs

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using DotVVM.Samples.Tests.Base;
2+
using DotVVM.Testing.Abstractions;
3+
using Riganti.Selenium.Core;
4+
using Xunit;
5+
using Xunit.Abstractions;
6+
7+
namespace DotVVM.Samples.Tests.Feature;
8+
9+
public class NullHandlingTests(ITestOutputHelper output) : AppSeleniumTest(output)
10+
{
11+
[Theory]
12+
[InlineData("resource-button")]
13+
[InlineData("value-button")]
14+
public void Feature_HtmlTag_NonPairHtmlTag(string buttonId)
15+
{
16+
RunInAllBrowsers(browser => {
17+
browser.NavigateToUrl(SamplesRouteUrls.FeatureSamples_NullHandling_Button_Enabled);
18+
19+
var button = browser.Single(buttonId, SelectByDataUi);
20+
AssertUI.IsNotEnabled(button);
21+
22+
browser.Single("a").Click();
23+
24+
button.Click();
25+
26+
var errorPage = browser.Single("#debugWindow");
27+
AssertUI.IsDisplayed(errorPage);
28+
29+
var scope = browser.GetFrameScope("#debugWindow iframe");
30+
AssertUI.Text(scope.Single(".summary"), t => t.Contains("Execution of '{command: Value = Value + 1}' was disallowed by '<dot:Button "));
31+
});
32+
}
33+
}

0 commit comments

Comments
 (0)