Skip to content

Commit e6c8012

Browse files
momijijinArgoZhang
andauthored
feat(IpAddress): support ArrowLeft/Right key (#6244)
* feat(IpAddress): add keydownEvent.code ['Space','ArrowLeft','ArrowRight','Backspace'] expansion operations * fix(IpAddress): Fix the issue where pressing 'Backspace' would delete an extra character when switching to the previous cell. * fix(IpAddress): Re-write the validation for cells that have content consisting solely of blank spaces. * refactor: 撤销更改 * refactor: 清除空格 * refactor: 删除中文注释 * refactor: 更新选中部分数字逻辑 * feat: 增加获得焦点后全选逻辑 --------- Co-Authored-By: jin momiji <[email protected]> Co-authored-by: Argo Zhang <[email protected]>
1 parent 863a208 commit e6c8012

File tree

2 files changed

+29
-12
lines changed

2 files changed

+29
-12
lines changed

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

Lines changed: 4 additions & 4 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 = ipSegments[0].Trim();
40+
Value2 = ipSegments[1].Trim();
41+
Value3 = ipSegments[2].Trim();
42+
Value4 = ipSegments[3].Trim();
4343
}
4444
else
4545
{

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

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,8 @@ const selectCell = (el, index) => {
88
if (index > 3) {
99
index = 3
1010
}
11-
const c = el.querySelectorAll(".ipv4-cell")[index]
12-
c.focus()
13-
return c
11+
const cell = el.querySelectorAll(".ipv4-cell")[index];
12+
cell.focus();
1413
}
1514

1615
export function init(id) {
@@ -23,16 +22,26 @@ export function init(id) {
2322
Data.set(id, ip)
2423

2524
el.querySelectorAll(".ipv4-cell").forEach((c, index) => {
25+
EventHandler.on(c, 'compositionend', e => {
26+
e.preventDefault();
27+
c.value = ip.prevValues[index];
28+
});
29+
EventHandler.on(c, 'focus', e => {
30+
const input = e.target;
31+
input.select();
32+
});
2633
EventHandler.on(c, 'keydown', e => {
34+
const current = e.target;
2735
if ((e.keyCode >= 48 && e.keyCode <= 57) || (e.keyCode >= 96 && e.keyCode <= 105)) {
2836
// numbers, backup last status
2937
ip.prevValues[index] = c.value
3038
if (c.value === "0") {
3139
c.value = ""
3240
}
3341
else if (c.selectionStart !== c.selectionEnd) {
34-
const v = c.value.substring(c.selectionStart, c.selectionEnd)
35-
const newVal = c.value.replace(v, e.key)
42+
const v1 = c.value.substring(0, c.selectionStart);
43+
const v2 = c.value.substring(c.selectionEnd, c.value.length);
44+
const newVal = `${v1}${e.key}${v2}`;
3645
const num = Number(newVal)
3746
if (num > 255) {
3847
e.preventDefault()
@@ -47,15 +56,23 @@ export function init(id) {
4756
}
4857
else if (e.key === '.') {
4958
e.preventDefault()
50-
const c = selectCell(el, index + 1)
51-
c.select()
59+
selectCell(el, index + 1)
5260
}
5361
else if (e.key === 'Backspace') {
54-
if (c.value.length === 0) {
62+
if (c.value.length <= 1) {
5563
c.value = "0"
64+
e.preventDefault();
5665
selectCell(el, index - 1)
5766
}
5867
}
68+
else if (current.selectionStart === current.value.length && (e.code === 'Space' || e.code === 'ArrowRight')) {
69+
e.preventDefault()
70+
selectCell(el, index + 1)
71+
}
72+
else if (current.selectionStart === 0 && e.code === 'ArrowLeft') {
73+
e.preventDefault()
74+
selectCell(el, index - 1)
75+
}
5976
else if (e.key === 'Delete' || e.key === 'Tab' || e.key === 'ArrowLeft' || e.key === 'ArrowRight') {
6077

6178
}

0 commit comments

Comments
 (0)