Skip to content

Commit 8782291

Browse files
authored
feat(IpAddress): trigger ValueChanged on paste event (#7280)
* refactor: 增加 JSInvoke 能力 * refactor: 更改样式 * refactor: 增加客户端更改 IP 回调方法 * test: 更新单元测试 * chore: bump version 10.1.3
1 parent 8ef6fe8 commit 8782291

6 files changed

Lines changed: 39 additions & 11 deletions

File tree

src/BootstrapBlazor/BootstrapBlazor.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk.Razor">
22

33
<PropertyGroup>
4-
<Version>10.1.2</Version>
4+
<Version>10.1.3</Version>
55
</PropertyGroup>
66

77
<ItemGroup>

src/BootstrapBlazor/Components/IpAddress/IpAddress.razor

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
@namespace BootstrapBlazor.Components
1+
@namespace BootstrapBlazor.Components
22
@inherits ValidateBase<string>
3-
@attribute [BootstrapModuleAutoLoader]
3+
@attribute [BootstrapModuleAutoLoader(JSObjectReference = true)]
44

55
@if (IsShowLabel)
66
{

src/BootstrapBlazor/Components/IpAddress/IpAddress.razor.cs

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Licensed to the .NET Foundation under one or more agreements.
1+
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the Apache 2.0 License
33
// See the LICENSE file in the project root for more information.
44
// Maintainer: Argo Zhang(argo@live.ca) Website: https://www.blazor.zone
@@ -21,7 +21,7 @@ public partial class IpAddress
2121
/// <summary>
2222
/// 获得 class 样式集合
2323
/// </summary>
24-
protected string? ClassName => CssBuilder.Default("ipaddress form-control")
24+
protected string? ClassName => CssBuilder.Default("bb-ip form-control")
2525
.AddClass("disabled", IsDisabled)
2626
.AddClass(CssClass).AddClass(ValidCss)
2727
.Build();
@@ -62,7 +62,6 @@ private void ValueChanged1(ChangeEventArgs args)
6262
{
6363
Value1 = Value1[0..3];
6464
}
65-
6665
UpdateValue();
6766
}
6867

@@ -112,4 +111,22 @@ private void UpdateValue()
112111
{
113112
CurrentValueAsString = $"{Value1}.{Value2}.{Value3}.{Value4}";
114113
}
114+
115+
/// <summary>
116+
/// 更新 值方法供 JS 调用
117+
/// </summary>
118+
/// <param name="v1"></param>
119+
/// <param name="v2"></param>
120+
/// <param name="v3"></param>
121+
/// <param name="v4"></param>
122+
[JSInvokable]
123+
public void TriggerUpdate(int v1, int v2, int v3, int v4)
124+
{
125+
Value1 = v1.ToString();
126+
Value2 = v2.ToString();
127+
Value3 = v3.ToString();
128+
Value4 = v4.ToString();
129+
130+
UpdateValue();
131+
}
115132
}

src/BootstrapBlazor/Components/IpAddress/IpAddress.razor.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const selectCell = (el, index) => {
1212
cell.focus();
1313
}
1414

15-
export function init(id) {
15+
export function init(id, invoke) {
1616
const el = document.getElementById(id)
1717
if (el === null) {
1818
return
@@ -33,7 +33,6 @@ export function init(id) {
3333
EventHandler.on(c, 'keydown', e => {
3434
const current = e.target;
3535
if ((e.keyCode >= 48 && e.keyCode <= 57) || (e.keyCode >= 96 && e.keyCode <= 105)) {
36-
// numbers, backup last status
3736
ip.prevValues[index] = c.value
3837
if (c.value === "0") {
3938
c.value = ""
@@ -99,15 +98,19 @@ export function init(id) {
9998
const parts = raw.replace(/[^\d.]/g, '').split('.').filter(p => p.length);
10099
const cells = el.querySelectorAll(".ipv4-cell");
101100
let pos = 0;
101+
const args = [];
102102
parts.forEach(p => {
103103
if (pos > 3) {
104104
return;
105105
}
106106
const num = Math.max(0, Math.min(255, parseInt(p, 10) || 0));
107+
args.push(num);
107108
cells[pos].value = num.toString();
108109
ip.prevValues[pos] = cells[pos].value;
109110
pos++;
110111
});
112+
113+
invoke.invokeMethodAsync("TriggerUpdate", ...args);
111114
});
112115
})
113116
}

src/BootstrapBlazor/Components/IpAddress/IpAddress.razor.scss

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
@use "../../wwwroot/scss/variables" as *;
1+
@use "../../wwwroot/scss/variables" as *;
22

3-
.ipaddress {
3+
.bb-ip {
44
--bb-ip-cell-max-width: #{$bb-ip-cell-max-width};
55
display: flex;
66
flex-wrap: nowrap;

test/UnitTest/Components/IpAddressTest.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public class IpAddressTest : BootstrapBlazorTestBase
1111
public async Task IpAddress_Ok()
1212
{
1313
var cut = Context.Render<IpAddress>();
14-
cut.Contains("ipaddress form-control");
14+
cut.Contains("bb-ip form-control");
1515
Assert.Equal("0.0.0.0", cut.Instance.Value);
1616

1717
var inputs = cut.FindAll(".ipv4-cell");
@@ -46,6 +46,14 @@ public async Task IpAddress_Value()
4646
Assert.Equal("123.123.123.123", cut.Instance.Value);
4747
}
4848

49+
[Fact]
50+
public async Task TriggerUpdate_Ok()
51+
{
52+
var cut = Context.Render<IpAddress>();
53+
await cut.InvokeAsync(() => cut.Instance.TriggerUpdate(192, 0, 1, 10));
54+
Assert.Equal("192.0.1.10", cut.Instance.Value);
55+
}
56+
4957
[Fact]
5058
public void ValidateForm_Ok()
5159
{

0 commit comments

Comments
 (0)