Skip to content

Commit

Permalink
[Fix] 20 Autocomplete Bug (alternative solution) (#155)
Browse files Browse the repository at this point in the history
* Fix the problem

* Rename file

---------

Co-authored-by: Vincent Wilms <[email protected]>
  • Loading branch information
Apollo3zehn and Vincent Wilms authored Oct 2, 2024
1 parent f25775a commit 0b1ef48
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 59 deletions.
2 changes: 1 addition & 1 deletion src/Nexus.UI/Components/Leftbar_Settings.razor
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
Label="Period"
CoerceValue="true"
SearchFunc="_ => Task.FromResult(_values.AsEnumerable())"
Converter="PeriodHelper.Converter" />
Converter="PeriodHelper.CreateConverter(getCurrentValue: () => AppState.Settings.SamplePeriod)" />
</div>

@code {
Expand Down
2 changes: 1 addition & 1 deletion src/Nexus.UI/Components/Rightbar_Settings.razor
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
Label="File Period"
CoerceValue="true"
SearchFunc="_ => Task.FromResult(_values.AsEnumerable())"
Converter="PeriodHelper.Converter" />
Converter="PeriodHelper.CreateConverter(getCurrentValue: () => AppState.Settings.FilePeriod)" /> />

@if (AppState.Settings.WriterDescriptions is not null)
{
Expand Down
5 changes: 0 additions & 5 deletions src/Nexus.UI/Controls/UIAutocomplete.razor
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,4 @@
//https://github.com/MudBlazor/MudBlazor/issues/6535
await SetValueAsync(value!, updateText: false);
}

protected override async Task SetValueAsync(T value, bool updateText = true, bool force = false)
{
await base.SetValueAsync(value, updateText, force);
}
}
63 changes: 63 additions & 0 deletions src/Nexus.UI/Core/Period.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// MIT License
// Copyright (c) [2024] [nexus-main]

using MudBlazor;

namespace Nexus.UI.Core;

public record Period
{
internal const string SINGLE_FILE_LABEL = "Single File";

public Period(TimeSpan value)
{
Value = value;
}

public TimeSpan Value { get; }

public override string ToString()
{
return Value.Equals(default)
? SINGLE_FILE_LABEL
: Utilities.ToUnitString(Value);
}
}

public static class PeriodHelper
{
public static Converter<Period> CreateConverter(Func<Period> getCurrentValue)
{
return new()
{
SetFunc = value => value != null ? value.ToString() : "",
GetFunc = text => GetFunc(text, getCurrentValue())
};
}

private static Period GetFunc(string? text, Period currentValue)
{
/* Whenever the input is invalid, the current value of the
* autocomplete input this converter belongs to is returned.
* This ensures that the input's current value does not change
* which in turn allows the user to continue typing until a
* valid value is entered. A valid value is a number + unit,
* e.g. "10 min".
*/

if (text is null)
return currentValue;

try
{
return text == Period.SINGLE_FILE_LABEL
? new Period(TimeSpan.Zero)
: new Period(Utilities.ToPeriod(text));
}

catch
{
return currentValue;
}
}
}
52 changes: 0 additions & 52 deletions src/Nexus.UI/Core/PeriodConverter.cs

This file was deleted.

0 comments on commit 0b1ef48

Please sign in to comment.