You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/08-templated-components.md
+18-22Lines changed: 18 additions & 22 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -205,7 +205,7 @@ Start by creating a new file `TemplatedList.razor` in the `BlazingComponents` pr
205
205
1. Async-loading of any type of data
206
206
2. Separate rendering logic for three states - loading, empty list, and showing items
207
207
208
-
We can solve async loading by accepting a delegate of type `Func<Task<List<?>>>` - we need to figure out what type should replace **?**. Since we want to support any kind of data, we need to declare this component as a generic type. We can make a generic-typed component using the `@typeparam` directive, so place this at the top of `TemplatedList.razor`.
208
+
We can solve async loading by accepting a delegate of type `Func<Task<IEnumerable<?>>>` - we need to figure out what type should replace **?**. Since we want to support any kind of data, we need to declare this component as a generic type. We can make a generic-typed component using the `@typeparam` directive, so place this at the top of `TemplatedList.razor`.
209
209
210
210
```html
211
211
@typeparam TItem
@@ -219,9 +219,9 @@ Now that we've defined a generic type parameter we can use it in a parameter dec
219
219
220
220
```html
221
221
@code {
222
-
List<TItem> items;
222
+
IEnumerable<TItem> items;
223
223
224
-
[Parameter] public Func<Task<List<TItem>>> Loader { get; set; }
224
+
[Parameter] public Func<Task<IEnumerable<TItem>>> Loader { get; set; }
@@ -237,7 +237,7 @@ Since we have the data, we can now add the structure of each of the states we ne
237
237
{
238
238
239
239
}
240
-
else if (items.Count == 0)
240
+
else if (!items.Any())
241
241
{
242
242
}
243
243
else
@@ -270,7 +270,7 @@ Now that we have some `RenderFragment` parameters, we can start using them. Upda
270
270
{
271
271
@Loading
272
272
}
273
-
else if (items.Count == 0)
273
+
else if (!items.Any())
274
274
{
275
275
@Empty
276
276
}
@@ -295,9 +295,9 @@ Let's add another `string` parameter, and finally the functions block of `Templa
295
295
296
296
```html
297
297
@code {
298
-
List<TItem> items;
298
+
IEnumerable<TItem> items;
299
299
300
-
[Parameter] public Func<Task<List<TItem>>> Loader { get; set; }
300
+
[Parameter] public Func<Task<IEnumerable<TItem>>> Loader { get; set; }
301
301
[Parameter] public RenderFragment Loading { get; set; }
302
302
[Parameter] public RenderFragment Empty { get; set; }
303
303
[Parameter] public RenderFragment<TItem> Item { get; set; }
@@ -319,7 +319,7 @@ Lastly update the `<div class="list-group">` to contain `<div class="list-group
319
319
{
320
320
@Loading
321
321
}
322
-
else if (items.Count == 0)
322
+
else if (!items.Any())
323
323
{
324
324
@Empty
325
325
}
@@ -336,9 +336,9 @@ else
336
336
}
337
337
338
338
@code {
339
-
List<TItem> items;
339
+
IEnumerable<TItem> items;
340
340
341
-
[Parameter] public Func<Task<List<TItem>>> Loader { get; set; }
341
+
[Parameter] public Func<Task<IEnumerable<TItem>>> Loader { get; set; }
342
342
[Parameter] public RenderFragment Loading { get; set; }
343
343
[Parameter] public RenderFragment Empty { get; set; }
344
344
[Parameter] public RenderFragment<TItem> Item { get; set; }
@@ -355,31 +355,27 @@ else
355
355
356
356
To use the new `TemplatedList` component, we're going to edit `MyOrders.razor`.
357
357
358
-
First, we need to create a delegate that we can pass to the `TemplatedList` that will load order data. We can do this by keeping the line of code that's in `MyOrders.OnParametersSetAsync` and changing the method signature. The `@code` block should look something like:
358
+
First, we need to create a delegate that we can pass to the `TemplatedList` that will load order data. We can do this by keeping the code that's in `MyOrders.OnParametersSetAsync` and changing the method signature. The `@code` block should look something like:
This matches the signature expected by the `Loader` parameter of `TemplatedList`, it's a `Func<Task<List<?>>>` where the **?** is replaced with `OrderWithStatus` so we are on the right track.
378
+
This matches the signature expected by the `Loader` parameter of `TemplatedList`, it's a `Func<Task<IEnumerable<?>>>` where the **?** is replaced with `OrderWithStatus` so we are on the right track.
383
379
384
380
You can use the `TemplatedList` component now like so:
0 commit comments