Skip to content

Commit 6e6a8ed

Browse files
ric-rosRiccardo Rossetti
andauthored
MudSelectExtended: Fix disabled and readonly state when parent is disabled or readonly (#558)
Co-authored-by: Riccardo Rossetti <[email protected]>
1 parent 56c6da3 commit 6e6a8ed

File tree

4 files changed

+63
-17
lines changed

4 files changed

+63
-17
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
@namespace MudExtensions.UnitTests.TestComponents
2+
<MudPopoverProvider></MudPopoverProvider>
3+
4+
<MudForm Disabled="true">
5+
<MudSelectExtended T="string" @bind-Value="value" PopoverClass="select-popover-class">
6+
<MudSelectItemExtended Value="@("1")" />
7+
<MudSelectItemExtended Value="@("2")" />
8+
</MudSelectExtended>
9+
</MudForm>
10+
11+
@code {
12+
string? value = null;
13+
}
Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
@namespace MudExtensions.UnitTests.TestComponents
22
<MudPopoverProvider></MudPopoverProvider>
33

4-
<MudSelectExtended T="string" @bind-Value="value" PopoverClass="select-popover-class" onfocus="Focused" OnBlur="Blurred">
4+
<MudSelectExtended T="string" @bind-Value="value" PopoverClass="select-popover-class" Disabled="@Disabled" onfocus="Focused" OnBlur="Blurred">
55
<MudSelectItemExtended Value="@("1")"/>
66
<MudSelectItemExtended Value="@("2")"/>
77
<MudSelectItemExtended Value="@("3")"/>
@@ -11,18 +11,21 @@
1111
<MudSwitch @bind-Value="@_focused" />
1212

1313
@code {
14-
public static string __description__ = "Click should open the Menu and selecting a value should update the bindable value.";
14+
public static string __description__ = "Click should open the Menu and selecting a value should update the bindable value.";
1515

16-
string? value = null;
17-
bool _focused = false;
16+
[Parameter]
17+
public bool Disabled { get; set; }
1818

19-
private void Focused()
20-
{
21-
_focused = true;
22-
}
19+
string? value = null;
20+
bool _focused = false;
2321

24-
private void Blurred()
25-
{
26-
_focused = false;
27-
}
22+
private void Focused()
23+
{
24+
_focused = true;
25+
}
26+
27+
private void Blurred()
28+
{
29+
_focused = false;
30+
}
2831
}

CodeBeam.MudBlazor.Extensions.UnitTests/Components/SelectExtendedTests.cs

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -471,7 +471,7 @@ public void SingleSelect_Should_FireTextChangedBeforeSelectedValuesChanged()
471471
string.Join(",", selectedValues ?? new List<string>()).Should().Be("2");
472472

473473
input.Click();
474-
comp.WaitForAssertion(()=>comp.FindAll("div.mud-list-item-extended").Count.Should().BeGreaterThan(0));
474+
comp.WaitForAssertion(() => comp.FindAll("div.mud-list-item-extended").Count.Should().BeGreaterThan(0));
475475
items = comp.FindAll("div.mud-list-item-extended").ToArray();
476476

477477
items[0].Click();
@@ -713,7 +713,7 @@ public void SingleSelect_Should_CallValidationFunc()
713713

714714
comp.FindAll("div.mud-list-item-extended")[1].Click();
715715
// menu should be closed now
716-
comp.WaitForAssertion(() => menu.ClassList.Should().NotContain("mud-popover-open"));
716+
comp.WaitForAssertion(() => comp.Find("div.mud-popover").ClassList.Should().NotContain("mud-popover-open"));
717717
comp.WaitForAssertion(() => select.Instance.Value.Should().Be("2"));
718718
select.Instance.Text.Should().Be("2");
719719
validatedValue.Should().Be("2");
@@ -1333,5 +1333,35 @@ await comp.InvokeAsync(() =>
13331333
select.SelectedValues?.Count().Should().Be(1);
13341334
select.Text.Should().Be("test");
13351335
}
1336+
1337+
[Test]
1338+
public void Select_Should_NotOpen_WhenDisabled()
1339+
{
1340+
var comp = Context.RenderComponent<SelectTest1>(parameters =>
1341+
{
1342+
parameters.Add(p => p.Disabled, true);
1343+
});
1344+
var select = comp.FindComponent<MudSelectExtended<string>>();
1345+
var input = comp.Find("div.mud-input-control");
1346+
1347+
// Try to open the select
1348+
input.Click();
1349+
// The menu should not open
1350+
comp.Find("div.mud-popover").ClassList.Should().NotContain("mud-popover-open");
1351+
}
1352+
1353+
[Test]
1354+
public void Select_Should_NotOpen_WhenParentDisabled()
1355+
{
1356+
// Use a test component that wraps the select in a disabled parent
1357+
var comp = Context.RenderComponent<DisabledParentSelectTest>();
1358+
var select = comp.FindComponent<MudSelectExtended<string>>();
1359+
var input = comp.Find("div.mud-input-control");
1360+
1361+
// Try to open the select
1362+
input.Click();
1363+
// The menu should not open
1364+
comp.Find("div.mud-popover").ClassList.Should().NotContain("mud-popover-open");
1365+
}
13361366
}
13371367
}

CodeBeam.MudBlazor.Extensions/Components/SelectExtended/MudSelectExtended.razor.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -850,7 +850,7 @@ protected override async ValueTask DisposeAsyncCore()
850850
/// <param name="obj"></param>
851851
protected internal async Task HandleKeyDownAsync(KeyboardEventArgs obj)
852852
{
853-
if (Disabled || ReadOnly)
853+
if (GetDisabledState() || GetReadOnlyState())
854854
return;
855855

856856
if (_list != null && _isOpen)
@@ -997,7 +997,7 @@ public override ValueTask SelectRangeAsync(int pos1, int pos2)
997997
/// <returns></returns>
998998
public async Task ToggleMenu()
999999
{
1000-
if (Disabled || ReadOnly)
1000+
if (GetDisabledState() || GetReadOnlyState())
10011001
return;
10021002
if (_isOpen)
10031003
await CloseMenu();
@@ -1011,7 +1011,7 @@ public async Task ToggleMenu()
10111011
/// <returns></returns>
10121012
public async Task OpenMenu()
10131013
{
1014-
if (Disabled || ReadOnly)
1014+
if (GetDisabledState() || GetReadOnlyState())
10151015
return;
10161016
_isOpen = true;
10171017
UpdateIcon();

0 commit comments

Comments
 (0)