Skip to content

AutoComplete: New Feature - Loading Indicator & CancellationToken#3096

Closed
mthrift2100 wants to merge 2 commits intoMudBlazor:devfrom
mthrift2100:feature/autocomplete-loading-cancellation
Closed

AutoComplete: New Feature - Loading Indicator & CancellationToken#3096
mthrift2100 wants to merge 2 commits intoMudBlazor:devfrom
mthrift2100:feature/autocomplete-loading-cancellation

Conversation

@mthrift2100
Copy link

@mthrift2100 mthrift2100 commented Oct 19, 2021

Description

Adds the following parameters to support a visual indicator for loading/progress while the MudAutoComplete component is awaiting the results of the SearchFunc Task.

  • [Parameter] public bool ShowLoading { get; set; }
  • [Parameter] public Size LoadingSize { get; set; } = Size.Medium;
  • [Parameter] public Color LoadingColor { get; set; } = Color.Default;
  • [Parameter] public int MinHeight {get; set;}

Adds a [Parameter] public Func<string, CancellationToken, Task<IEnumerable<T>>> SearchFuncWithCancel { get; set; } parameter which can be used instead of [Parameter] public Func<string, Task<IEnumerable<T>>> SearchFunc { get; set; }.
This allows a CancellationToken to be be passed into the SearchFunc by the MudAutoComplete component. The token is cancelled when the following methods are called:

  • Task OnSearchAsync()
  • Task SelectOption(T value)

This allows for expensive asynchronous work to be cancelled before the result is returned, and can then more quickly process the next search (if the user changed the search value).

How Has This Been Tested?

  • All existing Unit Tests for AutoComplete ran successfully.
  • Created AutocompleteTestCancellation.razor in MudBlazor.UnitTests.Viewer->TestComponents->Autocomplete to test the Loading and Cancellation functionality.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)

autocomplete_cancellation

Checklist:

  • The PR is submitted to the correct branch (dev).
  • My code follows the code style of this project.
  • I've added relevant tests.

@mckaragoz
Copy link
Member

I think if we add a progress, it should be linear, not circular, because MudTable etc. uses linear also. Material guides said that we should select one progress type for whole application for consistency.

@mthrift2100
Copy link
Author

Using the linear progress indicator would be easier. It would eliminate the need for a MinHeight / LoadingSize parameter, and could be applied to the bottom edge of the Input control.

I think a loading indicator is essential, as is cancellation functionality. In a production setting, the SearchFunc will almost certainly be making an API call to a server, which means there is no guarantee of how long it will take to return a result. A loading indicator will help the user understand what is happening when they start typing.

If any of the following occur while the component is awaiting the results of the SearchFunc, the results of the SearchFunc will
be discarded. So passing a CancellationToken into the SearchFunc will allow the user to cancel expensive work that is almost certainty going to be wasted.

  • User changes search criteria
  • User makes a selection from the list that is already visible
  • Component loses focus.

@JonBunator JonBunator added the enhancement Adds a new feature or enhances existing functionality (not fixing a defect) in the main library label Oct 21, 2021
@@ -0,0 +1,53 @@
@namespace MudBlazor.UnitTests.TestComponents
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please also add a bUnit test?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Acknowledged.

@@ -0,0 +1,53 @@
@namespace MudBlazor.UnitTests.TestComponents
@using System.Threading
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with @mckaragoz that a linear progress bar makes more sense. Something like vuetify:

vue
https://vuetifyjs.com/en/components/autocompletes/#asynchronous-items

Can you also add a RenderFragment that is displayed inside the popover while it's loading. Something like this in mui:

mui
https://mui.com/components/autocomplete/#asynchronous-requests

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Acknowledged. When I look at this a bit later, I may have a couple clarifying questions. Thanks!

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mthrift2100 Any updates with this feature. Will this be added to any of the upcoming releases

@JonBunator JonBunator added needs: changes A maintainer has asked for further modifications to be made to this pull request needs: tests A maintainer has explicitly asked for associated test cases to be added to this pull request labels Oct 21, 2021
@mikes-gh mikes-gh changed the title MudAutoComplete: New Feature - Loading Indicator & CancellationToken AutoComplete: New Feature - Loading Indicator & CancellationToken Dec 2, 2021
@olegrumiancev
Copy link

Hello everyone, definitely a good feature to have. My question is, after this is merged successfully, will it be available for .NET Core 5 version as well as 6? Thank you.

@mckaragoz
Copy link
Member

Hello everyone, definitely a good feature to have. My question is, after this is merged successfully, will it be available for .NET Core 5 version as well as 6? Thank you.

I know that PR's will added and merged also in net 5, unless it has technical impossibilites with net 5. But of course net 5 cant support forever.

@henon
Copy link
Contributor

henon commented Dec 24, 2021

  • Please merge dev and resolve the conflict.
  • Add categories to the new parameters
  • Write a test that uses the test component
  • Post a new gif to present how it looks after implementing the requested changes

Thanks.

@Garderoben
Copy link
Member

Will close this for inactivity, just make new PR when you/it is ready.

@artworkad
Copy link

@henon @Garderoben I am working on a PR. However I am not sure how to test this. IsLoading is set from within OnSearchAsync. So I have to trigger that and see if the loading indicator is displayed. Not sure how to do that. What I've tried:

[Test]
public async Task Autocomplete_Should_ShowLoadingIndicatorBottom()
{
    var comp = Context.RenderComponent<MudAutocomplete<string>>((a) =>
    {
        a.Add(x => x.DebounceInterval, 0);
        a.Add(x => x.ShowLoadingIndicator, true);
        a.Add(x => x.SearchFunc, new Func<string, Task<IEnumerable<string>>>(async s =>
        {
            await Task.Delay(500);
            return new List<string> { "Foo", "Bar" };
        }));
    });

    comp.SetParam(a => a.Text, "Foo");
    comp.Markup.Should().Contain("mud-progress-circular");
}

I tested the implementation in the demo and it works fine.

@henon
Copy link
Contributor

henon commented Jul 8, 2022

Not sure if it is that easy because of the popup implementation. Did you look if there are any other tests which check what is inside the drop-down? You could learn from them

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement Adds a new feature or enhances existing functionality (not fixing a defect) in the main library needs: changes A maintainer has asked for further modifications to be made to this pull request needs: tests A maintainer has explicitly asked for associated test cases to be added to this pull request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants