Skip to content

Commit bb01d76

Browse files
committed
feat: 增加数据双向绑定支持
1 parent 3ee89ff commit bb01d76

3 files changed

Lines changed: 27 additions & 8 deletions

File tree

src/BootstrapBlazor/Components/Input/OtpInput.razor

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
@inherits ValidateBase<string>
33

44
<div @attributes="AdditionalAttributes" id="@Id" class="@ClassString">
5-
<input hidden value="@CurrentValueAsString" class="bb-opt-input-val" />
65
@for (var index = 0; index < Digits; index++)
76
{
87
@if(IsReadonly || IsDisabled)

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

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ namespace BootstrapBlazor.Components;
88
/// <summary>
99
/// OTP input component
1010
/// </summary>
11-
[BootstrapModuleAutoLoader("Input/OtpInput.razor.js")]
11+
[BootstrapModuleAutoLoader("Input/OtpInput.razor.js", JSObjectReference = true)]
1212
public partial class OtpInput
1313
{
1414
/// <summary>
@@ -87,8 +87,27 @@ protected override void OnParametersSet()
8787
}
8888
}
8989

90+
/// <summary>
91+
/// <inheritdoc/>
92+
/// </summary>
93+
/// <returns></returns>
94+
protected override Task InvokeInitAsync() => InvokeVoidAsync("init", Id, Interop, nameof(TriggerSetValue));
95+
9096
private char? GetValueString(int index)
9197
{
9298
return _values[index] != 0 ? _values[index] : null;
9399
}
100+
101+
102+
/// <summary>
103+
/// Trigger value changed event callback. Trigger by JavaScript.
104+
/// </summary>
105+
/// <param name="val"></param>
106+
/// <returns></returns>
107+
[JSInvokable]
108+
public Task TriggerSetValue(string val)
109+
{
110+
CurrentValueAsString = val;
111+
return Task.CompletedTask;
112+
}
94113
}

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

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import EventHandler from '../../modules/event-handler.js'
22

3-
export function init(id) {
3+
export function init(id, invoke, method) {
44
const el = document.getElementById(id);
55
const skipKeys = ['Enter', 'Tab', 'Shift', 'Control', 'Alt'];
66
EventHandler.on(el, 'input', '.bb-opt-item', e => {
@@ -10,7 +10,7 @@ export function init(id) {
1010
e.target.value = e.target.value.slice(1, 2);
1111
}
1212
}
13-
setValue(el);
13+
setValue(el, invoke, method);
1414
});
1515
EventHandler.on(el, 'keydown', '.bb-opt-item', e => {
1616
if (e.ctrlKey) {
@@ -63,13 +63,14 @@ export function init(id) {
6363
}
6464
}
6565
e.target.blur();
66-
setValue(el);
66+
setValue(el, invoke, method);
6767
});
6868
}
6969

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('');
70+
const setValue = (el, invoke, method) => {
71+
const val = [...el.querySelectorAll('.bb-opt-item')].map(input => input.value).join('');
72+
console.log(val);
73+
invoke.invokeMethodAsync(method, val);
7374
}
7475

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

0 commit comments

Comments
 (0)