-
Notifications
You must be signed in to change notification settings - Fork 8
Bugfix/disabled #10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Bugfix/disabled #10
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
src/SimpleBlazorMultiselect.Demo/Pages/DisabledDropdown.razor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,31 @@ | ||||||
| @page "/DisabledDropdown" | ||||||
| <h3>DisabledDropdown</h3> | ||||||
|
|
||||||
| <div class="row"> | ||||||
| <div class="col-4"> | ||||||
| <SimpleMultiselect | ||||||
| Disabled="@_isDisabled" | ||||||
| Options="@Globals.EuropeanCapitals" | ||||||
| CanFilter="true" | ||||||
| @bind-SelectedOptions="_selectedItems"/> | ||||||
| </div> | ||||||
| <div class="col-4"> | ||||||
| <button class="btn btn-primary" @onclick="() => _isDisabled = !_isDisabled"> | ||||||
| @(_isDisabled ? "Enable" : "Disable") | ||||||
| </button> | ||||||
| <br/> | ||||||
| You have selected the following items: | ||||||
| <ul> | ||||||
| @foreach (var item in _selectedItems) | ||||||
| { | ||||||
| <li>@item</li> | ||||||
| } | ||||||
| </ul> | ||||||
| </div> | ||||||
| </div> | ||||||
|
|
||||||
| @code { | ||||||
| private bool _isDisabled; | ||||||
|
|
||||||
| private HashSet<string> _selectedItems = []; | ||||||
|
||||||
| private HashSet<string> _selectedItems = []; | |
| private readonly HashSet<string> _selectedItems = []; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| using Bunit; | ||
| using FluentAssertions; | ||
| using Xunit; | ||
|
|
||
| namespace SimpleBlazorMultiselect.Tests; | ||
|
|
||
| public class DisabledTests : BaseTest | ||
| { | ||
| [Fact] | ||
| public void Component_WhenDisabled_HasDisabledAttribute() | ||
| { | ||
| var component = RenderComponent<SimpleMultiselect<string>>(parameters => parameters | ||
| .Add(p => p.Options, TestOptions) | ||
| .Add(p => p.Disabled, true) | ||
| ); | ||
|
|
||
| var button = component.Find("button"); | ||
| button.HasAttribute("disabled").Should().BeTrue(); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void Component_WhenDisabled_DoesNotOpenOnClick() | ||
| { | ||
| var component = RenderComponent<SimpleMultiselect<string>>(parameters => parameters | ||
| .Add(p => p.Options, TestOptions) | ||
| .Add(p => p.Disabled, true) | ||
| ); | ||
|
|
||
| var button = component.Find("button"); | ||
| button.Click(); | ||
|
|
||
| AssertClosed(component); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void Component_WhenEnabled_OpensOnClick() | ||
| { | ||
| var component = RenderComponent<SimpleMultiselect<string>>(parameters => parameters | ||
| .Add(p => p.Options, TestOptions) | ||
| .Add(p => p.Disabled, false) | ||
| ); | ||
|
|
||
| var button = component.Find("button"); | ||
| button.Click(); | ||
|
|
||
| AssertOpen(component, TestOptions.Count); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void Component_WhenDisabledThenEnabled_OpensOnClick() | ||
| { | ||
| var component = RenderComponent<SimpleMultiselect<string>>(parameters => parameters | ||
| .Add(p => p.Options, TestOptions) | ||
| .Add(p => p.Disabled, true) | ||
| ); | ||
|
|
||
| var button = component.Find("button"); | ||
| button.Click(); | ||
| AssertClosed(component); | ||
|
|
||
| component.SetParametersAndRender(parameters => parameters.Add(p => p.Disabled, false)); | ||
|
|
||
| button.Click(); | ||
| AssertOpen(component, TestOptions.Count); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void Component_WhenEnabledThenDisabled_DoesNotOpenOnClick() | ||
| { | ||
| var component = RenderComponent<SimpleMultiselect<string>>(parameters => parameters | ||
| .Add(p => p.Options, TestOptions) | ||
| .Add(p => p.Disabled, false) | ||
| ); | ||
|
|
||
| var button = component.Find("button"); | ||
| button.Click(); | ||
| AssertOpen(component, TestOptions.Count); | ||
|
|
||
| component.SetParametersAndRender(parameters => parameters.Add(p => p.Disabled, true)); | ||
|
|
||
| button.Click(); | ||
| AssertClosed(component); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void Component_WhenDisabledWithMenuOpen_ClosesMenu() | ||
| { | ||
| var component = RenderComponent<SimpleMultiselect<string>>(parameters => parameters | ||
| .Add(p => p.Options, TestOptions) | ||
| ); | ||
|
|
||
| var button = component.Find("button"); | ||
| button.Click(); | ||
| AssertOpen(component, TestOptions.Count); | ||
|
|
||
| component.SetParametersAndRender(parameters => parameters.Add(p => p.Disabled, true)); | ||
|
|
||
| AssertClosed(component); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,170 @@ | ||
| using AngleSharp.Dom; | ||
| using AngleSharp.Html.Dom; | ||
| using Bunit; | ||
| using FluentAssertions; | ||
| using Microsoft.AspNetCore.Components; | ||
| using Xunit; | ||
|
|
||
| namespace SimpleBlazorMultiselect.Tests; | ||
|
|
||
| public class EqualityTests : BaseTest | ||
| { | ||
| [Fact] | ||
| public void Component_CanDeselect_WhenPrefilledValueItems() | ||
| { | ||
| var options = new List<TestValueItem> | ||
| { | ||
| new("1", "Apple"), | ||
| new("2", "Banana"), | ||
| new("3", "Cherry") | ||
| }; | ||
| var selectedItems = new HashSet<TestValueItem> | ||
| { | ||
| new("1", "Apple") | ||
| }; | ||
|
|
||
| var component = RenderComponent<SimpleMultiselect<TestValueItem>>(parameters => parameters | ||
| .Add(p => p.Options, options) | ||
| .Add(p => p.SelectedOptions, selectedItems) | ||
| .Add(p => p.StringSelector, item => item.Name) | ||
| .Add(p => p.DefaultText, "Select fruits") | ||
| .Add(p => p.SelectedOptionsChanged, EventCallback.Factory.Create<HashSet<TestValueItem>>(this, newSelection => { selectedItems = newSelection; }))); | ||
|
|
||
| var button = component.Find("button"); | ||
| button.TextContent.Should().Contain("Apple"); | ||
| button.Click(); | ||
|
|
||
| // Now only apple should be checked | ||
| var appleOption = component.FindAll(".dropdown-item")[0]; | ||
| var appleCheckbox = appleOption.QuerySelector<IHtmlInputElement>("input[type='checkbox']"); | ||
| appleCheckbox.Should().NotBeNull(); | ||
| appleCheckbox.IsChecked.Should().BeTrue(); | ||
|
|
||
| appleOption.Click(); | ||
|
|
||
| // After clicking, apple should be deselected | ||
| selectedItems.Should().BeEmpty(); | ||
| button = component.Find("button"); | ||
| button.TextContent.Should().Be("Select fruits"); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void Component_CanDeselect_WhenPrefilledReferenceItems() | ||
| { | ||
| var options = new List<TestReferenceItem> | ||
| { | ||
| new("1", "Apple"), | ||
| new("2", "Banana"), | ||
| new("3", "Cherry") | ||
| }; | ||
| var selectedItems = new HashSet<TestReferenceItem> | ||
| { | ||
| new("1", "Apple") | ||
| }; | ||
|
|
||
| var component = RenderComponent<SimpleMultiselect<TestReferenceItem>>(parameters => parameters | ||
| .Add(p => p.Options, options) | ||
| .Add(p => p.SelectedOptions, selectedItems) | ||
| .Add(p => p.StringSelector, item => item.Name) | ||
| .Add(p => p.DefaultText, "Select fruits") | ||
| .Add(p => p.SelectedOptionsChanged, EventCallback.Factory.Create<HashSet<TestReferenceItem>>(this, newSelection => { selectedItems = newSelection; }))); | ||
|
|
||
| var button = component.Find("button"); | ||
| button.TextContent.Should().Contain("Apple"); | ||
| button.Click(); | ||
|
|
||
| // Now only apple should be checked | ||
| var appleOption = component.FindAll(".dropdown-item")[0]; | ||
| var appleCheckbox = appleOption.QuerySelector<IHtmlInputElement>("input[type='checkbox']"); | ||
| appleCheckbox.Should().NotBeNull(); | ||
| appleCheckbox.IsChecked.Should().BeTrue(); | ||
|
|
||
| appleOption.Click(); | ||
|
|
||
| // After clicking, apple should be deselected | ||
| selectedItems.Should().BeEmpty(); | ||
| button = component.Find("button"); | ||
| button.TextContent.Should().Be("Select fruits"); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void Component_CanDeselectValueItem_WhenMatchByReference() | ||
| { | ||
| var options = new List<TestValueItem> | ||
| { | ||
| new("1", "Apple"), | ||
| new("2", "Banana"), | ||
| new("3", "Cherry") | ||
| }; | ||
| var selectedItems = new HashSet<TestValueItem> | ||
| { | ||
| new("1", "Apple") | ||
| }; | ||
|
|
||
| var component = RenderComponent<SimpleMultiselect<TestValueItem>>(parameters => parameters | ||
| .Add(p => p.Options, options) | ||
| .Add(p => p.SelectedOptions, selectedItems) | ||
| .Add(p => p.StringSelector, item => item.Name) | ||
| .Add(p => p.DefaultText, "Select fruits") | ||
| .Add(p => p.SelectedOptionsChanged, EventCallback.Factory.Create<HashSet<TestValueItem>>(this, newSelection => { selectedItems = newSelection; })) | ||
| .Add(p => p.MatchByReference, true)); // Should not matter for value types | ||
|
|
||
| var button = component.Find("button"); | ||
| button.TextContent.Should().Contain("Apple"); | ||
| button.Click(); | ||
|
|
||
| // Now only apple should be checked | ||
| var appleOption = component.FindAll(".dropdown-item")[0]; | ||
| var appleCheckbox = appleOption.QuerySelector<IHtmlInputElement>("input[type='checkbox']"); | ||
| appleCheckbox.Should().NotBeNull(); | ||
| appleCheckbox.IsChecked.Should().BeTrue(); | ||
|
|
||
| appleOption.Click(); | ||
|
|
||
| // After clicking, apple should be deselected | ||
| selectedItems.Should().BeEmpty(); | ||
| button = component.Find("button"); | ||
| button.TextContent.Should().Be("Select fruits"); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void Component_CannotDeselectIdenticalInstance_WhenMatchByReference() | ||
| { | ||
| var options = new List<TestReferenceItem> | ||
| { | ||
| new("1", "Apple"), | ||
| new("2", "Banana"), | ||
| new("3", "Cherry") | ||
| }; | ||
| var selectedItems = new HashSet<TestReferenceItem> | ||
| { | ||
| new("1", "Apple") | ||
| }; | ||
|
|
||
| var component = RenderComponent<SimpleMultiselect<TestReferenceItem>>(parameters => parameters | ||
| .Add(p => p.Options, options) | ||
| .Add(p => p.SelectedOptions, selectedItems) | ||
| .Add(p => p.StringSelector, item => item.Name) | ||
| .Add(p => p.DefaultText, "Select fruits") | ||
| .Add(p => p.SelectedOptionsChanged, EventCallback.Factory.Create<HashSet<TestReferenceItem>>(this, newSelection => { selectedItems = newSelection; })) | ||
| .Add(p => p.MatchByReference, true)); // This will break the deselection | ||
|
|
||
| var button = component.Find("button"); | ||
| button.TextContent.Should().Contain("Apple"); | ||
| button.Click(); | ||
|
|
||
| // Apple should not be checked because the instance is different | ||
| // So clicking it will add another apple instead of removing the existing one | ||
| var appleOption = component.FindAll(".dropdown-item")[0]; | ||
| var appleCheckbox = appleOption.QuerySelector<IHtmlInputElement>("input[type='checkbox']"); | ||
| appleCheckbox.Should().NotBeNull(); | ||
| appleCheckbox.IsChecked.Should().BeFalse(); | ||
|
|
||
| appleOption.Click(); | ||
|
|
||
| // After clicking, we should have two apples | ||
| selectedItems.Should().HaveCount(2); | ||
| button = component.Find("button"); | ||
| button.TextContent.Should().Be("Apple, Apple"); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| using Bunit; | ||
| using FluentAssertions; | ||
|
|
||
| namespace SimpleBlazorMultiselect.Tests; | ||
|
|
||
| public class BaseTest : TestContext | ||
| { | ||
| protected readonly List<string> TestOptions = | ||
| [ | ||
| "Apple", | ||
| "Banana", | ||
| "Cherry", | ||
| "Date", | ||
| "Elderberry" | ||
| ]; | ||
|
|
||
| public BaseTest() | ||
| { | ||
| JSInterop.SetupModule("./_content/SimpleBlazorMultiselect/js/simpleMultiselect.js") | ||
| .SetupModule("register", invocation => invocation.Arguments.Count == 2) | ||
| .SetupVoid("dispose"); | ||
| } | ||
|
|
||
| protected static void AssertOpen(IRenderedComponent<SimpleMultiselect<string>> component, int? expectedItemCount = null) | ||
| { | ||
| var dropdown = component.Find(".dropdown-menu.show"); | ||
| dropdown.Should().NotBeNull(); | ||
|
|
||
| if (expectedItemCount.HasValue) | ||
| { | ||
| var dropdownItems = component.FindAll(".dropdown-item"); | ||
| dropdownItems.Should().HaveCount(expectedItemCount.Value); | ||
| } | ||
| } | ||
|
|
||
| protected static void AssertClosed(IRenderedComponent<SimpleMultiselect<string>> component) | ||
| { | ||
| var dropdown = component.FindAll(".dropdown-menu.show"); | ||
| dropdown.Should().BeEmpty(); | ||
|
|
||
| var dropdownItems = component.FindAll(".dropdown-item"); | ||
| dropdownItems.Should().HaveCount(0); | ||
| } | ||
| } |
7 changes: 7 additions & 0 deletions
7
src/SimpleBlazorMultiselect.Tests/Helper/TestReferenceItem.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| namespace SimpleBlazorMultiselect.Tests; | ||
|
|
||
| public class TestReferenceItem(string id, string name) | ||
| { | ||
| public string Id { get; set; } = id; | ||
| public string Name { get; set; } = name; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| namespace SimpleBlazorMultiselect.Tests; | ||
|
|
||
| public record TestValueItem(string Id, string Name); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Extra space in '_isDisabled =' - should be single space. Change
_isDisabled =to_isDisabled =.