-
-
Notifications
You must be signed in to change notification settings - Fork 119
Expand file tree
/
Copy pathComponentParameterFactory.cs
More file actions
240 lines (221 loc) · 12 KB
/
ComponentParameterFactory.cs
File metadata and controls
240 lines (221 loc) · 12 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
using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Rendering;
namespace Bunit
{
/// <summary>
/// <see cref="ComponentParameter"/> factory methods.
/// </summary>
public static class ComponentParameterFactory
{
/// <summary>
/// Creates a <see cref="ComponentParameter"/> with an <see cref="Microsoft.AspNetCore.Components.EventCallback"/> that will call the provided <paramref name="callback"/>.
/// </summary>
/// <param name="name">Parameter name.</param>
/// <param name="callback">The event callback.</param>
/// <returns>The <see cref="ComponentParameter"/>.</returns>
public static ComponentParameter EventCallback(string name, Action callback)
{
return ComponentParameter.CreateParameter(name, new EventCallback(null, callback));
}
/// <summary>
/// Creates a <see cref="ComponentParameter"/> with an <see cref="Microsoft.AspNetCore.Components.EventCallback"/> that will call the provided <paramref name="callback"/>.
/// </summary>
/// <param name="name">Parameter name.</param>
/// <param name="callback">The event callback.</param>
/// <returns>The <see cref="ComponentParameter"/>.</returns>
public static ComponentParameter EventCallback(string name, Action<object> callback)
{
return ComponentParameter.CreateParameter(name, new EventCallback(null, callback));
}
/// <summary>
/// Creates a <see cref="ComponentParameter"/> with an <see cref="Microsoft.AspNetCore.Components.EventCallback"/> that will call the provided <paramref name="callback"/>.
/// </summary>
/// <param name="name">Parameter name.</param>
/// <param name="callback">The event callback.</param>
/// <returns>The <see cref="ComponentParameter"/>.</returns>
public static ComponentParameter EventCallback(string name, Func<Task> callback)
{
return ComponentParameter.CreateParameter(name, new EventCallback(null, callback));
}
/// <summary>
/// Creates a <see cref="ComponentParameter"/> with an <see cref="Microsoft.AspNetCore.Components.EventCallback"/> that will call the provided <paramref name="callback"/>.
/// </summary>
/// <param name="name">Parameter name.</param>
/// <param name="callback">The event callback.</param>
/// <returns>The <see cref="ComponentParameter"/>.</returns>
public static ComponentParameter EventCallback(string name, Func<object, Task> callback)
{
return ComponentParameter.CreateParameter(name, new EventCallback(null, callback));
}
/// <summary>
/// Creates a <see cref="ComponentParameter"/> with an <see cref="Microsoft.AspNetCore.Components.EventCallback"/> that will call the provided <paramref name="callback"/>.
/// </summary>
/// <param name="name">Parameter name.</param>
/// <param name="callback">The event callback.</param>
/// <returns>The <see cref="ComponentParameter"/>.</returns>
public static ComponentParameter EventCallback<TValue>(string name, Action callback)
{
return ComponentParameter.CreateParameter(name, new EventCallback<TValue>(null, callback));
}
/// <summary>
/// Creates a <see cref="ComponentParameter"/> with an <see cref="Microsoft.AspNetCore.Components.EventCallback"/> that will call the provided <paramref name="callback"/>.
/// </summary>
/// <param name="name">Parameter name.</param>
/// <param name="callback">The event callback.</param>
/// <returns>The <see cref="ComponentParameter"/>.</returns>
public static ComponentParameter EventCallback<TValue>(string name, Action<TValue> callback)
{
return ComponentParameter.CreateParameter(name, new EventCallback<TValue>(null, callback));
}
/// <summary>
/// Creates a <see cref="ComponentParameter"/> with an <see cref="Microsoft.AspNetCore.Components.EventCallback"/> that will call the provided <paramref name="callback"/>.
/// </summary>
/// <param name="name">Parameter name.</param>
/// <param name="callback">The event callback.</param>
/// <returns>The <see cref="ComponentParameter"/>.</returns>
public static ComponentParameter EventCallback<TValue>(string name, Func<Task> callback)
{
return ComponentParameter.CreateParameter(name, new EventCallback<TValue>(null, callback));
}
/// <summary>
/// Creates a <see cref="ComponentParameter"/> with an <see cref="Microsoft.AspNetCore.Components.EventCallback"/> that will call the provided <paramref name="callback"/>.
/// </summary>
/// <param name="name">Parameter name.</param>
/// <param name="callback">The event callback.</param>
/// <returns>The <see cref="ComponentParameter"/>.</returns>
public static ComponentParameter EventCallback<TValue>(string name, Func<TValue, Task> callback)
{
return ComponentParameter.CreateParameter(name, new EventCallback<TValue>(null, callback));
}
/// <summary>
/// Creates a component parameter which can be passed to a test contexts render methods.
/// </summary>
/// <param name="name">Parameter name</param>
/// <param name="value">Value or null of the parameter</param>
/// <returns>The <see cref="ComponentParameter"/>.</returns>
public static ComponentParameter Parameter(string name, object? value)
{
return ComponentParameter.CreateParameter(name, value);
}
/// <summary>
/// Creates a cascading value which can be passed to a test contexts render methods.
/// </summary>
/// <param name="name">Parameter name</param>
/// <param name="value">Value of the parameter</param>
/// <returns>The <see cref="ComponentParameter"/>.</returns>
public static ComponentParameter CascadingValue(string name, object value)
{
return ComponentParameter.CreateCascadingValue(name, value);
}
/// <summary>
/// Creates a cascading value which can be passed to a test contexts render methods.
/// </summary>
/// <param name="value">Value of the parameter</param>
/// <returns>The <see cref="ComponentParameter"/>.</returns>
public static ComponentParameter CascadingValue(object value)
{
return ComponentParameter.CreateCascadingValue(null, value);
}
/// <summary>
/// Creates a ChildContent <see cref="Microsoft.AspNetCore.Components.RenderFragment"/> with the provided
/// <paramref name="markup"/> as rendered output.
/// </summary>
/// <param name="markup">Markup to pass to the child content parameter</param>
/// <returns>The <see cref="ComponentParameter"/>.</returns>
public static ComponentParameter ChildContent(string markup)
{
return RenderFragment(nameof(ChildContent), markup);
}
/// <summary>
/// Creates a ChildContent <see cref="Microsoft.AspNetCore.Components.RenderFragment"/> which will render a <typeparamref name="TComponent"/> component
/// with the provided <paramref name="parameters"/> as input.
/// </summary>
/// <typeparam name="TComponent">The type of the component to render with the <see cref="Microsoft.AspNetCore.Components.RenderFragment"/></typeparam>
/// <param name="parameters">Parameters to pass to the <typeparamref name="TComponent"/>.</param>
/// <returns>The <see cref="ComponentParameter"/>.</returns>
public static ComponentParameter ChildContent<TComponent>(params ComponentParameter[] parameters) where TComponent : class, IComponent
{
return RenderFragment<TComponent>(nameof(ChildContent), parameters);
}
/// <summary>
/// Creates a ChildContent parameter that will pass the provided <paramref name="renderFragment"/>
/// to the parameter in the component.
/// </summary>
/// <param name="renderFragment">The <see cref="Microsoft.AspNetCore.Components.RenderFragment"/> to pass to the ChildContent parameter.</param>
/// <returns>The <see cref="ComponentParameter"/>.</returns>
public static ComponentParameter ChildContent(RenderFragment renderFragment)
{
return Parameter(nameof(ChildContent), renderFragment);
}
/// <summary>
/// Creates a <see cref="Microsoft.AspNetCore.Components.RenderFragment"/> with the provided
/// <paramref name="markup"/> as rendered output and passes it to the parameter specified in <paramref name="name"/>.
/// </summary>
/// <param name="name">Parameter name.</param>
/// <param name="markup">Markup to pass to the render fragment parameter</param>
/// <returns>The <see cref="ComponentParameter"/>.</returns>
public static ComponentParameter RenderFragment(string name, string markup)
{
return ComponentParameter.CreateParameter(name, markup.ToMarkupRenderFragment());
}
/// <summary>
/// Creates a <see cref="Microsoft.AspNetCore.Components.RenderFragment"/> which will render a <typeparamref name="TComponent"/> component
/// with the provided <paramref name="parameters"/> as input, and passes it to the parameter specified in <paramref name="name"/>.
/// </summary>
/// <typeparam name="TComponent">The type of the component to render with the <see cref="Microsoft.AspNetCore.Components.RenderFragment"/></typeparam>
/// <param name="name">Parameter name.</param>
/// <param name="parameters">Parameters to pass to the <typeparamref name="TComponent"/>.</param>
/// <returns>The <see cref="ComponentParameter"/>.</returns>
public static ComponentParameter RenderFragment<TComponent>(string name, params ComponentParameter[] parameters) where TComponent : class, IComponent
{
var cpc = new ComponentParameterCollection() { parameters };
return ComponentParameter.CreateParameter(name, cpc.ToRenderFragment<TComponent>());
}
/// <summary>
/// Creates a template component parameter which will pass the <paramref name="template"/> <see cref="Microsoft.AspNetCore.Components.RenderFragment{TValue}" />
/// to the parameter with the name <paramref name="name"/>.
/// </summary>
/// <typeparam name="TValue">The value used to build the content.</typeparam>
/// <param name="name">Parameter name.</param>
/// <param name="template"><see cref="Microsoft.AspNetCore.Components.RenderFragment{TValue}" /> to pass to the parameter.</param>
/// <returns>The <see cref="ComponentParameter"/>.</returns>
public static ComponentParameter Template<TValue>(string name, RenderFragment<TValue> template)
{
return ComponentParameter.CreateParameter(name, template);
}
/// <summary>
/// Creates a template component parameter which will pass the a <see cref="Microsoft.AspNetCore.Components.RenderFragment{TValue}" />
/// to the parameter with the name <paramref name="name"/>.
/// The <paramref name="markupFactory"/> will be used to generate the markup inside the template.
/// </summary>
/// <typeparam name="TValue">The value used to build the content.</typeparam>
/// <param name="name">Parameter name.</param>
/// <param name="markupFactory">A markup factory that takes a <typeparamref name="TValue"/> as input and returns markup/HTML.</param>
/// <returns>The <see cref="ComponentParameter"/>.</returns>
public static ComponentParameter Template<TValue>(string name, Func<TValue, string> markupFactory)
{
return Template<TValue>(name, value => (RenderTreeBuilder builder) => builder.AddMarkupContent(0, markupFactory(value)));
}
/// <summary>
/// Creates a template component parameter which will pass the a <see cref="Microsoft.AspNetCore.Components.RenderFragment{TValue}" />
/// to the <paramref name="parameterCollectionBuilder"/> at runtime. The parameters returned from it
/// will be passed to the <typeparamref name="TComponent"/> and it will be rendered as the template.
/// </summary>
/// <typeparam name="TComponent">The type of component to render in template.</typeparam>
/// <typeparam name="TValue">The value used to build the content.</typeparam>
/// <param name="name">Parameter name.</param>
/// <param name="parameterCollectionBuilder">The parameter collection builder function that will be passed the template <typeparamref name="TValue"/>.</param>
/// <returns>The <see cref="ComponentParameter"/>.</returns>
public static ComponentParameter Template<TComponent, TValue>(string name, Func<TValue, ComponentParameter[]> parameterCollectionBuilder)
where TComponent : IComponent
{
return Template<TValue>(name, value =>
{
var cpc = new ComponentParameterCollection() { parameterCollectionBuilder(value) };
return cpc.ToRenderFragment<TComponent>();
});
}
}
}