Skip to content

Commit cbfec64

Browse files
authored
Spright blazor chat input (#2664)
# Pull Request ## 🤨 Rationale - #2610 (Blazor wrapper and example app)
1 parent 4582e33 commit cbfec64

9 files changed

Lines changed: 155 additions & 11 deletions

File tree

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"type": "minor",
3+
"comment": "Add SprightChatInput to Blazor.",
4+
"packageName": "@ni/spright-blazor",
5+
"email": "26874831+atmgrifter00@users.noreply.github.com",
6+
"dependentChangeType": "patch"
7+
}

packages/blazor-workspace/Examples/Demo.Shared/Pages/ComponentsDemo.razor

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -464,6 +464,13 @@
464464
Check core temperature
465465
</NimbleButton>
466466
</SprightChatMessage>
467+
@foreach(var message in _userMessages)
468+
{
469+
<SprightChatMessage MessageType="ChatMessageType.Outbound">
470+
@message
471+
</SprightChatMessage>
472+
}
473+
<SprightChatInput slot="input" Placeholder="Type here" Send="UpdateUserMessages"></SprightChatInput>
467474
</SprightChatConversation>
468475
</div>
469476
</div>

packages/blazor-workspace/Examples/Demo.Shared/Pages/ComponentsDemo.razor.cs

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using Apache.Arrow;
44
using Apache.Arrow.Types;
55
using NimbleBlazor;
6+
using SprightBlazor;
67

78
namespace Demo.Shared.Pages;
89

@@ -31,16 +32,17 @@ public partial class ComponentsDemo
3132
};
3233
private readonly HashSet<string> _recordsLoadingChildren = new();
3334
private readonly HashSet<string> _recordsWithLoadedChildren = new();
34-
35-
public const string MarkdownString = "Supported rich text formatting options:\n" +
36-
"1. **Bold**\n" +
37-
"2. *Italics*\n" +
38-
"3. Numbered lists\n" +
39-
" 1. Option 1\n" +
40-
" 2. Option 2\n" +
41-
"4. Bulleted lists\n" +
42-
" * Option 1\n" +
43-
" * Option 2\n" +
35+
private List<string> _userMessages = new List<string>();
36+
37+
public const string MarkdownString = "Supported rich text formatting options:\n" +
38+
"1. **Bold**\n" +
39+
"2. *Italics*\n" +
40+
"3. Numbered lists\n" +
41+
" 1. Option 1\n" +
42+
" 2. Option 2\n" +
43+
"4. Bulleted lists\n" +
44+
" * Option 1\n" +
45+
" * Option 2\n" +
4446
"5. Absolute link: <https://nimble.ni.dev/>\n";
4547

4648
public IEnumerable<DemoColor> PossibleColors
@@ -313,6 +315,11 @@ public void RemoveDiesFromRadius(int numberOfDies)
313315
{
314316
UpdateDiesTable(DiesTable.Length - (int)(numberOfDies * numberOfDies * Math.PI));
315317
}
318+
319+
public void UpdateUserMessages(ChatInputSendEventArgs e)
320+
{
321+
_userMessages.Add(e.Text);
322+
}
316323
}
317324

318325
public enum DemoColor
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
@namespace SprightBlazor
2+
<spright-chat-input
3+
@attributes="AdditionalAttributes"
4+
placeholder="@Placeholder"
5+
value="@Value"
6+
@onsprightchatinputsend="(__value) => HandleChatInputSend(__value)">
7+
@ChildContent
8+
</spright-chat-input>
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using Microsoft.AspNetCore.Components;
2+
3+
namespace SprightBlazor;
4+
5+
public partial class SprightChatInput : ComponentBase
6+
{
7+
[Parameter]
8+
public string? Placeholder { get; set; }
9+
10+
[Parameter]
11+
public string? Value { get; set; }
12+
13+
/// <summary>
14+
/// Gets or sets a callback that is invoked when the send event is raised.
15+
/// </summary>
16+
[Parameter]
17+
public EventCallback<ChatInputSendEventArgs> Send { get; set; }
18+
19+
/// <summary>
20+
/// The child content of the element.
21+
/// </summary>
22+
[Parameter]
23+
public RenderFragment? ChildContent { get; set; }
24+
25+
/// <summary>
26+
/// Any additional attributes that did not match known properties.
27+
/// </summary>
28+
[Parameter(CaptureUnmatchedValues = true)]
29+
public IDictionary<string, object>? AdditionalAttributes { get; set; }
30+
31+
private async void HandleChatInputSend(ChatInputSendEventArgs eventArgs)
32+
{
33+
await Send.InvokeAsync(eventArgs);
34+
}
35+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using Microsoft.AspNetCore.Components;
2+
3+
namespace SprightBlazor;
4+
5+
public class ChatInputSendEventArgs : EventArgs
6+
{
7+
public string Text { get; set; } = string.Empty;
8+
}
9+
10+
[EventHandler("onsprightchatinputsend", typeof(ChatInputSendEventArgs), enableStopPropagation: true, enablePreventDefault: false)]
11+
public static class EventHandlers
12+
{
13+
}

packages/blazor-workspace/SprightBlazor/wwwroot/SprightBlazor.lib.module.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,15 @@ function registerEvents(Blazor) {
3434
}
3535
});
3636
*/
37+
38+
Blazor.registerCustomEventType('sprightchatinputsend', {
39+
browserEventName: 'send',
40+
createEventArgs: event => {
41+
return {
42+
text: event.target.value
43+
};
44+
}
45+
});
3746
}
3847

3948
function handleRuntimeStarted() {
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
using System;
2+
using System.Linq.Expressions;
3+
using Bunit;
4+
using Xunit;
5+
6+
namespace SprightBlazor.Tests.Unit.Components;
7+
8+
/// <summary>
9+
/// Test for <see cref="SprightChaInputTests"/>.
10+
/// </summary>
11+
public class SprightChatInputTests
12+
{
13+
[Fact]
14+
public void SprightChatInput_Render_HasChatInputMarkup()
15+
{
16+
var context = new TestContext();
17+
context.JSInterop.Mode = JSRuntimeMode.Loose;
18+
var expectedMarkup = "spright-chat-input";
19+
20+
var component = context.RenderComponent<SprightChatInput>();
21+
22+
Assert.Contains(expectedMarkup, component.Markup);
23+
}
24+
25+
[Fact]
26+
public void SprightChatInput_SupportsAdditionalAttributes()
27+
{
28+
var context = new TestContext();
29+
context.JSInterop.Mode = JSRuntimeMode.Loose;
30+
var exception = Record.Exception(() => context.RenderComponent<SprightChatInput>(ComponentParameter.CreateParameter("class", "foo")));
31+
Assert.Null(exception);
32+
}
33+
34+
[Fact]
35+
public void SprightChatInputValue_AttributeIsSet()
36+
{
37+
var value = "Tell me something.";
38+
var chatInput = RenderWithPropertySet(x => x.Value, value);
39+
40+
Assert.Contains("value", chatInput.Markup);
41+
}
42+
43+
[Fact]
44+
public void SprightChatInputPlaceholder_AttributeIsSet()
45+
{
46+
var placeholder = "Type here...";
47+
var chatInput = RenderWithPropertySet(x => x.Placeholder, placeholder);
48+
49+
Assert.Contains("placeholder", chatInput.Markup);
50+
}
51+
52+
private IRenderedComponent<SprightChatInput> RenderWithPropertySet<TProperty>(Expression<Func<SprightChatInput, TProperty>> propertyGetter, TProperty propertyValue)
53+
{
54+
var context = new TestContext();
55+
context.JSInterop.Mode = JSRuntimeMode.Loose;
56+
return context.RenderComponent<SprightChatInput>(p => p.Add(propertyGetter, propertyValue));
57+
}
58+
}

packages/blazor-workspace/Tests/SprightBlazor.Tests/Unit/Components/SprightChatMessageTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ namespace SprightBlazor.Tests.Unit.Components;
1111
public class SprightChatMessageTests
1212
{
1313
[Fact]
14-
public void SprightChatMessage_Render_HasChatConversationMarkup()
14+
public void SprightChatMessage_Render_HasChatMessageMarkup()
1515
{
1616
var context = new TestContext();
1717
context.JSInterop.Mode = JSRuntimeMode.Loose;

0 commit comments

Comments
 (0)