-
-
Notifications
You must be signed in to change notification settings - Fork 121
Expand file tree
/
Copy pathComponentParameterCollection.cs
More file actions
241 lines (196 loc) · 7.98 KB
/
Copy pathComponentParameterCollection.cs
File metadata and controls
241 lines (196 loc) · 7.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
using System.Reflection;
namespace Bunit;
/// <summary>
/// A collection for <see cref="ComponentParameter" />.
/// </summary>
internal class ComponentParameterCollection : ICollection<ComponentParameter>, IReadOnlyCollection<ComponentParameter>
{
private static readonly MethodInfo CreateTemplateWrapperMethod = GetCreateTemplateWrapperMethod();
private static readonly Type CascadingValueType = typeof(CascadingValue<>);
private readonly List<ComponentParameter> parameters = new();
/// <summary>
/// Gets the number of <see cref="ComponentParameter"/> in the collection.
/// </summary>
public int Count => parameters.Count;
/// <inheritdoc />
public bool IsReadOnly => false;
#if NET9_0_OR_GREATER
/// <summary>
/// Gets or sets the <see cref="IComponentRenderMode"/> that will be specified in
/// the render tree for component the parameters are being passed to.
/// </summary>
public IComponentRenderMode? RenderMode { get; set; }
#endif
/// <summary>
/// Adds a <paramref name="item"/> to the collection.
/// </summary>
/// <param name="item">Parameter to add to the collection.</param>
public void Add(ComponentParameter item)
{
if (item.Name is null && item.Value is null)
throw new ArgumentException("A component parameter without a name and value is not valid.", nameof(item));
parameters.Add(item);
}
/// <summary>
/// Adds an enumerable of parameters to the collection.
/// </summary>
/// <param name="parameters">Parameters to add.</param>
public void Add(IEnumerable<ComponentParameter> parameters)
{
ArgumentNullException.ThrowIfNull(parameters);
foreach (var cp in parameters)
{
Add(cp);
}
}
/// <summary>
/// Checks if the <paramref name="item"/> is in the collection.
/// </summary>
/// <param name="item">Parameter to check with.</param>
/// <returns>True if <paramref name="item"/> is in the collection, false otherwise.</returns>
public bool Contains(ComponentParameter item) => parameters.Contains(item);
/// <inheritdoc/>
public void Clear() => parameters.Clear();
/// <inheritdoc/>
public void CopyTo(ComponentParameter[] array, int arrayIndex) => parameters.CopyTo(array, arrayIndex);
/// <inheritdoc/>
public bool Remove(ComponentParameter item) => parameters.Remove(item);
/// <summary>
/// Creates a <see cref="RenderFragment"/> that will render a
/// component of type <typeparamref name="TComponent"/> with
/// the parameters in the collection passed to it.
/// </summary>
/// <typeparam name="TComponent">Type of component to render.</typeparam>
public RenderFragment ToRenderFragment<TComponent>()
where TComponent : IComponent
{
var cascadingValues = GetCascadingValues();
return cascadingValues.Count > 0
? AddCascadingValue
: AddComponent;
void AddCascadingValue(RenderTreeBuilder builder)
{
var cv = cascadingValues.Dequeue();
builder.OpenComponent(0, cv.Type);
if (cv.Parameter.Name is not null)
builder.AddAttribute(1, nameof(CascadingValue<object>.Name), cv.Parameter.Name);
builder.AddAttribute(2, nameof(CascadingValue<object>.Value), cv.Parameter.Value);
builder.AddAttribute(3, nameof(CascadingValue<object>.IsFixed), value: true);
if (cascadingValues.Count > 0)
builder.AddAttribute(4, nameof(CascadingValue<object>.ChildContent), (RenderFragment)AddCascadingValue);
else
builder.AddAttribute(4, nameof(CascadingValue<object>.ChildContent), (RenderFragment)AddComponent);
builder.CloseComponent();
}
void AddComponent(RenderTreeBuilder builder)
{
builder.OpenComponent<TComponent>(0);
AddAttributes(builder);
#if NET9_0_OR_GREATER
builder.AddComponentRenderMode(RenderMode);
#endif
builder.CloseComponent();
}
void AddAttributes(RenderTreeBuilder builder)
{
var attrCount = 100;
foreach (var pgroup in parameters.Where(x => !x.IsCascadingValue).GroupBy(x => x.Name, StringComparer.Ordinal))
{
var group = pgroup.ToArray();
var groupObject = Array.Find(group, x => x.Value is not null).Value;
if (group.Length == 1)
{
var p = group[0];
builder.AddAttribute(
attrCount++,
p.Name!, // BANG: ComponentParameter does not allow a regular param to be created without a name
p.Value);
continue;
}
if (groupObject is RenderFragment)
{
builder.AddAttribute(
attrCount++,
group[0].Name!, // BANG: ComponentParameter does not allow a regular param to be created without a name
(RenderFragment)(ccBuilder =>
{
for (var i = 0; i < group.Length; i++)
{
if (group[i].Value is RenderFragment rf)
ccBuilder.AddContent(i, rf);
}
}));
continue;
}
var groupType = groupObject?.GetType();
if (groupType is { IsGenericType: true } && groupType.GetGenericTypeDefinition() == typeof(RenderFragment<>))
{
builder.AddAttribute(
attrCount++,
group[0].Name!, // BANG: ComponentParameter does not allow a regular param to be created without a name
WrapTemplates(groupType, group));
continue;
}
throw new ArgumentException($"The parameter with the name '{pgroup.Key}' was added more than once. This parameter can only be added one time.");
}
}
Queue<(ComponentParameter Parameter, Type Type)> GetCascadingValues()
{
var cascadingValues = parameters
.Where(x => x.IsCascadingValue)
.Select(x => (Parameter: x, Type: GetCascadingValueType(x)))
.ToArray();
var duplicate = cascadingValues
.GroupBy(x => new { x.Type, x.Parameter.Name })
.FirstOrDefault(g => g.Count() > 1);
if (duplicate is not null)
{
var name = duplicate.Key.Name;
var type = duplicate.First().Type.GetGenericArguments()[0];
throw new ArgumentException($"Two or more unnamed cascading values with the type '{name ?? type.Name}' was added. " +
$"Only add one unnamed cascading value of the same type.");
}
return new Queue<(ComponentParameter Parameter, Type Type)>(cascadingValues);
}
}
/// <inheritdoc/>
public IEnumerator<ComponentParameter> GetEnumerator() => parameters.GetEnumerator();
/// <inheritdoc/>
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() => GetEnumerator();
private static object WrapTemplates(Type templateParameterType, ComponentParameter[] templateParameters)
{
// gets the generic argument to RenderFragment<>, e.g. string with RenderFragment<string>
var templateType = templateParameterType.GetGenericArguments()[0];
// this creates an invokable version of CreateTemplateWrapper with the
// generic type set to templateType, e.g. CreateTemplateWrapper<string>
var templateWrapper = CreateTemplateWrapperMethod.MakeGenericMethod(templateType);
// BANG: since CreateTemplateWrapper<T> will never return null BANG (!) is safe here
return templateWrapper.Invoke(null, new object[] { templateParameters })!;
}
private static RenderFragment<T> CreateTemplateWrapper<T>(ComponentParameter[] subTemplateParams)
{
return input => builder =>
{
foreach (var tp in subTemplateParams)
{
if (tp.Value is RenderFragment<T> rf)
builder.AddContent(0, rf(input));
else
throw new ArgumentException($"The parameter with name {tp.Name} was different types of templates.", tp.Name);
}
};
}
private static Type GetCascadingValueType(ComponentParameter parameter)
{
if (parameter.Value is null)
throw new InvalidOperationException("Cannot get the type of a null object");
var cascadingValueType = parameter.Value.GetType();
return CascadingValueType.MakeGenericType(cascadingValueType);
}
[SuppressMessage("Major Code Smell", "S3011:Reflection should not be used to increase accessibility of classes, methods, or fields", Justification = "Accesses private method in this type.")]
private static MethodInfo GetCreateTemplateWrapperMethod()
{
var result = typeof(ComponentParameterCollection).GetMethod(nameof(CreateTemplateWrapper), BindingFlags.NonPublic | BindingFlags.Static);
return result ?? throw new InvalidOperationException($"Could not find the {nameof(CreateTemplateWrapper)} method.");
}
}