Skip to content

Commit 8e4db1c

Browse files
authored
feat(IpAddress): support paste function (#7276)
* feat(IpAddress): support paste function * refactor: 代码格式化
1 parent 19da5ef commit 8e4db1c

1 file changed

Lines changed: 22 additions & 2 deletions

File tree

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

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import Data from "../../modules/data.js"
1+
import Data from "../../modules/data.js"
22
import EventHandler from "../../modules/event-handler.js"
33

44
const selectCell = (el, index) => {
@@ -73,7 +73,7 @@ export function init(id) {
7373
e.preventDefault()
7474
selectCell(el, index - 1)
7575
}
76-
else if (e.key === 'Delete' || e.key === 'Tab' || e.key === 'ArrowLeft' || e.key === 'ArrowRight') {
76+
else if (e.composed || e.key === 'Delete' || e.key === 'Tab' || e.key === 'ArrowLeft' || e.key === 'ArrowRight') {
7777

7878
}
7979
else {
@@ -89,6 +89,26 @@ export function init(id) {
8989
}
9090
}
9191
})
92+
93+
EventHandler.on(c, 'paste', e => {
94+
e.preventDefault();
95+
const raw = (e.clipboardData || window.clipboardData)?.getData('text') ?? '';
96+
if (!raw) {
97+
return;
98+
}
99+
const parts = raw.replace(/[^\d.]/g, '').split('.').filter(p => p.length);
100+
const cells = el.querySelectorAll(".ipv4-cell");
101+
let pos = 0;
102+
parts.forEach(p => {
103+
if (pos > 3) {
104+
return;
105+
}
106+
const num = Math.max(0, Math.min(255, parseInt(p, 10) || 0));
107+
cells[pos].value = num.toString();
108+
ip.prevValues[pos] = cells[pos].value;
109+
pos++;
110+
});
111+
});
92112
})
93113
}
94114

0 commit comments

Comments
 (0)