app/BTCPayApp.UI/Components/InputAmount.razor
2024-08-19 17:56:27 +02:00

74 lines
2.6 KiB
Plaintext

@using BTCPayApp.UI.Util
@using NBitcoin
@inject DisplayFormatter DisplayFormatter
@inherits InputNumber<decimal?>
<div class="d-flex flex-wrap gap-2">
<div class="input-group flex-nowrap" style="flex: 1 0 150px">
<InputNumber @bind-Value="@CurrentValue" @bind-Value:after="() => UpdateFiatValue(CurrentValue)" readonly="@Readonly" id="@Id" class="form-control flex-grow-1 hide-number-spin text-end" inputmode="@(IsSats ? "numeric" : "decimal")" max="@MaxAttr" step="any" min="0"/>
<span class="input-group-text" @onclick="ToggleDisplayCurrency" @onclick:stopPropagation>@(IsSats ? "sats" : Unit)</span>
</div>
@if (Rate is > 0 && !string.IsNullOrEmpty(Currency))
{
<div class="input-group flex-nowrap" style="flex:1 0 140px">
<span class="input-group-text px-2">~</span>
<InputNumber @bind-Value="@FiatValue" @bind-Value:after="() => UpdateValue(FiatValue)" readonly="@Readonly" inputmode="decimal" class="input-group-text w-100 hide-number-spin text-end" step="any" min="0"/>
<span class="input-group-text">@Currency</span>
</div>
}
</div>
@code {
[Parameter, EditorRequired]
public string? Id { get; set; }
[Parameter, EditorRequired]
public MoneyUnit? Unit { get; set; }
[Parameter]
public decimal? Max { get; set; }
[Parameter]
public decimal? Rate { get; set; }
[Parameter]
public string? Currency { get; set; }
[Parameter]
public bool? Readonly { get; set; }
[Parameter]
public EventCallback OnToggleDisplayCurrency { get; set; }
private decimal? FiatValue { get; set; }
private bool IsSats => Unit == MoneyUnit.Satoshi;
private decimal? MaxAttr => Max.HasValue && Unit.HasValue ? new Money(Max.Value, Unit.Value).ToDecimal(Unit.Value) : null;
private decimal? EffectiveRate => Rate is null ? null : IsSats ? Rate / 100_000_000 : Rate;
protected override void OnParametersSet()
{
UpdateFiatValue(CurrentValue);
}
private async Task ToggleDisplayCurrency()
{
if (OnToggleDisplayCurrency.HasDelegate)
await OnToggleDisplayCurrency.InvokeAsync();
}
private void UpdateFiatValue(decimal? val)
{
FiatValue = val is null || EffectiveRate is null || string.IsNullOrEmpty(Currency)
? null
: DisplayFormatter.Rounded((val * EffectiveRate).Value, Currency);
}
private void UpdateValue(decimal? fiat)
{
CurrentValue = fiat is null || EffectiveRate is null || string.IsNullOrEmpty(Currency)
? null
: DisplayFormatter.Rounded((fiat / EffectiveRate).Value, IsSats ? "SATS" : "BTC");
}
}