Skip to content

Commit 3ee89ff

Browse files
committed
feat: 支持粘贴自动填充功能
1 parent dc2a561 commit 3ee89ff

File tree

1 file changed

+24
-2
lines changed

1 file changed

+24
-2
lines changed

src/BootstrapBlazor/Components/Input/OtpInput.razor.js

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
export function init(id) {
44
const el = document.getElementById(id);
5-
const input = el.querySelector('.bb-opt-input-val');
65
const skipKeys = ['Enter', 'Tab', 'Shift', 'Control', 'Alt'];
76
EventHandler.on(el, 'input', '.bb-opt-item', e => {
87
const isNumber = e.target.getAttribute('type') === 'number';
@@ -11,7 +10,7 @@ export function init(id) {
1110
e.target.value = e.target.value.slice(1, 2);
1211
}
1312
}
14-
input.value = [...el.querySelectorAll('.bb-opt-item')].map(input => input.value).join('');
13+
setValue(el);
1514
});
1615
EventHandler.on(el, 'keydown', '.bb-opt-item', e => {
1716
if (e.ctrlKey) {
@@ -48,6 +47,29 @@ export function init(id) {
4847
e.target.select();
4948
}
5049
});
50+
51+
EventHandler.on(el, 'paste', e => {
52+
if (e.clipboardData && e.clipboardData.getData) {
53+
const pastedText = e.clipboardData.getData('text/plain');
54+
const inputs = [...el.querySelectorAll('.bb-opt-item')];
55+
for (const index in inputs) {
56+
const input = inputs[index];
57+
if (index < pastedText.length) {
58+
input.value = pastedText[index];
59+
}
60+
else {
61+
input.value = '';
62+
}
63+
}
64+
}
65+
e.target.blur();
66+
setValue(el);
67+
});
68+
}
69+
70+
const setValue = el => {
71+
const input = el.querySelector('.bb-opt-input-val');
72+
input.value = [...el.querySelectorAll('.bb-opt-item')].map(input => input.value).join('');
5173
}
5274

5375
const setPrevFocus = (el, target) => {

0 commit comments

Comments
 (0)