Skip to content

Commit

Permalink
Component parameter assignment enhancements (#461)
Browse files Browse the repository at this point in the history
  • Loading branch information
guardrex authored Jan 27, 2025
1 parent ef5205f commit ba02586
Show file tree
Hide file tree
Showing 47 changed files with 163 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
Logger.LogInformation("HandleSubmit called: Form is valid");

// Process the valid form
// await ...
await Task.CompletedTask;
}
else
{
Expand Down
11 changes: 7 additions & 4 deletions 3.1/BlazorSample_Server/Pages/index/ParameterParent2.razor
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
@page "/parameter-parent-2"
@page "/parameter-parent-2"

<ParameterChild Title="title" />
<ParameterChild Title="@title" />

<ParameterChild Title="@GetTitle()" />

<ParameterChild Title="DateTime.Now.ToLongDateString()" />
<ParameterChild Title="@DateTime.Now.ToLongDateString()" />

<ParameterChild Title="panelData.Title" />
<ParameterChild Title="@panelData.Title" />

<ParameterChild Title="String literal title" Count="count" />

@code {
private string title = "From Parent field";
private PanelData panelData = new PanelData();
private int count = 12345;

private string GetTitle()
{
Expand Down
4 changes: 2 additions & 2 deletions 3.1/BlazorSample_Server/Shared/SurveyPrompt.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ public void OnError(Exception error)

public void OnNext(ElementReference value)
{
JS.InvokeAsync<object>(
"setElementClass", new object[] { value, "red" });
JS?.InvokeAsync<object>(
"setElementClass", [ value, "red" ]);

This comment has been minimized.

Copy link
@hakenr

hakenr Jan 28, 2025

Member

@guardrex The collection expressions syntax won't work in original net3.1, it is new in C# 12 (net8 and newer). The code currently compiles only due to .NET 9 SDK usage.

}

public void Dispose()
Expand Down
9 changes: 8 additions & 1 deletion 3.1/BlazorSample_Server/Shared/index/ParameterChild.razor
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
<div class="card w-25" style="margin-bottom:15px">
<div class="card-header font-weight-bold">@Title</div>
<div class="card-body" style="font-style:@Body.Style">
@Body.Text
<p>@Body.Text</p>
@if (Count is not null)
{
<p>The count is @Count.</p>
}
</div>
</div>

Expand All @@ -16,4 +20,7 @@
Text = "Set by child.",
Style = "normal"
};

[Parameter]
public int? Count { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
Logger.LogInformation("HandleSubmit called: Form is valid");

// Process the valid form
// await ...
await Task.CompletedTask;
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@

<ParameterChild Title="@panelData.Title" />

<ParameterChild Title="String literal title" Count="count" />

@code {
private string title = "From Parent field";
private PanelData panelData = new PanelData();
private int count = 12345;

private string GetTitle()
{
Expand Down
2 changes: 1 addition & 1 deletion 3.1/BlazorSample_WebAssembly/Shared/SurveyPrompt.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public void OnError(Exception error)

public void OnNext(ElementReference value)
{
JS.InvokeAsync<object>(
JS?.InvokeAsync<object>(
"setElementClass", new object[] { value, "red" });
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
<div class="card w-25" style="margin-bottom:15px">
<div class="card-header font-weight-bold">@Title</div>
<div class="card-body" style="font-style:@Body.Style">
@Body.Text
<p>@Body.Text</p>
@if (Count is not null)
{
<p>The count is @Count.</p>
}
</div>
</div>

Expand All @@ -16,4 +20,7 @@
Text = "Set by child.",
Style = "normal"
};

[Parameter]
public int? Count { get; set; }
}
2 changes: 1 addition & 1 deletion 5.0/BlazorSample_Server/LazyBrowserFileStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ internal sealed class LazyBrowserFileStream : Stream
{
private readonly IBrowserFile file;
private readonly int maxAllowedSize;
private Stream? underlyingStream;
private Stream underlyingStream;
private bool isDisposed;

public override bool CanRead => true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
{
await module.DisposeAsync();
}
catch (JSDisconnectedException)
catch

This comment has been minimized.

Copy link
@hakenr

hakenr Jan 28, 2025

Member

I think the JSDiconnectedException was intentional, and the documentation text refers to it?

This comment has been minimized.

Copy link
@guardrex

This comment has been minimized.

Copy link
@guardrex

guardrex Jan 28, 2025

Author Collaborator

I'll fix the collection (over)simplification. I'm going to see if I can run that 3.1 sample and watch it 💥 when I hit that component ... IF I can get it to run. Back in those days, these were merely here to supply code for articles. They weren't meant to be runnable samples. That came later.

This comment has been minimized.

Copy link
@guardrex

guardrex Jan 28, 2025

Author Collaborator

Actually, the collection situation is ok ...

Image

... but I see another bug that I need to fix in these early samples to get them running. I'd like to take the default SurveyPrompt component OUT of the Index component because unless it adopts the full approach of ...

https://learn.microsoft.com/en-us/aspnet/core/blazor/javascript-interoperability/call-javascript-from-dotnet?view=aspnetcore-9.0#reference-elements-across-components

... it breaks 💥 the app when it start up. By merely removing it from the Index component, the app runs and the example component for that scenario works.

WRT the collection simplification, it appear the compiler knows what to do, right? 🧠😆

This comment has been minimized.

Copy link
@guardrex

guardrex Jan 28, 2025

Author Collaborator

... and ... AND ... I don't have a red class in the stylesheet of these apps. The article section shows it, but the sample apps don't have the class. I'll add it on a new PR in a few minutes with the other updates.

I'm glad you look at all of my RexHacks! 🦖😆 Thanks for your on-going support, and I told Dan and Larry that you've been providing time and effort to help. You're a valuable MVP!!!!!!

This comment has been minimized.

Copy link
@hakenr

hakenr Jan 28, 2025

Member

I think collection expressions work in .NET Core 3.1 when you use a new C# 12 compiler (from a newer SDK). If you use the .NET Core 3.1 SDK with the older C# compiler from that time, it won't compile.

This comment has been minimized.

Copy link
@guardrex

guardrex Jan 28, 2025

Author Collaborator

I totally agree, but that's the question for Dan and Larry: Do they want to assume devs are using the latest SDK or not for these earlier .NET releases. It's hard to say ............. 🤔 .......... mmmmmmmmmmmmm .........

OK ... Let's do this. Let's leave the old coding approach in place for <8.0. Let's adopt your original pitch for this. I'll update the PR now. I'll still ask Dan and Larry about it to get their advice/direction in case this comes up in the future.

{
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
{
await mapInstance.DisposeAsync();
}
catch (JSDisconnectedException)
catch
{
}
}
Expand All @@ -49,7 +49,7 @@
{
await mapModule.DisposeAsync();
}
catch (JSDisconnectedException)
catch
{
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
Logger.LogInformation("HandleSubmit called: Form is valid");

// Process the valid form
// await ...
await Task.CompletedTask;
}
else
{
Expand Down
3 changes: 3 additions & 0 deletions 5.0/BlazorSample_Server/Pages/index/ParameterParent2.razor
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@

<ParameterChild Title="@panelData.Title" />

<ParameterChild Title="String literal title" Count="count" />

@code {
private string title = "From Parent field";
private PanelData panelData = new();
private int count = 12345;

private string GetTitle()
{
Expand Down
2 changes: 1 addition & 1 deletion 5.0/BlazorSample_Server/Shared/SurveyPrompt.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public void OnError(Exception error)

public void OnNext(ElementReference value)
{
JS.InvokeAsync<object>(
JS?.InvokeAsync<object>(
"setElementClass", new object[] { value, "red" });
}

Expand Down
9 changes: 8 additions & 1 deletion 5.0/BlazorSample_Server/Shared/index/ParameterChild.razor
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
<div class="card w-25" style="margin-bottom:15px">
<div class="card-header font-weight-bold">@Title</div>
<div class="card-body" style="font-style:@Body.Style">
@Body.Text
<p>@Body.Text</p>
@if (Count is not null)
{
<p>The count is @Count.</p>
}
</div>
</div>

Expand All @@ -16,4 +20,7 @@
Text = "Set by child.",
Style = "normal"
};

[Parameter]
public int? Count { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
Logger.LogInformation("HandleSubmit called: Form is valid");

// Process the valid form
// await ...
await Task.CompletedTask;
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@

<ParameterChild Title="@panelData.Title" />

<ParameterChild Title="String literal title" Count="count" />

@code {
private string title = "From Parent field";
private PanelData panelData = new();
private int count = 12345;

private string GetTitle()
{
Expand Down
2 changes: 1 addition & 1 deletion 5.0/BlazorSample_WebAssembly/Shared/SurveyPrompt.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public void OnError(Exception error)

public void OnNext(ElementReference value)
{
JS.InvokeAsync<object>(
JS?.InvokeAsync<object>(
"setElementClass", new object[] { value, "red" });
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
<div class="card w-25" style="margin-bottom:15px">
<div class="card-header font-weight-bold">@Title</div>
<div class="card-body" style="font-style:@Body.Style">
@Body.Text
<p>@Body.Text</p>
@if (Count is not null)
{
<p>The count is @Count.</p>
}
</div>
</div>

Expand All @@ -16,4 +20,7 @@
Text = "Set by child.",
Style = "normal"
};

[Parameter]
public int? Count { get; set; }
}
2 changes: 1 addition & 1 deletion 6.0/BlazorSample_Server/ExampleModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ public class ExampleModel
{
[Required]
[StringLength(10, ErrorMessage = "Name is too long.")]
public string Name { get; set; }
public string? Name { get; set; }
}
3 changes: 3 additions & 0 deletions 6.0/BlazorSample_Server/Pages/index/ParameterParent2.razor
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@

<ParameterChild Title="@panelData.Title" />

<ParameterChild Title="String literal title" Count="count" />

@code {
private string title = "From Parent field";
private PanelData panelData = new();
private int count = 12345;

private string GetTitle()
{
Expand Down
2 changes: 1 addition & 1 deletion 6.0/BlazorSample_Server/Shared/SurveyPrompt.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public void OnError(Exception error)

public void OnNext(ElementReference value)
{
JS.InvokeAsync<object>(
JS?.InvokeAsync<object>(
"setElementClass", new object[] { value, "red" });
}

Expand Down
9 changes: 8 additions & 1 deletion 6.0/BlazorSample_Server/Shared/index/ParameterChild.razor
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
<div class="card w-25" style="margin-bottom:15px">
<div class="card-header font-weight-bold">@Title</div>
<div class="card-body" style="font-style:@Body.Style">
@Body.Text
<p>@Body.Text</p>
@if (Count is not null)
{
<p>The count is @Count.</p>
}
</div>
</div>

Expand All @@ -16,4 +20,7 @@
Text = "Set by child.",
Style = "normal"
};

[Parameter]
public int? Count { get; set; }
}
2 changes: 1 addition & 1 deletion 6.0/BlazorSample_WebAssembly/ExampleModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ public class ExampleModel
{
[Required]
[StringLength(10, ErrorMessage = "Name is too long.")]
public string Name { get; set; }
public string? Name { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@

<ParameterChild Title="@panelData.Title" />

<ParameterChild Title="String literal title" Count="count" />

@code {
private string title = "From Parent field";
private PanelData panelData = new();
private int count = 12345;

private string GetTitle()
{
Expand Down
2 changes: 1 addition & 1 deletion 6.0/BlazorSample_WebAssembly/Shared/SurveyPrompt.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public void OnError(Exception error)

public void OnNext(ElementReference value)
{
JS.InvokeAsync<object>(
JS?.InvokeAsync<object>(
"setElementClass", new object[] { value, "red" });
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
<div class="card w-25" style="margin-bottom:15px">
<div class="card-header font-weight-bold">@Title</div>
<div class="card-body" style="font-style:@Body.Style">
@Body.Text
<p>@Body.Text</p>
@if (Count is not null)
{
<p>The count is @Count.</p>
}
</div>
</div>

Expand All @@ -16,4 +20,7 @@
Text = "Set by child.",
Style = "normal"
};

[Parameter]
public int? Count { get; set; }
}
2 changes: 1 addition & 1 deletion 7.0/BlazorSample_Server/ExampleModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ public class ExampleModel
{
[Required]
[StringLength(10, ErrorMessage = "Name is too long.")]
public string Name { get; set; }
public string? Name { get; set; }
}
3 changes: 3 additions & 0 deletions 7.0/BlazorSample_Server/Pages/index/ParameterParent2.razor
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@

<ParameterChild Title="@panelData.Title" />

<ParameterChild Title="String literal title" Count="count" />

@code {
private string title = "From Parent field";
private PanelData panelData = new();
private int count = 12345;

private string GetTitle()
{
Expand Down
2 changes: 1 addition & 1 deletion 7.0/BlazorSample_Server/Shared/SurveyPrompt.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public void OnError(Exception error)

public void OnNext(ElementReference value)
{
JS.InvokeAsync<object>(
JS?.InvokeAsync<object>(
"setElementClass", new object[] { value, "red" });
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
public string? ChildMessage { get; set; }

[Parameter]
public EventCallback<string> ChildMessageChanged { get; set; }
public EventCallback<string?> ChildMessageChanged { get; set; }

private async Task ChangeValue()
{
Expand Down
Loading

0 comments on commit ba02586

Please sign in to comment.