-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added Blazor wrappers for chat components
- Loading branch information
Jose A. Hernandez
committed
Feb 14, 2025
1 parent
a188625
commit 1c39f7e
Showing
7 changed files
with
150 additions
and
0 deletions.
There are no files selected for viewing
15 changes: 15 additions & 0 deletions
15
packages/blazor-workspace/SprightBlazor/ChatMessageType.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
namespace SprightBlazor; | ||
|
||
public enum ChatMessageType | ||
{ | ||
System, | ||
Outbound, | ||
Inbound | ||
} | ||
|
||
internal static class ChatMessageTypeExtensions | ||
{ | ||
private static readonly Dictionary<ChatMessageType, string> _enumValues = AttributeHelpers.GetEnumNamesAsKebabCaseValues<ChatMessageType>(); | ||
|
||
public static string? ToAttributeValue(this ChatMessageType? value) => value == null ? null : _enumValues[value.Value]; | ||
} |
5 changes: 5 additions & 0 deletions
5
packages/blazor-workspace/SprightBlazor/Components/SprightChatConversation.razor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
@namespace SprightBlazor | ||
<spright-chat-conversation | ||
@attributes="AdditionalAttributes"> | ||
@ChildContent | ||
</spright-chat-conversation> |
18 changes: 18 additions & 0 deletions
18
packages/blazor-workspace/SprightBlazor/Components/SprightChatConversation.razor.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
using Microsoft.AspNetCore.Components; | ||
|
||
namespace SprightBlazor; | ||
|
||
public partial class SprightChatConversation : ComponentBase | ||
{ | ||
/// <summary> | ||
/// The child content of the element. | ||
/// </summary> | ||
[Parameter] | ||
public RenderFragment? ChildContent { get; set; } | ||
|
||
/// <summary> | ||
/// Any additional attributes that did not match known properties. | ||
/// </summary> | ||
[Parameter(CaptureUnmatchedValues = true)] | ||
public IDictionary<string, object>? AdditionalAttributes { get; set; } | ||
} |
6 changes: 6 additions & 0 deletions
6
packages/blazor-workspace/SprightBlazor/Components/SprightChatMessage.razor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
@namespace SprightBlazor | ||
<spright-chat-message | ||
message-type="@MessageType.ToAttributeValue()" | ||
@attributes="AdditionalAttributes"> | ||
@ChildContent | ||
</spright-chat-message> |
24 changes: 24 additions & 0 deletions
24
packages/blazor-workspace/SprightBlazor/Components/SprightChatMessage.razor.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
using Microsoft.AspNetCore.Components; | ||
|
||
namespace SprightBlazor; | ||
|
||
public partial class SprightChatMessage : ComponentBase | ||
{ | ||
/// <summary> | ||
/// The message type of the message | ||
/// </summary> | ||
[Parameter] | ||
public ChatMessageType? MessageType { get; set; } | ||
|
||
/// <summary> | ||
/// The child content of the element. | ||
/// </summary> | ||
[Parameter] | ||
public RenderFragment? ChildContent { get; set; } | ||
|
||
/// <summary> | ||
/// Any additional attributes that did not match known properties. | ||
/// </summary> | ||
[Parameter(CaptureUnmatchedValues = true)] | ||
public IDictionary<string, object>? AdditionalAttributes { get; set; } | ||
} |
31 changes: 31 additions & 0 deletions
31
...lazor-workspace/Tests/SprightBlazor.Tests/Unit/Components/SprightChatConversationTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
using Bunit; | ||
using Xunit; | ||
|
||
namespace SprightBlazor.Tests.Unit.Components; | ||
|
||
/// <summary> | ||
/// Test for <see cref="SprightChatConversation"/>. | ||
/// </summary> | ||
public class SprightChatConversationTests | ||
{ | ||
[Fact] | ||
public void SprightChatConversation_Render_HasChatConversationMarkup() | ||
{ | ||
var context = new TestContext(); | ||
context.JSInterop.Mode = JSRuntimeMode.Loose; | ||
var expectedMarkup = "spright-chat-conversation"; | ||
|
||
var component = context.RenderComponent<SprightChatConversation>(); | ||
|
||
Assert.Contains(expectedMarkup, component.Markup); | ||
} | ||
|
||
[Fact] | ||
public void SprightChatConversation_SupportsAdditionalAttributes() | ||
{ | ||
var context = new TestContext(); | ||
context.JSInterop.Mode = JSRuntimeMode.Loose; | ||
var exception = Record.Exception(() => context.RenderComponent<SprightChatConversation>(ComponentParameter.CreateParameter("class", "foo"))); | ||
Assert.Null(exception); | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
...ges/blazor-workspace/Tests/SprightBlazor.Tests/Unit/Components/SprightChatMessageTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
using System; | ||
using System.Linq.Expressions; | ||
using Bunit; | ||
using Xunit; | ||
|
||
namespace SprightBlazor.Tests.Unit.Components; | ||
|
||
/// <summary> | ||
/// Test for <see cref="SprightChatMessage"/>. | ||
/// </summary> | ||
public class SprightChatMessageTests | ||
{ | ||
[Fact] | ||
public void SprightChatMessage_Render_HasChatConversationMarkup() | ||
{ | ||
var context = new TestContext(); | ||
context.JSInterop.Mode = JSRuntimeMode.Loose; | ||
var expectedMarkup = "spright-chat-message"; | ||
|
||
var component = context.RenderComponent<SprightChatMessage>(); | ||
|
||
Assert.Contains(expectedMarkup, component.Markup); | ||
} | ||
|
||
[Fact] | ||
public void SprightChatMessage_SupportsAdditionalAttributes() | ||
{ | ||
var context = new TestContext(); | ||
context.JSInterop.Mode = JSRuntimeMode.Loose; | ||
var exception = Record.Exception(() => context.RenderComponent<SprightChatMessage>(ComponentParameter.CreateParameter("class", "foo"))); | ||
Assert.Null(exception); | ||
} | ||
|
||
[Theory] | ||
[InlineData(ChatMessageType.Outbound, "message-type=\"outbound\"")] | ||
[InlineData(ChatMessageType.Inbound, "message-type=\"inbound\"")] | ||
[InlineData(ChatMessageType.System, "message-type=\"system\"")] | ||
public void SprightChatMessageTypeVariant_AttributeIsSet(ChatMessageType value, string expectedAttribute) | ||
{ | ||
var button = RenderWithPropertySet(x => x.MessageType, value); | ||
|
||
Assert.Contains(expectedAttribute, button.Markup); | ||
} | ||
|
||
private IRenderedComponent<SprightChatMessage> RenderWithPropertySet<TProperty>(Expression<Func<SprightChatMessage, TProperty>> propertyGetter, TProperty propertyValue) | ||
{ | ||
var context = new TestContext(); | ||
context.JSInterop.Mode = JSRuntimeMode.Loose; | ||
return context.RenderComponent<SprightChatMessage>(p => p.Add(propertyGetter, propertyValue)); | ||
} | ||
} |