Accent color
# SearchBox
## Description
A search box (SearchBox) provides an input field for searching content within a site or app to find specific items.
## Parameters
| Name | Type | Default Value | Description |
| :--- | :--- | :------------ | :---------- |
| Background | `BitColorKind?` | null | The background color kind of the search box. |
| Classes | `BitSearchBoxClassStyles?` | null | Custom CSS classes for different parts of the search box. |
| ClearButtonIcon | `BitIconInfo?` | null | Gets or sets the icon to display on the clear button using custom CSS classes for external icon libraries. Takes precedence over ClearButtonIconName when both are set. |
| ClearButtonIconName | `string?` | Cancel | Gets or sets the name of the icon to display on the clear button from the built-in Fluent UI icons. |
| ClearButtonTemplate | `RenderFragment?` | null | The custom template for clear button icon. |
| Color | `BitColor?` | null | The general color of the search box, used for colored parts like icons. |
| DisableAnimation | `bool` | false | Whether or not to animate the search box icon on focus. |
| FixedCalloutWidth | `bool` | false | Forces the suggest callout width to be always fixed at the component's width. |
| FixedIcon | `bool` | false | Whether or not to make the icon be always visible (it hides by default when the search box is focused). |
| HideIcon | `bool` | false | Whether or not the icon is visible. |
| HideClearButton | `bool` | false | Whether to hide the clear button when the search box has value. |
| Icon | `BitIconInfo?` | null | Gets or sets the icon to display using custom CSS classes for external icon libraries. Takes precedence over IconName when both are set. |
| IconName | `string?` | Search | Gets or sets the name of the icon to display from the built-in Fluent UI icons. |
| InputMode | `BitInputMode?` | null | Sets the inputmode html attribute of the input element. |
| MaxSuggestCount | `int` | 5 | The maximum number of items or suggestions that will be displayed. |
| MinSuggestTriggerChars | `int` | 3 | The minimum character requirement for doing a search in suggested items. |
| Modeless | `bool` | false | Removes the overlay of suggest items callout. |
| NoBorder | `bool` | false | Removes the default border of the search box. |
| OnClear | `EventCallback` | | Callback executed when the user clears the search box by either clicking 'X' or hitting escape. |
| OnEscape | `EventCallback` | | Callback executed when the user presses escape in the search box. |
| OnSearch | `EventCallback<string?>` | | Callback executed when the user presses enter in the search box. |
| Placeholder | `string?` | null | Placeholder for the search box. |
| Prefix | `string?` | null | Prefix text displayed before the search box input. This is not included in the value. |
| PrefixTemplate | `RenderFragment?` | null | The custom template for the prefix of the search box. |
| SearchButtonIcon | `BitIconInfo?` | null | Gets or sets the icon to display on the search button using custom CSS classes for external icon libraries. Takes precedence over SearchButtonIconName when both are set. |
| SearchButtonIconName | `string?` | ChromeBackMirrored | Gets or sets the name of the icon to display on the search button from the built-in Fluent UI icons. |
| SearchButtonTemplate | `RenderFragment?` | null | The custom template for search button icon. |
| ShowSearchButton | `bool` | false | Whether to show the search button. |
| Styles | `BitSearchBoxClassStyles?` | null | Custom CSS styles for different parts of the search box. |
| Suffix | `string?` | null | Suffix text displayed after the search box input. This is not included in the value. |
| SuffixTemplate | `RenderFragment?` | null | The custom template for the suffix of the search box. |
| SuggestFilterFunction | `Func<string?, string?, bool>?` | null | Custom search function to be used in place of the default search algorithm. |
| SuggestItems | `ICollection<string>?` | null | The list of suggest items to display in the callout. |
| SuggestItemsProvider | `BitSearchBoxSuggestItemsProvider?` | null | The item provider function providing suggest items. |
| SuggestItemTemplate | `RenderFragment<string>?` | null | The custom template for rendering the suggest items of the search box. |
| Underlined | `bool` | false | Whether or not the search box is underlined. |
| AutoComplete | `string?` | null | Specifies the value of the autocomplete attribute of the input component. |
| AutoFocus | `bool` | false | Determines if the text input is auto focused on first render. |
| DebounceTime | `int` | 0 | The debounce time in milliseconds. |
| Immediate | `bool` | false | Change the content of the input field when the user write text (based on 'oninput' HTML event). |
| ThrottleTime | `int` | 0 | The throttle time in milliseconds. |
| DefaultValue | `TValue?` | null | The default value of the input to be used in uncontrolled mode (i.e. when the Value is not bound), typically used alongside the OnChange callback. |
| DisplayName | `string?` | null | Gets or sets the display name for this field. |
| InputHtmlAttributes | `IReadOnlyDictionary<string, object>?` | null | Gets or sets a collection of additional attributes that will be applied to the created element. |
| Name | `string?` | null | Gets or sets the name of the element. Allows access by name from the associated form. |
| NoValidate | `bool` | false | Disables the validation of the input. |
| OnChange | `EventCallback<TValue?>` | | Callback for when the input value changes. |
| ReadOnly | `bool` | false | Makes the input read-only. |
| Required | `bool` | false | Makes the input required. |
| Value | `TValue?` | null | Gets or sets the value of the input. This should be used with two-way binding. |
| AriaLabel | `string?` | null | Gets or sets the accessible label for the component, used by assistive technologies. |
| Class | `string?` | null | Gets or sets the CSS class name(s) to apply to the rendered element. |
| Dir | `BitDir?` | null | Gets or sets the text directionality for the component's content. |
| ForceAnimation | `bool` | false | Gets or sets a value indicating whether the component's animations play at their full duration even when reduced motion is requested. |
| HtmlAttributes | `Dictionary<string, object>` | new Dictionary<string, object>() | Captures additional HTML attributes to be applied to the rendered element, in addition to the component's parameters. |
| Id | `string?` | null | Gets or sets the unique identifier for the component's root element. |
| IsEnabled | `bool` | true | Gets or sets a value indicating whether the component is enabled and can respond to user interaction. |
| Style | `string?` | null | Gets or sets the CSS style string to apply to the rendered element. |
| TabIndex | `string?` | null | Gets or sets the tab order index for the component when navigating with the keyboard. |
| Visibility | `BitVisibility` | BitVisibility.Visible | Gets or sets the visibility state (visible, hidden, or collapsed) of the component. |
## Public Members
| Name | Type | Default Value | Description |
| :--- | :--- | :------------ | :---------- |
| InputElement | `ElementReference` | | The ElementReference to the input element of the BitSearchBox. |
| FocusAsync | `ValueTask` | | Gives focus to the input element of the BitSearchBox. |
| InputElement | `ElementReference` | | The ElementReference of the input element. |
| FocusAsync() | `() => ValueTask` | | Gives focus to the input element. |
| FocusAsync(bool preventScroll) | `(bool preventScroll) => ValueTask` | | Gives focus to the input element. |
| UniqueId | `Guid` | Guid.NewGuid() | Gets the readonly unique identifier for the component's root element, assigned when the component instance is constructed. |
| RootElement | `ElementReference` | | Gets the reference to the root HTML element associated with this component. |
## Enums
### BitColorKind Enum
| Name | Value | Description |
| :--- | :--- | :---------- |
| Primary | 0 | The primary color kind. |
| Secondary | 1 | The secondary color kind. |
| Tertiary | 2 | The tertiary color kind. |
| Transparent | 3 | The transparent color kind. |
### BitColor Enum
| Name | Value | Description |
| :--- | :--- | :---------- |
| Primary | 0 | Info Primary general color. |
| Secondary | 1 | Secondary general color. |
| Tertiary | 2 | Tertiary general color. |
| Info | 3 | Info general color. |
| Success | 4 | Success general color. |
| Warning | 5 | Warning general color. |
| SevereWarning | 6 | SevereWarning general color. |
| Error | 7 | Error general color. |
| PrimaryBackground | 8 | Primary background color. |
| SecondaryBackground | 9 | Secondary background color. |
| TertiaryBackground | 10 | Tertiary background color. |
| PrimaryForeground | 11 | Primary foreground color. |
| SecondaryForeground | 12 | Secondary foreground color. |
| TertiaryForeground | 13 | Tertiary foreground color. |
| PrimaryBorder | 14 | Primary border color. |
| SecondaryBorder | 15 | Secondary border color. |
| TertiaryBorder | 16 | Tertiary border color. |
### BitInputMode Enum
| Name | Value | Description |
| :--- | :--- | :---------- |
| None | 0 | The input expects text characters. |
| Text | 1 | Standard input keyboard for the user's current locale. |
| Decimal | 2 | Fractional numeric input keyboard containing the digits and decimal separator for the user's locale. |
| Numeric | 3 | Numeric input keyboard, but only requires the digits 0–9. |
| Tel | 4 | A telephone keypad input, including the digits 0–9, the asterisk (*), and the pound (#) key |
| Search | 5 | A virtual keyboard optimized for search input. |
| Email | 6 | A virtual keyboard optimized for entering email addresses. |
| Url | 7 | A keypad optimized for entering URLs. |
### BitVisibility Enum
| Name | Value | Description |
| :--- | :--- | :---------- |
| Visible | 0 | The content of the component is visible. |
| Hidden | 1 | The content of the component is hidden, but the space it takes on the page remains (visibility:hidden). |
| Collapsed | 2 | The component is hidden (display:none). |
### BitDir Enum
| Name | Value | Description |
| :--- | :--- | :---------- |
| Ltr | 0 | Ltr (left to right) is to be used for languages that are written from the left to the right (like English). |
| Rtl | 1 | Rtl (right to left) is to be used for languages that are written from the right to the left (like Arabic). |
| Auto | 2 | Auto lets the user agent decide. It uses a basic algorithm as it parses the characters inside the element until it finds a character with a strong directionality, then applies that directionality to the whole element. |
## Sub Classes
### BitSearchBoxClassStyles Properties
| Name | Type | Default Value | Description |
| :--- | :--- | :------------ | :---------- |
| Root | `string?` | null | Custom CSS classes/styles for the root element of the search box. |
| Focused | `string?` | null | Custom CSS classes/styles for the focus state of the search box. |
| InputContainer | `string?` | null | Custom CSS classes/styles for the search box's input container. |
| IconWrapper | `string?` | null | Custom CSS classes/styles for the search box's icon wrapper. |
| Icon | `string?` | null | Custom CSS classes/styles for the search box's search icon. |
| PrefixContainer | `string?` | null | Custom CSS classes/styles for the search box's search prefix container. |
| Prefix | `string?` | null | Custom CSS classes/styles for the search box's search prefix. |
| Input | `string?` | null | Custom CSS classes/styles for the search box's Input. |
| SuffixContainer | `string?` | null | Custom CSS classes/styles for the search box's search suffix container. |
| Suffix | `string?` | null | Custom CSS classes/styles for the search box's search suffix. |
| ClearButton | `string?` | null | Custom CSS classes/styles for the search box's clear button. |
| ClearButtonIcon | `string?` | null | Custom CSS classes/styles for the search box's clear button icon. |
| SearchButton | `string?` | null | Custom CSS classes/styles for the search box's search button. |
| SearchButtonIcon | `string?` | null | Custom CSS classes/styles for the search box's search button icon. |
| Overlay | `string?` | null | Custom CSS classes/styles for the search box's overlay. |
| Callout | `string?` | null | Custom CSS classes/styles for the search box's callout. |
| ScrollContainer | `string?` | null | Custom CSS classes/styles for the search box's scroll container. |
| SuggestItemWrapper | `string?` | null | Custom CSS classes/styles for the search box's suggest item wrapper. |
| SuggestItemButton | `string?` | null | Custom CSS classes/styles for the search box's suggest item button. |
| SuggestItemText | `string?` | null | Custom CSS classes/styles for the search box's suggest item text. |
### BitIconInfo Properties
| Name | Type | Default Value | Description |
| :--- | :--- | :------------ | :---------- |
| Name | `string?` | null | Gets or sets the name of the icon. |
| BaseClass | `string?` | null | Gets or sets the base CSS class for the icon. For built-in Fluent UI icons, this defaults to "bit-icon". For external icon libraries like FontAwesome, you might set this to "fa" or leave empty. |
| Prefix | `string?` | null | Gets or sets the CSS class prefix used before the icon name. For built-in Fluent UI icons, this defaults to "bit-icon--". For external icon libraries, you might set this to "fa-" or leave empty. |
## Examples
\n**Basic**:
```razor
```
\n**Underlined**:
```razor
```
\n**NoBorder**:
```razor
```
\n**Background**:
```razor
```
\n**Icon**:
```razor
```
\n**Search Button**:
```razor
```
\n**Clear Button**:
```razor
```
\n**Prefix & Suffix**:
```razor
```
\n**Binding**:
```razor
```
```csharp
private string searchValue;
private string searchValueWithSuggestFilterFunction;
private string searchValueWithSearchDelay;
private string searchValueWithMinSearchLength;
private string searchValueWithMaxSuggestedItems;
private string searchValueWithItemsProvider;
private List GetSuggestedItems() =>
[
"Apple",
"Red Apple",
"Blue Apple",
"Green Apple",
"Banana",
"Orange",
"Grape",
"Broccoli",
"Carrot",
"Lettuce"
];
private List GetLongSuggestedItems() =>
[
"Apple Apple Apple Apple Apple Apple Apple Apple Apple Apple Apple Apple Apple Apple Apple Apple",
"Red Apple Apple Apple Apple Apple Apple Apple Apple Apple Apple Apple Apple Apple Apple Apple Apple",
"Blue Apple Apple Apple Apple Apple Apple Apple Apple Apple Apple Apple Apple Apple Apple Apple Apple",
"Green Apple Apple Apple Apple Apple Apple Apple Apple Apple Apple Apple Apple Apple Apple Apple Apple",
"Banana Banana Banana Banana Banana Banana Banana Banana Banana Banana Banana Banana Banana Banana Banana",
"Orange Orange Orange Orange Orange Orange Orange Orange Orange Orange Orange Orange Orange Orange Orange",
"Grape Grape Grape Grape Grape Grape Grape Grape Grape Grape Grape Grape Grape Grape Grape Grape Grape Grape",
"Broccoli Broccoli Broccoli Broccoli Broccoli Broccoli Broccoli Broccoli Broccoli Broccoli Broccoli Broccoli",
"Carrot Carrot Carrot Carrot Carrot Carrot Carrot Carrot Carrot Carrot Carrot Carrot Carrot Carrot Carrot Carrot",
"Lettuce Lettuce Lettuce Lettuce Lettuce Lettuce Lettuce Lettuce Lettuce Lettuce Lettuce Lettuce Lettuce Lettuce"
];
private Func SearchFunc = (string searchText, string itemText) =>
{
if (string.IsNullOrEmpty(searchText) || string.IsNullOrEmpty(itemText)) return false;
return itemText.StartsWith(searchText, StringComparison.OrdinalIgnoreCase);
};
private async ValueTask> LoadItems(BitSearchBoxSuggestItemsProviderRequest request)
{
try
{
var query = new Dictionary()
{
{ "$top", request.Take < 1 ? 5 : request.Take },
};
if (string.IsNullOrEmpty(request.SearchTerm) is false)
{
query.Add("$filter", $"contains(toupper(Name),'{request.SearchTerm.ToUpper()}')");
}
var url = NavManager.GetUriWithQueryParameters("api/Products/GetProducts", query);
var data = await HttpClient.GetFromJsonAsync(url, AppJsonContext.Default.PagedResultProductDto, request.CancellationToken);
return data!.Items!.Select(i => i.Name)!;
}
catch
{
return [];
}
}
```
\n**Validation**:
```razor
```
```csharp
public class ValidationSearchBoxModel
{
[StringLength(6, MinimumLength = 2, ErrorMessage = "Text must be between 2 and 6 chars.")]
public string Text { get; set; }
}
private ValidationSearchBoxModel validationBoxModel = new();
```
\n**Color**:
```razor
```
\n**External Icons**:
```razor
```
\n**Style & Class**:
```razor
```
\n**RTL**:
```razor
```
Search Value: @onChangeSearchValue
Search Value: @onSearchValue
```
```csharp
private string twoWaySearchValue;
private string onChangeSearchValue;
private string onSearchValue;
```
\n**Suggestion (AutoComplete)**:
```razor
SearchValue: @searchValue
SearchValue: @searchValueWithSuggestFilterFunction
SearchValue: @searchValueWithMinSearchLength
SearchValue: @searchValueWithMaxSuggestedItems
SearchValue: @searchValueWithSearchDelay
SearchValue: @searchValueWithItemsProvider