I'm trying to create a "composite" Blazor text input component.
I'd like to combine
- a title/label for the input text field
- the actual input text field
- a counter to show how many characters of the defined max length have already been used
So far, I've created something like this (slightly simplified):
MyInputText.razor:
<div class="row"><div class="col-md-3"><label>@Title</label></div><div class="col-md-8"><InputText class="form-control" name="textbox" @bind-Value="Value" @oninput="TextInputEvent" /><ValidationMessage For="() => Value" /></div><div class="col-md-1"><label>@CurrentChars / @MaxChars</label></div></div>MyInputText.razor.cs:
public partial class MyTextInput{ [Parameter] [EditorRequired] public string Title { get; set; } = string.Empty; [Parameter] [EditorRequired] public string Value { get; set; } = string.Empty; [Parameter] [EditorRequired] public int MaxChars { get; set; } = 255; public int CurrentChars { get; set; } private void TextInputEvent() { CurrentChars = Value.Length; }}It seems to almost work - but:
It seems to be always "one step behind" - so if I have four characters, and type in a fifth one - the "CurrentChars" is set to 4. Seems it's looking at the "old" content of
Value- before the char I've just typed is registeredIt seems to work only for the first char I type - any subsequent chars don't seem to trigger the
TextInputEventhandler anymore...
Any ideas what I'm missing here?