Quantcast
Channel: Active questions tagged blazor - Stack Overflow
Viewing all articles
Browse latest Browse all 4839

Custom Blazor InputText component

$
0
0

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 registered

  • It seems to work only for the first char I type - any subsequent chars don't seem to trigger the TextInputEvent handler anymore...

Any ideas what I'm missing here?


Viewing all articles
Browse latest Browse all 4839

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>