|
| 1 | +--- |
| 2 | +title: Add Floating Label to Telerik Inputs |
| 3 | +description: How to add floating label in the Telerik Inputs. |
| 4 | +type: how-to |
| 5 | +page_title: Floating Label in Telerik Inputs |
| 6 | +slug: inputs-kb-floating-label |
| 7 | +position: |
| 8 | +tags: |
| 9 | +res_type: kb |
| 10 | +--- |
| 11 | + |
| 12 | +## Environment |
| 13 | +<table> |
| 14 | + <tbody> |
| 15 | + <tr> |
| 16 | + <td>Product</td> |
| 17 | + <td>TextBox for Blazor, MaskedTextBox for Blazor, NumericTextBox for Blazor, other inputs/form elements</td> |
| 18 | + </tr> |
| 19 | + </tbody> |
| 20 | +</table> |
| 21 | + |
| 22 | + |
| 23 | +## Description |
| 24 | + |
| 25 | +I would like to show a floating label for Telerik Inputs. The label should be displayed like a placeholder when the component is empty, and should animate to top label position of the input when focused. |
| 26 | + |
| 27 | +## Solution |
| 28 | + |
| 29 | +>important The 3.0.0 release introduced a breaking change of removing the `Label` parameter from TextBox, MaskedTextBox, TextArea components. In 3.2.0 Floating Label component will be released to fulfill all use cases. However, in the meantime this solution can be used to implement floating label in any component. |
| 30 | +
|
| 31 | +The following code snippet shows how to add a floating label to TextBox, MaskedTextBox, TextArea, DatePicker, and DropDownList components. The example illustrates how with simple html rendering that is shipped with `Telerik UI for Blazor` you could transform the inner html label into a floating label. |
| 32 | + |
| 33 | +````CSHTML |
| 34 | +<div style="width: 400px;"> |
| 35 | + <span class="k-floating-label-container @TextBoxEmptyClass"> |
| 36 | + <TelerikTextBox Id="textbox" @bind-Value="@TextBoxValue"></TelerikTextBox> |
| 37 | + <label for="textbox" class="k-label">TextBox</label> |
| 38 | + </span> |
| 39 | + <br/><br/> |
| 40 | + <span class="k-floating-label-container @MaskedTextBoxEmptyClass"> |
| 41 | + <TelerikMaskedTextBox Mask="###.###" Id="maskedtextbox" PlaceHolder=" " MaskOnFocus="true" @bind-Value="@MaskedTextBoxValue"></TelerikMaskedTextBox> |
| 42 | + <label for="maskedtextbox" class="k-label">MaskedTextBox</label> |
| 43 | + </span> |
| 44 | + <br/><br/> |
| 45 | + <span class="k-floating-label-container @TextAreaEmptyClass"> |
| 46 | + <TelerikTextArea Id="textarea" @bind-Value="@TextAreaValue"></TelerikTextArea> |
| 47 | + <label for="textarea" class="k-label">TextArea</label> |
| 48 | + </span> |
| 49 | + <br/><br/> |
| 50 | + <span class="k-floating-label-container @DatePickerEmptyClass"> |
| 51 | + <!-- Add empty space as Placeholder to show empty datepicker by default --> |
| 52 | + <TelerikDatePicker Id="datepicker" @bind-Value="@DatePickerValue" Placeholder=" "></TelerikDatePicker> |
| 53 | + <label for="datepicker" class="k-label">DatePicker</label> |
| 54 | + </span> |
| 55 | + <br/><br/> |
| 56 | + <span class="k-floating-label-container @DropDownListEmptyClass"> |
| 57 | + <TelerikDropDownList Id="ddl" @bind-Value="@DropDownListValue" Data="@DropDownListData"></TelerikDropDownList> |
| 58 | + <label for="ddl" class="k-label">DropDownList</label> |
| 59 | + </span> |
| 60 | +</div> |
| 61 | +
|
| 62 | +@code { |
| 63 | + public const string emptyClass = "k-state-empty"; |
| 64 | +
|
| 65 | + public string TextBoxValue { get; set; } |
| 66 | + public string TextBoxEmptyClass => string.IsNullOrEmpty(TextBoxValue) ? emptyClass : string.Empty; |
| 67 | +
|
| 68 | + public string MaskedTextBoxValue { get; set; } |
| 69 | + public string MaskedTextBoxEmptyClass => string.IsNullOrEmpty(MaskedTextBoxValue) ? emptyClass : string.Empty; |
| 70 | +
|
| 71 | + public string TextAreaValue { get; set; } |
| 72 | + public string TextAreaEmptyClass => string.IsNullOrEmpty(TextAreaValue) ? emptyClass : string.Empty; |
| 73 | +
|
| 74 | + public DateTime? DatePickerValue { get; set; } |
| 75 | + public string DatePickerEmptyClass => DatePickerValue == null ? emptyClass : string.Empty; |
| 76 | +
|
| 77 | + public string DropDownListValue { get; set; } |
| 78 | + public string DropDownListEmptyClass => string.IsNullOrEmpty(DropDownListValue) ? emptyClass : string.Empty; |
| 79 | + public List<string> DropDownListData { get; set; } = new List<string>() { "one", "two", "three" }; |
| 80 | +} |
| 81 | +
|
| 82 | +```` |
0 commit comments