Skip to content
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

fix: Cross Dependent sliders do not reset thumb after range is updated. #1836

Closed
russellSmithMS opened this issue Apr 11, 2024 · 5 comments
Closed
Labels
area:fast A FAST-specific issue closed:done Work is finished community:contribution Issue will/can be addressed by community contribution

Comments

@russellSmithMS
Copy link

🐛 Bug Report

I have a Blazor app that I am using to display electrical usage over time. On my page, I have 4 sliders. 2 represent the years associated with the data and the other 2 represent the months. These are used for min/max. When I drag the start year, I want the min option for end year to be no smaller than the selected start year. Same goes for Start/end month.
The underlying selected (bound) values are correct, the display of the slider bar is what does not line up.

💻 Repro or Code Sample

This is a page I created to repro this issue.

@page "/"
@rendermode InteractiveServer
<PageTitle>Home</PageTitle>

<FluentGrid Style="width:100%">
    <FluentGridItem xs="12">
        <h1>Related Sliders</h1>
    </FluentGridItem>
    <FluentGridItem xs="2" Justify="JustifyContent.FlexEnd">From Month</FluentGridItem>
    <FluentGridItem xs="4">
        <FluentSlider Step="1" Min="1" Max="EndMonth" @bind-Value="StartMonth">
            @foreach (var sy in GetCollection(1, EndMonth))
            {
                <FluentSliderLabel Position="@sy">@sy</FluentSliderLabel>
            }
        </FluentSlider>
    </FluentGridItem> 
    <FluentGridItem xs="2" Justify="JustifyContent.FlexEnd">To Month</FluentGridItem>
    <FluentGridItem xs="4">
        <FluentSlider Step="1" Min="StartMonth" Max="12" @bind-Value="EndMonth">
            @foreach (var sy in GetCollection(StartMonth, 12))
            {
                <FluentSliderLabel Position="@sy">@sy</FluentSliderLabel>
            }
        </FluentSlider>
    </FluentGridItem>
</FluentGrid>
@code {
    private int StartMonth = 1;
    private int EndMonth = 12;

    public IEnumerable<int> GetCollection(int start, int end) => Enumerable.Range(start, end - start + 1);
}

This is what the output looks like
image

When I slide the To Month to 6 The from month's max value changes to 6 and the steps reflect properly.
image

But when I then change the From month to 5, the To Month slider stays pinned to middle of the line, but the steps update to reflect the correct choices.
image

When I try to slide the To Month again, it jumps to the step that it's closest to (in my example, between 8 and 9, and I then have to move the slider again back to 6 for both sliders to look correct.
image

But, if I again, change the From Month back to 1, the To Month no longer lines up as expected.
image

🤔 Expected Behavior

Both the slider indicator and the steps should be reflecting the same scale/numbers

😯 Current Behavior

Steps do get updated, but bar and button/thumb does not chanage.

💁 Possible Solution

When the Min/Max values are dependent on flexible values, the position should render last.

🔦 Context

🌍 Your Environment

  • OS & Device: [VS 2022 Preview, Win11 ]
  • Browser [Brave, Edge, Chrome]
  • .NET and Fluent UI Blazor library Version [.NET 8 Fluent 4.6.1]
@microsoft-github-policy-service microsoft-github-policy-service bot added the triage New issue. Needs to be looked at label Apr 11, 2024
@vnbaaij vnbaaij added area:fast A FAST-specific issue status:blocked Any issue blocked by another and removed triage New issue. Needs to be looked at labels Apr 12, 2024
@vnbaaij
Copy link
Collaborator

vnbaaij commented Apr 12, 2024

Hi,

Unfortunately, the needed sync is not hooked up in the underlying web component.

There exists a calculateNewValue function that updates the value based on current position (by using a convertPixelToPercent function), but that only gets called on mouse move and mouse down action, not when the value is changed programmatically

please raise an issue in the microsoft/fast repo for this

@oneolddev
Copy link
Contributor

oneolddev commented Apr 14, 2024

@russellSmithMS

I have a temporary work-around if you are interested. I derived a new component called MyFluentSlider from FluentSlider that you can use in it's place. You'll need to add 3 files to project.

MyFluentSlider.razor

@inherits FluentSlider<TValue>
@typeparam TValue
@attribute [CascadingTypeParameter(nameof(TValue))]

<div @ref=container>
    @{
        base.BuildRenderTree(__builder);
    }
</div>

MyFluentSlider.razor.cs

using Microsoft.AspNetCore.Components;
using Microsoft.FluentUI.AspNetCore.Components;
using Microsoft.JSInterop;

namespace Server.Components;

public partial class MyFluentSlider<TValue> : FluentSlider<TValue>, IAsyncDisposable
    where TValue : System.Numerics.INumber<TValue>
{
    private const string JAVASCRIPT_FILE = "./MyFluentSlider.js";
    private ElementReference? container;

    /// <summary />
    [Inject]
    private IJSRuntime JSRuntime { get; set; } = default!;

    /// <summary />
    private IJSObjectReference? Module { get; set; }

    protected override async Task OnAfterRenderAsync(bool firstRender)
    {
        if (firstRender)
        {
            Module ??= await JSRuntime.InvokeAsync<IJSObjectReference>("import", JAVASCRIPT_FILE);
        }
        await Module!.InvokeVoidAsync("sliderUpdate", container);
    }
    public ValueTask DisposeAsync()
    {
        if (Module is not null)
        {
            return Module.DisposeAsync();
        }
        return ValueTask.CompletedTask;
    }
}

MyFluentSlider.js

export function sliderUpdate(ref) {
    var fs = ref.getElementsByTagName('fluent-slider')[0];

    // fixes issue where you can't reset value programmatically
    fs.value = fs.attributes['value'].value;

    // fixes issue where the value indicator is not redrawn when either the min or max is changed
    fs.setThumbPositionForOrientation(fs.direction);
}

If you are using the folder layout from the fluentblazor template, place the first two files in the Components folder. Place the javascript file directly in the wwwroot folder. Change all FluentSlider to MyFluentSlider.

@vnbaaij
Copy link
Collaborator

vnbaaij commented Apr 14, 2024

Nice one Gary!

I think we can add that to the regular slider in the lib.

Fancy doing a PR for that?

@oneolddev
Copy link
Contributor

Nice one Gary!

I think we can add that to the regular slider in the lib.

Fancy doing a PR for that?

Sure 😄. I was hesitating suggesting a PR until I could verify the root cause. My adhoc preliminary testing of the fluent-slider component independently didn't seem to reveal a problem with it. I'd like to do some more investigating before submitting a PR.

@vnbaaij vnbaaij added community:contribution Issue will/can be addressed by community contribution and removed status:blocked Any issue blocked by another labels Apr 14, 2024
vnbaaij added a commit that referenced this issue Apr 17, 2024
* redraws thumb for slider when min or max changes

* allows the slider value to be changed programmatically

---------

Co-authored-by: Vincent Baaij <[email protected]>
@vnbaaij vnbaaij added the closed:done Work is finished label Apr 17, 2024
@vnbaaij
Copy link
Collaborator

vnbaaij commented Apr 17, 2024

Fixed with PR mentioned above. Will be in next release. Closing this.

@vnbaaij vnbaaij closed this as completed Apr 17, 2024
vnbaaij added a commit that referenced this issue Apr 23, 2024
* redraws thumb for slider when min or max changes

* allows the slider value to be changed programmatically

---------

Co-authored-by: Vincent Baaij <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area:fast A FAST-specific issue closed:done Work is finished community:contribution Issue will/can be addressed by community contribution
Projects
None yet
Development

No branches or pull requests

3 participants