Skip to content

Commit 8ac3f2c

Browse files
authored
feat(blazorui): add Params implementation for BitCard #11806 (#11808)
1 parent f65a045 commit 8ac3f2c

File tree

6 files changed

+365
-221
lines changed

6 files changed

+365
-221
lines changed

src/BlazorUI/Bit.BlazorUI.SourceGenerators/Component/ComponentSourceGenerator.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ namespace {namespaceName}
4848
public partial class {className}
4949
{{
5050
");
51+
builder.AppendLine(" private HashSet<string> __assignedParameters = new();");
52+
builder.AppendLine("");
5153
foreach (var par in twoWayParameters)
5254
{
5355
var sym = par.PropertySymbol;
@@ -59,6 +61,7 @@ public partial class {className}
5961
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
6062
public override async Task SetParametersAsync(ParameterView parameters)
6163
{{");
64+
builder.AppendLine($" __assignedParameters.Clear();");
6265
foreach (var par in twoWayParameters)
6366
{
6467
builder.AppendLine($" {par.PropertySymbol.Name}HasBeenSet = false;");
@@ -82,6 +85,7 @@ public override async Task SetParametersAsync(ParameterView parameters)
8285
var varName = $"@{paramName.ToLower()}";
8386
var paramType = sym.Type.ToDisplayString();
8487
builder.AppendLine($" case nameof({paramName}):");
88+
builder.AppendLine($" __assignedParameters.Add(nameof({paramName})); ");
8589
if (par.IsTwoWayBound)
8690
{
8791
builder.AppendLine($" {paramName}HasBeenSet = true;");
@@ -140,6 +144,15 @@ public override async Task SetParametersAsync(ParameterView parameters)
140144
}
141145
builder.AppendLine(@" }");
142146

147+
builder.AppendLine("");
148+
149+
builder.AppendLine($@" [global::System.Diagnostics.DebuggerNonUserCode]
150+
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
151+
public bool HasNotBeenSet(string name)
152+
{{");
153+
builder.AppendLine(" return __assignedParameters.Contains(name) is false;");
154+
builder.AppendLine(" }");
155+
143156
if (twoWayParameters.Length > 0) builder.AppendLine("");
144157
foreach (var par in twoWayParameters)
145158
{

src/BlazorUI/Bit.BlazorUI/Components/Surfaces/Card/BitCard.razor.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
1-
namespace Bit.BlazorUI;
1+
using System.Diagnostics.CodeAnalysis;
2+
3+
namespace Bit.BlazorUI;
24

35
/// <summary>
46
/// A Card provides a container to wrap around a specific content. Keeping a card to a single subject keeps the design clean.
57
/// </summary>
68
public partial class BitCard : BitComponentBase
79
{
10+
[CascadingParameter(Name = BitCardParams.ParamName)]
11+
public BitCardParams? CascadingParameters { get; set; }
12+
13+
14+
815
/// <summary>
916
/// The color kind of the background of the card.
1017
/// </summary>
@@ -75,4 +82,11 @@ protected override void RegisterCssClasses()
7582

7683
ClassBuilder.Register(() => NoShadow ? "bit-crd-nsd" : string.Empty);
7784
}
85+
86+
[DynamicDependency(DynamicallyAccessedMemberTypes.All, typeof(BitCardParams))]
87+
protected override void OnParametersSet()
88+
{
89+
CascadingParameters?.UpdateParameters(this);
90+
base.OnParametersSet();
91+
}
7892
}
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
namespace Bit.BlazorUI;
2+
3+
/// <summary>
4+
/// The parameters for <see cref="BitCard"/> component.
5+
/// </summary>
6+
public class BitCardParams : IBitComponentParams
7+
{
8+
/// <summary>
9+
/// Represents the parameter name used to identify the BitCard cascading parameters within BitParams.
10+
/// </summary>
11+
/// <remarks>
12+
/// This constant is typically used when referencing or accessing the BitCard value in
13+
/// parameterized APIs or configuration settings. Using this constant helps ensure consistency and reduces the risk
14+
/// of typographical errors.
15+
/// </remarks>
16+
public const string ParamName = $"{nameof(BitParams)}.{nameof(BitCard)}";
17+
18+
19+
20+
public string Name => ParamName;
21+
22+
23+
24+
/// <summary>
25+
/// The color kind of the background of the card.
26+
/// </summary>
27+
public BitColorKind? Background { get; set; }
28+
29+
/// <summary>
30+
/// The color kind of the border of the card.
31+
/// </summary>
32+
public BitColorKind? Border { get; set; }
33+
34+
/// <summary>
35+
/// Makes the card height 100% of its parent container.
36+
/// </summary>
37+
public bool? FullHeight { get; set; }
38+
39+
/// <summary>
40+
/// Makes the card width and height 100% of its parent container.
41+
/// </summary>
42+
public bool? FullSize { get; set; }
43+
44+
/// <summary>
45+
/// Makes the card width 100% of its parent container.
46+
/// </summary>
47+
public bool? FullWidth { get; set; }
48+
49+
/// <summary>
50+
/// Removes the default shadow around the card.
51+
/// </summary>
52+
public bool? NoShadow { get; set; }
53+
54+
55+
56+
/// <summary>
57+
/// Updates the properties of the specified <see cref="BitCard"/> instance with any values that have been set on
58+
/// this object, if those properties have not already been set on the <see cref="BitCard"/>.
59+
/// </summary>
60+
/// <remarks>
61+
/// Only properties that have a value set and have not already been set on the <paramref name="bitCard"/> will be updated.
62+
/// This method does not overwrite existing values on <paramref name="bitCard"/>.
63+
/// </remarks>
64+
/// <param name="bitCard">
65+
/// The <see cref="BitCard"/> instance whose properties will be updated. Cannot be null.
66+
/// </param>
67+
public void UpdateParameters(BitCard bitCard)
68+
{
69+
if (bitCard is null) return;
70+
71+
if (Background.HasValue && bitCard.HasNotBeenSet(nameof(Background)))
72+
{
73+
bitCard.Background = Background.Value;
74+
}
75+
76+
if (Border.HasValue && bitCard.HasNotBeenSet(nameof(Border)))
77+
{
78+
bitCard.Border = Border.Value;
79+
}
80+
81+
if (FullHeight.HasValue && bitCard.HasNotBeenSet(nameof(FullHeight)))
82+
{
83+
bitCard.FullHeight = FullHeight.Value;
84+
}
85+
86+
if (FullSize.HasValue && bitCard.HasNotBeenSet(nameof(FullSize)))
87+
{
88+
bitCard.FullSize = FullSize.Value;
89+
}
90+
91+
if (FullWidth.HasValue && bitCard.HasNotBeenSet(nameof(FullWidth)))
92+
{
93+
bitCard.FullWidth = FullWidth.Value;
94+
}
95+
96+
if (NoShadow.HasValue && bitCard.HasNotBeenSet(nameof(NoShadow)))
97+
{
98+
bitCard.NoShadow = NoShadow.Value;
99+
}
100+
}
101+
}

src/BlazorUI/Bit.BlazorUI/Utils/Params/BitParams.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
namespace Bit.BlazorUI;
1+
using System.Diagnostics.CodeAnalysis;
2+
3+
namespace Bit.BlazorUI;
24

35
/// <summary>
46
/// Provides cascading parameter values for the bit BlazorUI components.
@@ -38,13 +40,16 @@ public override Task SetParametersAsync(ParameterView parameters)
3840

3941

4042

43+
[DynamicDependency(DynamicallyAccessedMemberTypes.All, typeof(IBitComponentParams))]
4144
protected override void BuildRenderTree(RenderTreeBuilder builder)
4245
{
4346
var seq = 0;
4447
var values = new List<BitCascadingValue>();
4548

4649
foreach (var compParams in Parameters ?? [])
4750
{
51+
if (compParams is null) continue;
52+
4853
values.Add(new(compParams, compParams.Name, true));
4954
}
5055

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
namespace Bit.BlazorUI;
22

3+
/// <summary>
4+
/// Defines the contract for parameters used by a bit BlazorUI component.
5+
/// </summary>
36
public interface IBitComponentParams
47
{
8+
/// <summary>
9+
/// Gets the name associated with the current instance of BitComponentParams.
10+
/// </summary>
511
string Name { get; }
612
}

0 commit comments

Comments
 (0)