Skip to content

Commit bcbf29f

Browse files
authored
[CounterBadge] Fix HorizontalPosition/VerticalPosition not being used
1 parent 8a14138 commit bcbf29f

5 files changed

+61
-10
lines changed

src/Core/Components/CounterBadge/FluentCounterBadge.razor.cs

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
// ------------------------------------------------------------------------
2+
// MIT License - Copyright (c) Microsoft Corporation. All rights reserved.
3+
// ------------------------------------------------------------------------
4+
15
using System.Globalization;
26
using Microsoft.AspNetCore.Components;
37
using Microsoft.FluentUI.AspNetCore.Components.Extensions;
@@ -18,9 +22,9 @@ public partial class FluentCounterBadge : FluentComponentBase, IDisposable
1822

1923
/// <summary />
2024
protected string? StyleValue => new StyleBuilder(Style)
21-
.AddStyle("left", $"{HorizontalPosition.ToString(CultureInfo.InvariantCulture)}%", () => GlobalState.Dir == LocalizationDirection.LeftToRight)
22-
.AddStyle("right", $"{HorizontalPosition.ToString(CultureInfo.InvariantCulture)}%", () => GlobalState.Dir == LocalizationDirection.RightToLeft)
23-
.AddStyle("bottom", $"{VerticalPosition.ToString(CultureInfo.InvariantCulture)}%")
25+
.AddStyle("left", $"{HorizontalPosition?.ToString(CultureInfo.InvariantCulture)}%", () => GlobalState.Dir == LocalizationDirection.LeftToRight)
26+
.AddStyle("right", $"{HorizontalPosition?.ToString(CultureInfo.InvariantCulture)}%", () => GlobalState.Dir == LocalizationDirection.RightToLeft)
27+
.AddStyle("bottom", $"{VerticalPosition?.ToString(CultureInfo.InvariantCulture)}%")
2428
.AddStyle("background-color", GetBackgroundColor().ToAttributeValue())
2529
.AddStyle("color", GetFontColor().ToAttributeValue())
2630
.AddStyle("border", $"1px solid {GetBorderColor().ToAttributeValue()}")
@@ -50,7 +54,7 @@ public partial class FluentCounterBadge : FluentComponentBase, IDisposable
5054
/// Gets or sets the content you want inside the badge, to customize the badge content.
5155
/// </summary>
5256
[Parameter]
53-
public RenderFragment<int?>? BadgeTemplate{ get; set; }
57+
public RenderFragment<int?>? BadgeTemplate { get; set; }
5458

5559
/// <summary>
5660
/// Gets or sets the maximum number that can be displayed inside the badge.
@@ -64,7 +68,7 @@ public partial class FluentCounterBadge : FluentComponentBase, IDisposable
6468
/// Default value is 60 (80 when Dot=true).
6569
/// </summary>
6670
[Parameter]
67-
public int HorizontalPosition { get; set; }
71+
public int? HorizontalPosition { get; set; }
6872

6973
/// <summary>
7074
/// Gets or sets the bottom position of the badge in percentage.
@@ -75,7 +79,7 @@ public partial class FluentCounterBadge : FluentComponentBase, IDisposable
7579
[Parameter]
7680
public int BottomPosition
7781
{
78-
get => VerticalPosition;
82+
get => VerticalPosition ?? 60;
7983
set => VerticalPosition = value;
8084
}
8185

@@ -84,7 +88,7 @@ public int BottomPosition
8488
/// Default value is 60 (80 when Dot=true).
8589
/// </summary>
8690
[Parameter]
87-
public int VerticalPosition { get; set; }
91+
public int? VerticalPosition { get; set; }
8892

8993
/// <summary>
9094
/// Gets or sets the default design of this badge using colors from theme.
@@ -160,8 +164,9 @@ protected override Task OnParametersSetAsync()
160164

161165
protected override void OnInitialized()
162166
{
163-
HorizontalPosition = Dot ? 80 : 60;
164-
VerticalPosition = Dot ? 80 : 60;
167+
HorizontalPosition ??= Dot ? 80 : 60;
168+
VerticalPosition ??= Dot ? 80 : 60;
169+
165170
GlobalState.OnChange += StateHasChanged;
166171
}
167172

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2+
<div class="fluentui-counterbadge-container" b-v8ui8wcemu="">
3+
<div class="fluentui-counterbadge" style="left: -25%; bottom: -25%; background-color: var(--accent-fill-rest); color: var(--neutral-fill-rest); border: 1px solid var(--accent-fill-rest);" dot-only="" b-v8ui8wcemu=""></div>
4+
</div>
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2+
<div class="fluentui-counterbadge-container" b-v8ui8wcemu="">
3+
<div class="fluentui-counterbadge" style="left: -25%; bottom: -25%; background-color: var(--accent-fill-rest); color: var(--neutral-fill-rest); border: 1px solid var(--accent-fill-rest);" title="1" b-v8ui8wcemu="">1</div>
4+
</div>

tests/Core/CounterBadge/FluentCounterBadgeTests.cs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -454,4 +454,39 @@ public void FluentCounterBadge_DotWithCount()
454454
// Assert
455455
cut.Verify();
456456
}
457+
458+
[Fact]
459+
public void FluentCounterBadge_DotWithPositioning()
460+
{
461+
// Arrange && Act
462+
TestContext.Services.AddSingleton(GlobalState);
463+
464+
var cut = TestContext.RenderComponent<FluentCounterBadge>(parameters =>
465+
{
466+
parameters.Add(p => p.Dot, true);
467+
parameters.Add(p => p.Count, 1);
468+
parameters.Add(parameters => parameters.HorizontalPosition, -25);
469+
parameters.Add(parameters => parameters.VerticalPosition, -25);
470+
});
471+
472+
// Assert
473+
cut.Verify();
474+
}
475+
476+
[Fact]
477+
public void FluentCounterBadge_WithPositioning()
478+
{
479+
// Arrange && Act
480+
TestContext.Services.AddSingleton(GlobalState);
481+
482+
var cut = TestContext.RenderComponent<FluentCounterBadge>(parameters =>
483+
{
484+
parameters.Add(p => p.Count, 1);
485+
parameters.Add(parameters => parameters.HorizontalPosition, -25);
486+
parameters.Add(parameters => parameters.VerticalPosition, -25);
487+
});
488+
489+
// Assert
490+
cut.Verify();
491+
}
457492
}

tests/Core/Search/FluentSearchTests.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
// ------------------------------------------------------------------------
2+
// MIT License - Copyright (c) Microsoft Corporation. All rights reserved.
3+
// ------------------------------------------------------------------------
4+
15
using Bunit;
26
using Microsoft.Extensions.DependencyInjection;
37
using Xunit;
@@ -12,7 +16,6 @@ public FluentSearchTests()
1216
Services.AddSingleton(LibraryConfiguration.ForUnitTests);
1317
}
1418

15-
1619
[Fact]
1720
public void FluentSearch_Default()
1821
{

0 commit comments

Comments
 (0)