MudBlazor

Text Field

Text field components are used for receiving user provided information.

Basic Text Field's

<MudGrid>
    <MudItem xs="12" sm="6" md="4">
        <MudTextField @bind-Value="TextValue" Label="Standard" Variant="Variant.Text"></MudTextField>
    </MudItem>
    <MudItem xs="12" sm="6" md="4">
        <MudTextField @bind-Value="TextValue" Label="Filled" Variant="Variant.Filled"></MudTextField>
    </MudItem>
    <MudItem xs="12" sm="6" md="4">
        <MudTextField @bind-Value="TextValue" Label="Outlined" Variant="Variant.Outlined"></MudTextField>
    </MudItem>
</MudGrid>
@code {
    public string TextValue { get; set; }
}

Dense

With the margin prop you can reduce the text field height.

<MudGrid>
    <MudItem xs="12" sm="6" md="4">
        <MudTextField @bind-Value="TextValue" Label="Standard" Variant="Variant.Text" Margin="Margin.Dense"></MudTextField>
    </MudItem>
    <MudItem xs="12" sm="6" md="4">
        <MudTextField @bind-Value="TextValue" Label="Filled" Variant="Variant.Filled" Margin="Margin.Dense"></MudTextField>
    </MudItem>
    <MudItem xs="12" sm="6" md="4">
        <MudTextField @bind-Value="TextValue" Label="Outlined" Variant="Variant.Outlined" Margin="Margin.Dense"></MudTextField>
    </MudItem>
</MudGrid>
@code {
    public string TextValue { get; set; }
}

Form Props

Required, Disabled, Type

Some helping Text

Some helping Text

Some helping Text

<MudGrid>
    <MudItem xs="12" sm="6" md="3">
        <MudTextField @bind-Value="HelperText" Label="With Helper" HelperText="Some helping Text" Variant="Variant.Text" />
    </MudItem>
    <MudItem xs="12" sm="6" md="3">
        <MudTextField @bind-Value="Disabled" Label="Disabled" Variant="Variant.Text" Disabled="true" />
    </MudItem>
    <MudItem xs="12" sm="6" md="3">
        <MudTextField @bind-Value="Password" Label="Password" InputType="InputType.Password" Variant="Variant.Text" />
    </MudItem>
    <MudItem xs="12" sm="6" md="3">
        <MudTextField @bind-Value="ReadOnly" Label="Read Only" ReadOnly="true" Variant="Variant.Text" />
    </MudItem>

    <MudItem xs="12" sm="6" md="3">
        <MudTextField @bind-Value="HelperText" Label="With Helper" HelperText="Some helping Text" Variant="Variant.Filled" />
    </MudItem>
    <MudItem xs="12" sm="6" md="3">
        <MudTextField @bind-Value="Disabled" Label="Disabled" Variant="Variant.Filled" Disabled="true" />
    </MudItem>
    <MudItem xs="12" sm="6" md="3">
        <MudTextField @bind-Value="Password" Label="Password" InputType="InputType.Password" Variant="Variant.Filled" />
    </MudItem>
    <MudItem xs="12" sm="6" md="3">
        <MudTextField @bind-Value="ReadOnly" Label="Read Only" ReadOnly="true" Variant="Variant.Filled" />
    </MudItem>

    <MudItem xs="12" sm="6" md="3">
        <MudTextField @bind-Value="HelperText" Label="With Helper" HelperText="Some helping Text" Variant="Variant.Outlined" />
    </MudItem>
    <MudItem xs="12" sm="6" md="3">
        <MudTextField @bind-Value="Disabled" Label="Disabled" Variant="Variant.Outlined" Disabled="true" />
    </MudItem>
    <MudItem xs="12" sm="6" md="3">
        <MudTextField @bind-Value="Password" Label="Password" InputType="InputType.Password" Variant="Variant.Outlined" />
    </MudItem>
    <MudItem xs="12" sm="6" md="3">
        <MudTextField @bind-Value="ReadOnly" Label="Read Only" ReadOnly="true" Variant="Variant.Outlined" />
    </MudItem>
</MudGrid>
@code {
    public string HelperText { get; set; }
    public string Disabled { get; set; }
    public string Password { get; set; }
    public string ReadOnly { get; set; } = "Can't change me";
}

Helper Text On Focus

With the HelperTextOnFocus property set to true, the helper text will only display on focus.

Some helping text

Some helping text

Some helping text

Some helping text

Some helping text

Some helping text

<MudGrid>
    <MudItem xs="6">
        <MudTextField T="string" Label="OnFocus Helper" HelperText="Some helping text" HelperTextOnFocus="true" Variant="Variant.Text" />
    </MudItem>
    <MudItem xs="6">
        <MudTextField T="string" Label="With Helper" HelperText="Some helping text" Variant="Variant.Text" />
    </MudItem>
    <MudItem xs="6">
        <MudTextField T="string" Label="OnFocus Helper" HelperText="Some helping text" HelperTextOnFocus="true" Variant="Variant.Filled" />
    </MudItem>
    <MudItem xs="6">
        <MudTextField T="string" Label="With Helper" HelperText="Some helping text" Variant="Variant.Filled" />
    </MudItem>
    <MudItem xs="6">
        <MudTextField T="string" Label="OnFocus Helper" HelperText="Some helping text" HelperTextOnFocus="true" Variant="Variant.Outlined" />
    </MudItem>
    <MudItem xs="6">
        <MudTextField T="string" Label="With Helper" HelperText="Some helping text" Variant="Variant.Outlined" />
    </MudItem>
</MudGrid>

Counter

If you set the Counter prop to any int, the counter will be display at the bottom of the textfield.
Using a specific number will show the desired max while setting it to 0 will only show the current count.
Add MaxLength to force a max count directly on the input and use Validation to validate the data.

This field uses Counter prop
0 / 25

This field uses Counter and MaxLength prop
0 / 25

This field has Counter set to 0
0

This field uses MaxLength prop

<MudGrid>
    <MudItem xs="12" md="6">
        <MudTextField T="string" Counter="25" HelperText="This field uses Counter prop" Immediate="true" Validation="@(new Func<string, IEnumerable<string>>(MaxCharacters))" Label="Regular" Variant="Variant.Text" />
    </MudItem>
    <MudItem xs="12" md="6">
        <MudTextField T="string" Counter="25" MaxLength="25" HelperText="This field uses Counter and MaxLength prop" Immediate="true" Validation="@(new Func<string, IEnumerable<string>>(MaxCharacters))" Label="Limited" Variant="Variant.Text" />
    </MudItem>
    <MudItem xs="12" md="6">
        <MudTextField T="string" Counter="0" HelperText="This field has Counter set to 0" Immediate="true" Label="Counter" Variant="Variant.Text" />
    </MudItem>

    <MudItem xs="12" md="6">
        <MudTextField T="string" MaxLength="10" HelperText="This field uses MaxLength prop" Immediate="true" Label="Max Length" Variant="Variant.Text" />
    </MudItem>
</MudGrid>
@code {
    private IEnumerable<string> MaxCharacters(string ch)
    {
        if (!string.IsNullOrEmpty(ch) && 25 < ch?.Length)
            yield return "Max 25 characters";
    }
}

Adornments

This can be used to add a prefix or a suffix. Text, Icon or Icon Button.

Kr

Kg

Weight

Kg

Weight

Kg

Weight

<MudGrid>
    <MudItem xs="12" sm="12" md="12">
        <MudTextField @bind-Value="Amount" Label="Amount" Variant="Variant.Text" Adornment="Adornment.Start" AdornmentText="Kr" />
    </MudItem>
    <MudItem xs="12" sm="6" md="4">
        <MudTextField @bind-Value="Amount" Label="Amount" Variant="Variant.Text" Adornment="Adornment.Start" AdornmentIcon="@Icons.Material.Filled.AttachMoney" />
    </MudItem>
    <MudItem xs="12" sm="6" md="4">
        <MudTextField @bind-Value="Weight" HelperText="Weight" Variant="Variant.Text" Adornment="Adornment.End" AdornmentText="Kg" />
    </MudItem>
    <MudItem xs="12" sm="6" md="4">
        <MudTextField @bind-Value="Password" Label="Password" Variant="Variant.Text" InputType="@PasswordInput" Adornment="Adornment.End" AdornmentIcon="@PasswordInputIcon" OnAdornmentClick="ButtonTestclick" />
    </MudItem>
    <MudItem xs="12" sm="6" md="4">
        <MudTextField @bind-Value="Amount" Label="Amount" Variant="Variant.Filled" Adornment="Adornment.Start" AdornmentIcon="@Icons.Material.Filled.AttachMoney" />
    </MudItem>
    <MudItem xs="12" sm="6" md="4">
        <MudTextField @bind-Value="Weight" HelperText="Weight" Variant="Variant.Filled" Adornment="Adornment.End" AdornmentText="Kg" />
    </MudItem>
    <MudItem xs="12" sm="6" md="4">
        <MudTextField  @bind-Value="Password" Label="Password"  Variant="Variant.Filled" InputType="@PasswordInput" Adornment="Adornment.End" AdornmentIcon="@PasswordInputIcon" OnAdornmentClick="ButtonTestclick" />
    </MudItem>
    <MudItem xs="12" sm="6" md="4">
        <MudTextField @bind-Value="Amount" Label="Amount" Variant="Variant.Outlined" Adornment="Adornment.Start" AdornmentIcon="@Icons.Material.Filled.AttachMoney" />
    </MudItem>
    <MudItem xs="12" sm="6" md="4">
        <MudTextField @bind-Value="Weight" HelperText="Weight" Variant="Variant.Outlined" Adornment="Adornment.End" AdornmentText="Kg" />
    </MudItem>
    <MudItem xs="12" sm="6" md="4">
        <MudTextField  @bind-Value="Password" Label="Password" Variant="Variant.Outlined" InputType="@PasswordInput" Adornment="Adornment.End" AdornmentIcon="@PasswordInputIcon" OnAdornmentClick="ButtonTestclick" />
    </MudItem>
</MudGrid>
@code {
    public double? Amount { get; set; }
    public int? Weight { get; set; }
    public string Password { get; set;} = "superstrong123";

    bool isShow;
    InputType PasswordInput = InputType.Password;
    string PasswordInputIcon = Icons.Material.Filled.VisibilityOff;

    void ButtonTestclick()
    {
        @if (isShow)
        {
            isShow = false;
            PasswordInputIcon = Icons.Material.Filled.VisibilityOff;
            PasswordInput = InputType.Password;
        }
        else {
            isShow = true;
            PasswordInputIcon = Icons.Material.Filled.Visibility;
            PasswordInput = InputType.Text;
        }
    }
}

Adornment Color

The color of the adornment can be changed separately from the textfield.

Kg

<MudGrid>
    <MudItem xs="12" sm="6" md="4">
        <MudTextField @bind-Value="Amount" Label="Amount" Variant="Variant.Outlined" Adornment="Adornment.Start" AdornmentIcon="@Icons.Material.Filled.AttachMoney" AdornmentColor="Color.Warning" />
    </MudItem>
    <MudItem xs="12" sm="6" md="4">
        <MudTextField @bind-Value="Weight" Label="Weight" Variant="Variant.Outlined" Adornment="Adornment.End" AdornmentText="Kg" AdornmentColor="Color.Info" />
    </MudItem>
    <MudItem xs="12" sm="6" md="4">
        <MudTextField  @bind-Value="Search" Label="Search" Variant="Variant.Outlined" Adornment="Adornment.End" AdornmentIcon="@Icons.Filled.Search" AdornmentColor="Color.Secondary" />
    </MudItem>
</MudGrid>
@code {
    public double? Amount { get; set; }
    public int? Weight { get; set; }
    public string Search { get; set;}
}

Binding Properties of a POCO

The following text fields are bound against the properties of a POCO (Plain old C-Sharp Object). Edit them to see the model change. Reset the model and see the textfields change.

Name: Hydrogen

Mass: 1.00794 u

Electrons: 1

Last Update: 10/18/2021

<MudGrid>
    <MudItem xs="12" sm="6" md="3">
        <MudTextField @bind-Value="@element.Name" Label="Name"/>
    </MudItem>
    <MudItem xs="12" sm="6" md="3">
        <MudTextField @bind-Value="@element.Mass" Label="Mass"/>
    </MudItem>
    <MudItem xs="12" sm="6" md="3">
        <MudTextField @bind-Value="@element.Electrons" Label="Electrons"/>
    </MudItem>
    <MudItem xs="12" sm="6" md="3">
        <MudTextField @bind-Value="@element.Changed" Format="yyyy/MM/dd" Label="Last Update"/>
    </MudItem>
</MudGrid>
<div class="mt-3 ml-0 d-flex">
    <div>
        <MudText>Name: @element.Name</MudText>
        <MudText>Mass: @element.Mass u</MudText>
        <MudText>Electrons: @element.Electrons</MudText>
        <MudText>Last Update: @element.Changed.ToShortDateString()</MudText>
    </div>
    <div class="d-flex ml-auto align-center">
        <MudButton Variant="Variant.Filled" OnClick="Reset">Reset Model</MudButton>
    </div>
</div>
@code {
    Atom element = new Atom { Name = "Hydrogen", Mass = 1.00794, Electrons = 1, Changed=DateTime.Today };

    // A typical POCO
    public class Atom
    {
        public string Name { get; set; }
        public double Mass { get; set; }
        public int Electrons { get; set; }
        public DateTime Changed { get; set; }
    }

    private void Reset()
    {
        element = new Atom { Name = "Hydrogen", Mass = 1.00794, Electrons = 1, Changed = DateTime.Today };
        StateHasChanged();
    }

}

Binding Value Types vs Nullables

When you bind value types, the text field will not be empty even if the user hasn't entered a value yet because a value type always has a value, even when unassigned. The two-way-bindable Value property will automatically assume the default value of that type (i.e. 0 for int).
So if you want your text fields to be empty as long as nothing has been entered yet, use the nullable version of that type (i.e. int?).

Enter an int

Enter a double

Enum (Yes|No|Maybe)

<MudGrid>
    <MudItem xs="12" sm="6" md="4">
        <MudTextField @bind-Value="intValue" Label="Enter an int" />
    </MudItem>
    <MudItem xs="12" sm="6" md="4">
        <MudTextField @bind-Value="doubleValue" Label="Enter a double" Format="F1"/>
    </MudItem>
    <MudItem xs="12" sm="6" md="4">
        <MudTextField @bind-Value="enumValue" Label="Enum (Yes|No|Maybe)" />
    </MudItem>
</MudGrid>
<MudGrid>
    <MudItem xs="12" sm="6" md="4">
        <MudTextField @bind-Value="nullableInt" HelperText="Enter an int" />
    </MudItem>
    <MudItem xs="12" sm="6" md="4">
        <MudTextField @bind-Value="nullableDouble" HelperText="Enter a double" Format="F1" />
    </MudItem>
    <MudItem xs="12" sm="6" md="4">
        <MudTextField @bind-Value="nullableEnum" HelperText="Enum (Yes|No|Maybe)" />
    </MudItem>
</MudGrid>
@code { 
    int intValue;
    double doubleValue;
    YesNoMaybe enumValue;
    int? nullableInt;
    double? nullableDouble;
    YesNoMaybe? nullableEnum;

    public enum YesNoMaybe { Maybe, Yes, No }
}

Immediate vs Debounced

By default, MudTextField updates the bound value on Enter or when it loses focus. Set Immediate="true" to update the value whenever the user types.
You can also set the DebounceInterval parameter to the number of milliseconds you want to wait before updating the bound value. If you need to know when the interval elapses, you can pass an OnDebounceIntervalElapsed EventCallback. Notice how in this example the debounced text only updates 500 ms after you stop typing.

Normal:

Immediate:

Debounced:

<MudGrid>
    <MudItem xs="12" sm="6" md="4">
        <MudTextField @bind-Value="@_normalText"
                      Label="Normal"
                      Variant="Variant.Outlined"
                      Adornment="Adornment.End"
                      AdornmentIcon="@Icons.Filled.Search"/>
    </MudItem>
    <MudItem xs="12" sm="6" md="4">
        <MudTextField @bind-Value="@_immediateText"
                      Label="Immediate"
                      Variant="Variant.Outlined"
                      Adornment="Adornment.End"
                      AdornmentIcon="@Icons.Filled.Search"
                      Immediate="true" />
    </MudItem>
    <MudItem xs="12" sm="6" md="4">
        <MudTextField @bind-Value="@_debouncedText"
                      Label="Debounced"
                      Variant="Variant.Outlined"
                      Adornment="Adornment.End"
                      AdornmentIcon="@Icons.Filled.Search"
                      DebounceInterval="500"
                      OnDebounceIntervalElapsed="HandleIntervalElapsed" />
    </MudItem>
</MudGrid>
<div class="mt-3">
    <MudText>Normal: @_normalText</MudText>
    <MudText>Immediate: @_immediateText</MudText>
    <MudText>Debounced: @_debouncedText</MudText>
</div>
@code {
    string _normalText;
    string _immediateText;
    string _debouncedText;

    void HandleIntervalElapsed(string debouncedText)
    {
        // at this stage, interval has elapsed
    }
}

Clear button and special input types

MudTextField can show a clear button and you can set the HTML5 input type. The input type can be important on mobile devices to show an appropriate keyboard.

Clearable textfield:

InputType telephone:

<MudGrid>
    <MudItem xs="12" sm="6" md="4">
        <MudTextField @bind-Value="@_searchString"
                              Immediate="true" 
                              Placeholder="Clearable"
                              Clearable="true"
                              Class="mt-0" />
    </MudItem>
    <MudItem xs="12" sm="6" md="4">
        <MudTextField @bind-Value="@_telephone"
                      Label="InputType.Telephone"
                      Immediate="true" />
    </MudItem>
</MudGrid>
<div class="mt-3">
    <MudText>Clearable textfield: @_searchString</MudText>
    <MudText>InputType telephone: @_telephone</MudText>
</div>
@code {
    string _searchString;
    string _telephone;
}

Multiline

<MudGrid>
    <MudItem xs="12" sm="12" md="12">
        <MudTextField T="string" Label="Multiline" Variant="Variant.Text" Text="@sampleText" Lines="5" />
    </MudItem>
    <MudItem xs="12" sm="6" md="6">
        <MudTextField T="string" Label="Filled" Variant="Variant.Filled" Text="@sampleText" Lines="3" />
    </MudItem>
    <MudItem xs="12" sm="6" md="6">
        <MudTextField T="string" Label="Outlined" Variant="Variant.Outlined" Text="@sampleText" Lines="3" />
    </MudItem>
</MudGrid>
@code
{
    string sampleText = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.";
}

Focus

<MudGrid>
    <MudItem xs="12">
        <MudTextField @ref="multilineReference" T="string" Label="Manual focus" Variant="Variant.Filled" Text="@sampleText" Lines="3" />
    </MudItem>
    <MudItem xs="12" Class="d-flex justify-end">
        <MudButton @onclick="() => multilineReference.FocusAsync()">
            Focus
        </MudButton>
    </MudItem>
    <MudItem xs="12">
        <MudTextField @ref="singleLineReference" T="string" Label="Manual focus" Variant="Variant.Filled" Text="@sampleText" />
    </MudItem>
    <MudItem xs="12" Class="d-flex justify-end">
        <MudButton @onclick="() => singleLineReference.FocusAsync()">
            Focus
        </MudButton>
    </MudItem>
</MudGrid>
@code
{
    private MudTextField<string> multilineReference;
    private MudTextField<string> singleLineReference;

    string sampleText = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.";
}

Select

<MudGrid>
    <MudItem xs="12" sm="12" md="12">
        <MudTextField @ref="multilineReference" T="string" Label="Multiline Select" Lines="3" Variant="Variant.Text" Text="@sampleText" />
    </MudItem>
    <MudItem xs="12" Class="d-flex justify-end">
        <MudButton @onclick="() => multilineReference.SelectAsync()">
            Select
        </MudButton>
    </MudItem>
    <MudItem xs="12">
        <MudTextField @ref="singleLineReference" T="string" Label="Single Select" Variant="Variant.Filled" Text="@sampleText" />
    </MudItem>
    <MudItem xs="12" Class="d-flex justify-end">
        <MudButton @onclick="() => singleLineReference.SelectAsync()">
            Select
        </MudButton>
    </MudItem>
</MudGrid>
@code
{
    private MudTextField<string> multilineReference;
    private MudTextField<string> singleLineReference;

    string sampleText = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.";
}

Select Range

<MudGrid>
    <MudItem xs="12" sm="12" md="12">
        <MudTextField @ref="multilineReference" T="string" Label="Multiline Select" Lines="3" Variant="Variant.Text" Text="@sampleText" />
    </MudItem>
    <MudItem xs="12" Class="d-flex justify-end">
        <MudButton @onclick="() => multilineReference.SelectRangeAsync(5, 10)">
            Select
        </MudButton>
    </MudItem>
    <MudItem xs="12">
        <MudTextField @ref="singleLineReference" T="string" Label="Single Select" Variant="Variant.Filled" Text="@sampleText" />
    </MudItem>
    <MudItem xs="12" Class="d-flex justify-end">
        <MudButton @onclick="() => singleLineReference.SelectRangeAsync(5, 10)">
            Select
        </MudButton>
    </MudItem>
</MudGrid>
@code
{
    private MudTextField<string> multilineReference;
    private MudTextField<string> singleLineReference;

    string sampleText = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."; 
}

Inputs

<MudInput Value="@("Basic Input")" />
<MudInput T="string" Placeholder="Placeholder" />
<MudInput Value="@("Disabled")" Disabled="true" />
<MudInput Value="@("Error")" Error="true" />
An error has occurred. This application may no longer respond until reloaded. Reload 🗙