Skip to content

Commit 35548f4

Browse files
committed
feat(IpAddress): add keydownEvent.code ['Space','ArrowLeft','ArrowRight','Backspace'] expansion operations
1 parent d97672f commit 35548f4

File tree

2 files changed

+23
-7
lines changed

2 files changed

+23
-7
lines changed

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,10 @@ protected override void OnParametersSet()
3636
var ipSegments = CurrentValueAsString.Split(".", System.StringSplitOptions.RemoveEmptyEntries);
3737
if (ipSegments.Length == 4)
3838
{
39-
Value1 = ipSegments[0];
40-
Value2 = ipSegments[1];
41-
Value3 = ipSegments[2];
42-
Value4 = ipSegments[3];
39+
Value1 = string.IsNullOrWhiteSpace(ipSegments[0]) ? "0" : ipSegments[0];
40+
Value2 = string.IsNullOrWhiteSpace(ipSegments[1]) ? "0" : ipSegments[1];
41+
Value3 = string.IsNullOrWhiteSpace(ipSegments[2]) ? "0" : ipSegments[2];
42+
Value4 = string.IsNullOrWhiteSpace(ipSegments[3]) ? "0" : ipSegments[3];
4343
}
4444
else
4545
{
@@ -110,6 +110,7 @@ private void ValueChanged4(ChangeEventArgs args)
110110

111111
private void UpdateValue()
112112
{
113-
CurrentValueAsString = $"{Value1}.{Value2}.{Value3}.{Value4}";
113+
// 如果遇到内容中间存在空格,去掉所有空格
114+
CurrentValueAsString = $"{Value1}.{Value2}.{Value3}.{Value4}".Replace(" ", "");
114115
}
115116
}

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

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ export function init(id) {
2424

2525
el.querySelectorAll(".ipv4-cell").forEach((c, index) => {
2626
EventHandler.on(c, 'keydown', e => {
27+
const current = selectCell(el, index)
2728
if ((e.keyCode >= 48 && e.keyCode <= 57) || (e.keyCode >= 96 && e.keyCode <= 105)) {
2829
// numbers, backup last status
2930
ip.prevValues[index] = c.value
@@ -51,11 +52,25 @@ export function init(id) {
5152
c.select()
5253
}
5354
else if (e.key === 'Backspace') {
54-
if (c.value.length === 0) {
55+
if (c.value.length <= 1) {
5556
c.value = "0"
56-
selectCell(el, index - 1)
57+
if (index === 0)
58+
e.preventDefault();
59+
const prevCell = selectCell(el, index - 1)
60+
prevCell.selectionStart = prevCell.value.length
61+
prevCell.selectionEnd = prevCell.value.length
5762
}
5863
}
64+
// 空格或右箭头(光标一定要在内容最后面)向后换一格
65+
else if (current.selectionStart === current.value.length && (e.code === 'Space' || e.code === 'ArrowRight')) {
66+
e.preventDefault()
67+
selectCell(el, index + 1)
68+
}
69+
// 左箭头(光标一定要在内容最前面)向前换一格
70+
else if (current.selectionStart === 0 && e.code === 'ArrowLeft') {
71+
e.preventDefault()
72+
selectCell(el, index - 1)
73+
}
5974
else if (e.key === 'Delete' || e.key === 'Tab' || e.key === 'ArrowLeft' || e.key === 'ArrowRight') {
6075

6176
}

0 commit comments

Comments
 (0)