Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 26 additions & 26 deletions CodeBeam.MudExtensions.UnitTests/Components/SelectExtendedTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,32 +116,32 @@ public void Select_ValueBubblingTest_MultiSelection()
select.Instance.Text.Should().Be("2, 1");
}

//[Test]
//public async Task Select_ValueChangeEventCountTest()
//{
// var comp = Context.RenderComponent<SelectEventCountTest>(x =>
// {
// x.Add(c => c.MultiSelection, false);
// });
// var select = comp.FindComponent<MudSelectExtended<string>>();
// var input = comp.Find("div.mud-input-control");

// comp.Instance.ValueChangeCount.Should().Be(0);
// comp.Instance.ValuesChangeCount.Should().Be(0);

// await comp.InvokeAsync(() => select.SetParam("Value", "1"));
// await comp.InvokeAsync(() => select.Instance.ForceUpdate());
// comp.WaitForAssertion(() => comp.Instance.ValueChangeCount.Should().Be(1));
// comp.Instance.ValuesChangeCount.Should().Be(1);
// select.Instance.Value.Should().Be("1");

// // Changing value programmatically without ForceUpdate should change value, but should not fire change events
// // Its by design, so this part can be change if design changes
// await comp.InvokeAsync(() => select.SetParam("Value", "2"));
// comp.WaitForAssertion(() => comp.Instance.ValueChangeCount.Should().Be(1));
// comp.Instance.ValuesChangeCount.Should().Be(1);
// select.Instance.Value.Should().Be("2");
//}
[Test]
public async Task Select_ValueChangeEventCountTest()
{
var comp = Context.RenderComponent<SelectEventCountTest>(x =>
{
x.Add(c => c.MultiSelection, false);
});
var select = comp.FindComponent<MudSelectExtended<string>>();
var input = comp.Find("div.mud-input-control");

comp.Instance.ValueChangeCount.Should().Be(0);
comp.Instance.ValuesChangeCount.Should().Be(0);

await comp.InvokeAsync(() => select.SetParam("Value", "1"));
await comp.InvokeAsync(() => select.Instance.ForceUpdate());
comp.WaitForAssertion(() => comp.Instance.ValueChangeCount.Should().Be(1));
comp.Instance.ValuesChangeCount.Should().Be(1);
select.Instance.Value.Should().Be("1");

// Changing value programmatically without ForceUpdate should change value, but should not fire change events
// Its by design, so this part can be change if design changes
await comp.InvokeAsync(() => select.SetParam("Value", "2"));
comp.WaitForAssertion(() => comp.Instance.ValueChangeCount.Should().Be(1));
comp.Instance.ValuesChangeCount.Should().Be(1);
select.Instance.Value.Should().Be("2");
}

[Test]
public async Task Select_ValueChangeEventCountTest_MultiSelection()
Expand Down
25 changes: 17 additions & 8 deletions CodeBeam.MudExtensions/Base/MudBaseInputExtended.cs
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,15 @@ protected MudBaseInputExtended() : base(new DefaultConverter<T>()) { }
[Category(CategoryTypes.FormComponent.Validation)]
public virtual string Pattern { get; set; }

/// <summary>
/// Sync the value, values and text, calls validation manually. Useful to call after user changes value or text programmatically.
/// </summary>
/// <returns></returns>
public virtual async Task ForceUpdate()
{
await SetValueAsync(Value, force: true);
}

/// <summary>
/// Derived classes need to override this if they can be something other than text
/// </summary>
Expand Down Expand Up @@ -296,10 +305,10 @@ protected internal virtual void OnBlurred(FocusEventArgs obj)
/// </summary>
[Parameter] public EventCallback<KeyboardEventArgs> OnKeyDown { get; set; }

protected virtual void InvokeKeyDown(KeyboardEventArgs obj)
protected virtual async Task InvokeKeyDown(KeyboardEventArgs obj)
{
_isFocused = true;
OnKeyDown.InvokeAsync(obj).AndForget();
await OnKeyDown.InvokeAsync(obj);
}

/// <summary>
Expand All @@ -315,9 +324,9 @@ protected virtual void InvokeKeyDown(KeyboardEventArgs obj)
/// </summary>
[Parameter] public EventCallback<KeyboardEventArgs> OnKeyPress { get; set; }

protected virtual void InvokeKeyPress(KeyboardEventArgs obj)
protected virtual async Task InvokeKeyPress(KeyboardEventArgs obj)
{
OnKeyPress.InvokeAsync(obj).AndForget();
await OnKeyPress.InvokeAsync(obj);
}

/// <summary>
Expand All @@ -332,10 +341,10 @@ protected virtual void InvokeKeyPress(KeyboardEventArgs obj)
/// </summary>
[Parameter] public EventCallback<KeyboardEventArgs> OnKeyUp { get; set; }

protected virtual void InvokeKeyUp(KeyboardEventArgs obj)
protected virtual async Task InvokeKeyUp(KeyboardEventArgs obj)
{
_isFocused = true;
OnKeyUp.InvokeAsync(obj).AndForget();
await OnKeyUp.InvokeAsync(obj);
}

/// <summary>
Expand All @@ -362,9 +371,9 @@ public T Value
set => _value = value;
}

protected virtual async Task SetValueAsync(T value, bool updateText = true)
protected virtual async Task SetValueAsync(T value, bool updateText = true, bool force = false)
{
if (!EqualityComparer<T>.Default.Equals(Value, value))
if (!EqualityComparer<T>.Default.Equals(Value, value) || force == true)
{
_isDirty = true;
Value = value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
Value="@(Strict && !IsValueInList ? null : Text)" DisableUnderLine="@DisableUnderLine"
Disabled="@Disabled" ReadOnly="true" Error="@Error" ErrorId="@ErrorId"
Clearable="@Clearable" OnClearButtonClick="(async (e) => await SelectClearButtonClickHandlerAsync(e))"
@attributes="UserAttributes" OnBlur="@OnLostFocus">
@attributes="UserAttributes" OnBlur="@OnLostFocus" ForceShrink="@ForceShrink">

<AdornmentEnd>
<MudIcon Icon="@_currentIcon" Color="@AdornmentColor" Size="@IconSize" @onclick="OnAdornmentClick" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -903,9 +903,9 @@ public async Task SelectOption(object obj)
}

//TODO: will override this method when core library will have the base one.
public async Task ForceUpdate()
public override async Task ForceUpdate()
{
//await base.ForceUpdate();
await base.ForceUpdate();
if (!MultiSelection)
{
SelectedValues = new HashSet<T>(_comparer) { Value };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ public IMask Mask
}
}

protected override Task SetValueAsync(T value, bool updateText = true)
protected override Task SetValueAsync(T value, bool updateText = true, bool force = false)
{
if (_mask != null)
{
Expand All @@ -150,7 +150,7 @@ protected override Task SetValueAsync(T value, bool updateText = true)
textValue=Mask.GetCleanText();
value = Converter.Get(textValue);
}
return base.SetValueAsync(value, updateText);
return base.SetValueAsync(value, updateText, force);
}

protected override Task SetTextAsync(string text, bool updateValue = true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,19 @@
<MudGrid>
<MudItem xs="12" sm="8" Class="d-flex justify-center">
<MudPaper Width="200px" Elevation="0">
<MudTextFieldExtended T="string" Lines="2" Immediate="true" AutoSize="_autosize" Label="AutoSized TextField" Variant="Variant.Filled" />
<MudTextFieldExtended T="string" Lines="2" Immediate="true" AutoSize="_autosize" Label="AutoSized TextField" Variant="Variant.Filled" ForceShrink="_forceShrink" />
</MudPaper>
</MudItem>

<MudItem xs="12" sm="4">
<MudStack>
<MudSwitchM3 @bind-Checked="_autosize" Label="AutoSize" Color="Color.Primary" />
<MudSwitchM3 @bind-Checked="_forceShrink" Label="Force Shrink" Color="Color.Primary" />
</MudStack>
</MudItem>
</MudGrid>

@code {
bool _autosize = true;
bool _forceShrink = false;
}