Files
lux/Lux.UI/Components/Compo/Base/InputPercent.razor
T
2025-10-31 11:21:01 +01:00

33 lines
896 B
Plaintext

@inherits InputBase<decimal>
<input @bind="CurrentValueAsString" class="@CssClass" />
@code {
// Format the bound value as "10%" when rendering
protected override string FormatValueAsString(decimal value)
{
return value == 0 ? string.Empty : $"{value:P2}";
}
// Parse the user's input back into a decimal
protected override bool TryParseValueFromString(
string value,
out decimal result,
out string validationErrorMessage)
{
var cleaned = value?.Replace("%", "").Trim();
if (decimal.TryParse(cleaned, out var parsed))
{
// se > 1 --> divido per 100...
result = parsed < 1 ? parsed : parsed / 100;
validationErrorMessage = null;
return true;
}
result = 0;
validationErrorMessage = "Invalid percentage";
return false;
}
}