Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion src/BootstrapBlazor/Components/Input/BootstrapInput.razor.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,28 @@
import EventHandler from "../../modules/event-handler.js"
import EventHandler from "../../modules/event-handler.js"


export function init(id, invoke, method) {
const el = document.getElementById(id)
EventHandler.on(el, 'focus', e => {
const isString = e.target.getAttribute('type') === 'text';
const isNumber = e.target.classList('class').contains("input-number")
if (isString && isNumber) {
let value = e.target.value;
e.target.setAttribute('type', 'number');
e.target.value = value.split(',').join('');
invoke.invokeMethodAsync(method, true);
}
});
EventHandler.on(el, 'blur', e => {
const isString = e.target.getAttribute('type') === 'number';
const isNumber = e.target.classList('class').contains("input-number")
if (isString && isNumber) {
e.target.setAttribute('type', 'text');
invoke.invokeMethodAsync(method, false);
}

});
}

export function focus(id) {
const el = document.getElementById(id)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ namespace BootstrapBlazor.Components;
/// <para lang="zh">An input component for editing numeric values. Supported numeric 类型s are <see cref="int"/>, <see cref="long"/>, <see cref="short"/>, <see cref="float"/>, <see cref="double"/>, <see cref="decimal"/></para>
/// <para lang="en">An input component for editing numeric values. Supported numeric types are <see cref="int"/>, <see cref="long"/>, <see cref="short"/>, <see cref="float"/>, <see cref="double"/>, <see cref="decimal"/></para>
/// </summary>
[BootstrapModuleAutoLoader("Input/BootstrapInput.razor.js", JSObjectReference = true)]
public partial class BootstrapInputNumber<TValue>
{
/// <summary>
Expand All @@ -29,6 +30,7 @@ public partial class BootstrapInputNumber<TValue>
/// </summary>
protected string? InputClassString => CssBuilder.Default("form-control")
.AddClass(CssClass).AddClass(ValidCss)
.AddClass("input-number")
.AddClass("input-number-fix", ShowButton)
.AddClass($"border-{Color.ToDescriptionString()}", Color != Color.None)
.AddClassFromAttributes(AdditionalAttributes)
Expand Down Expand Up @@ -120,6 +122,34 @@ protected override void OnInitialized()
{
_lastInputValueString ??= Value?.ToString();
}


}

/// <summary>
///
/// <inheritdoc/>
/// </summary>
protected override Task InvokeInitAsync() => InvokeVoidAsync("init", Id, Interop, nameof(TriggerFormatValue));

/// <summary>
/// <para lang="zh">Trigger value changed event 回调. Trigger by JavaScript</para>
/// <para lang="en">Trigger value changed event callback. Trigger by JavaScript</para>
/// </summary>
/// <param name="focus"></param>
[JSInvokable]
public Task TriggerFormatValue(bool focus)
{
if (focus)
{
CurrentValueAsString = InternalFormat(Value) ?? "";
}
else
{
CurrentValueAsString = GetFormatString(Value) ?? "";
}
StateHasChanged();
return Task.CompletedTask;
}

/// <summary>
Expand Down