SearchBox
Description
SearchBox provides a dedicated input field for searching through content. It is designed to capture search queries either when the user presses "Enter" or automatically as they type. Being a part of the Components group, it offers customization options such as underlining, custom icons, disabled states, and more. Use the SearchBox when you need an interactive search input that supports user-triggered search events.
Usage
To instantiate a SearchBox, use the static helper method from Tesserae.UI. You can chain various methods to configure its appearance and behavior, including setting placeholder text, enabling a "search as you type" mode, customizing the icon, and handling the search event.
A trailing clear button appears whenever the box holds text, whether or not it is focused, and needs no setup: pressing it empties the box, keeps the caret in it and raises OnSearch with the empty query. Clear() does the same from code. The button is drawn by the toolkit rather than by the browser, so it looks the same in every browser instead of only appearing in Chromium in the browser's own colour and weight.
The input is only as tall as its text and sits centred in a box roughly twice its height, so most of what looks like the search box is the container around it. A press anywhere in that container focuses the input, and a box already holding the caret keeps it where it is.
Below is a simple example demonstrating how to create a SearchBox:
API reference
public class SearchBox : ComponentBase<SearchBox, HTMLInputElement>, ITextFormating, IHasBackgroundColor, ITabIndex, IRoundedStyleA single-line search input with a leading magnifier glyph, a trailing clear button and search-on-enter / debounced-input semantics.
- Namespace
- Tesserae
- Inheritance
- ComponentBase<SearchBox, HTMLInputElement> → SearchBox
- Implements
- ITextFormating, IHasBackgroundColor, ITabIndex, IRoundedStyle
Constructors
| Name | Description |
|---|---|
| SearchBox | Initializes a new instance of this class. |
Properties
| Name | Description |
|---|---|
| TabIndex | Sets the keyboard tab order of the component. |
| IsFocused | Returns a value indicating whether the component is focused. |
| IsEnabled | Gets or sets a value indicating whether the component is interactive (enabled). |
| IsUnderlined | Returns a value indicating whether the component is underlined. |
| Text | Gets or sets the text shown in the component. |
| Placeholder | Gets or sets the placeholder text shown when the component is empty. |
| IsInvalid | Gets or sets a value indicating whether the component is currently in an invalid state. |
| IsBusy | Gets or sets a value indicating whether the component is waiting on the search it asked for. While set, a spinner stands where the clear button does and the box reports itself as busy to assistive technology; the box stays editable throughout. On a box with an OnCancel handler, a pointer on it swaps the spinner for the cancel button. It says nothing about Failed, which the caller takes down itself - usually with ClearFailure where it starts the next search. |
| IsFailed | Returns a value indicating whether the box is showing that its last search did not answer. |
| Size | Gets or sets the size of the component. |
| Weight | Gets or sets the font weight of the component. |
| TextAlign | Gets or sets the text alignment of the component. |
| Background | Gets or sets the CSS background of the component. |
| IsShowingProgress | Returns a value indicating whether the progress bar along the bottom edge of the box is showing. |
public int TabIndex { set ; }Sets the keyboard tab order of the component.
public bool IsFocusedReturns a value indicating whether the component is focused.
public bool IsEnabled { get ; set ; }Gets or sets a value indicating whether the component is interactive (enabled).
public bool IsUnderlined { get ; set ; }Returns a value indicating whether the component is underlined.
public string Text { get ; set ; }Gets or sets the text shown in the component.
public string Placeholder { get ; set ; }Gets or sets the placeholder text shown when the component is empty.
public bool IsInvalid { get ; set ; }Gets or sets a value indicating whether the component is currently in an invalid state.
public bool IsBusy { get ; set ; }Gets or sets a value indicating whether the component is waiting on the search it asked for. While set, a spinner stands where the clear button does and the box reports itself as busy to assistive technology; the box stays editable throughout. On a box with an OnCancel handler, a pointer on it swaps the spinner for the cancel button. It says nothing about Failed, which the caller takes down itself - usually with ClearFailure where it starts the next search.
public bool IsFailedReturns a value indicating whether the box is showing that its last search did not answer.
public TextWeight Weight { get ; set ; }Gets or sets the font weight of the component.
public TextAlign TextAlign { get ; set ; }Gets or sets the text alignment of the component.
public string Background { get ; set ; }Gets or sets the CSS background of the component.
Methods
| Name | Description |
|---|---|
| Clear | Empties the box, puts the caret in it and raises OnSearch with the empty query - what pressing the clear button does. |
| Render | Renders the component's root HTML element. |
| Attach | Attaches a handler to the component's value-changed event. |
| SetText | Sets the text of the component. |
| SetPlaceholder | Sets the placeholder of the component. |
| Disabled | Disables the component. |
| Busy | Shows (or hides) the spinner that says the box is waiting on its search - see IsBusy. |
| Failed | Says the last search did not answer: the box is outlined in the danger colour and carries a warning glyph where the spinner was, for millisecondsVisible (pass 0 to leave it up until something clears it, which is ClearFailure). Nothing else takes it down: it describes one search, so the caller that starts the next one is what says the last one stopped mattering. It says that the search itself failed, which is not the same as a query the user should fix; a box whose *contents* are wrong is IsInvalid, and that one stays until it is put right. |
| ClearFailure | Takes down the failure set by Failed now, rather than when it would have expired. |
| Progress | Shows a determinate progress bar along the bottom edge of the box, filled to percent (clamped to 0-100). The box stays editable: the bar is for data the box searches over still arriving, so the user can keep typing while it loads. Take it down with HideProgress. |
| ProgressIndeterminate | Shows an indeterminate progress bar sweeping along the bottom edge of the box, for a load whose size is not known. Take it down with HideProgress. |
| HideProgress | Takes down the progress set by Progress or ProgressIndeterminate. A bar kept up by ShowProgressWhile stays until its tasks end. |
| ShowProgressWhile | Shows an indeterminate progress bar until task ends, however it ends. Overlapping calls keep the bar up until the last of their tasks has ended; a progress set by hand takes precedence while set. |
| Underlined | Configures the component to underlined. |
| NotUnderlined | Removes the underline from the component. |
| SetIcon | Sets the icon of the component. |
| NoIcon | Removes / disables the icon on the component. |
| Focus | Moves keyboard focus to the component. |
| SearchAsYouType | Configures the search box to fire its callback after every keystroke (debounced) rather than only on submit. |
| OnSearch | Registers a callback invoked when the search event fires. |
| OnCancel | Registers what to do when the user calls off a search that is still running, and by registering it says a search can be called off: while IsBusy a pointer on the box then swaps the spinner for a cancel button, in the same place and at the same size, and pressing it raises this and nothing else. The box is not emptied and no search is raised - a handler that wants either does it itself (box.Clear() empties it and raises OnSearch with the empty query). Without a handler there is no cancel button and a busy box keeps its clear button, since nothing would be offered in its place. |
| SetKeyboardShortcut | Registers a global keyboard shortcut that focuses this SearchBox when pressed, and renders a visual chip showing the shortcut on the right side of the box. Modifier names are case-insensitive ("Ctrl", "Cmd", "Meta", "Alt", "Shift"). Example: SetKeyboardShortcut("Ctrl", "K"). |
| OnShortcut | What the shortcut set by SetKeyboardShortcut does instead of focusing the box - for a box that stands for a search happening somewhere else. Pass null to have it focus again. |
| Height | Gets or sets the CSS height of the component. |
| H | Shortcut for setting the height in pixels. |
public SearchBox Clear()Empties the box, puts the caret in it and raises OnSearch with the empty query - what pressing the clear button does.
public override HTMLElement Render()Renders the component's root HTML element.
public void Attach(ComponentEventHandler<SearchBox> handler)Attaches a handler to the component's value-changed event.
public SearchBox SetPlaceholder(string error)Sets the placeholder of the component.
public SearchBox Busy(bool value = true)Shows (or hides) the spinner that says the box is waiting on its search - see IsBusy.
public SearchBox Failed(int millisecondsVisible = 5000)Says the last search did not answer: the box is outlined in the danger colour and carries a warning glyph where the spinner was, for millisecondsVisible (pass 0 to leave it up until something clears it, which is ClearFailure). Nothing else takes it down: it describes one search, so the caller that starts the next one is what says the last one stopped mattering. It says that the search itself failed, which is not the same as a query the user should fix; a box whose *contents* are wrong is IsInvalid, and that one stays until it is put right.
public SearchBox ClearFailure()Takes down the failure set by Failed now, rather than when it would have expired.
| Overload | |
|---|---|
| Progress(float) | Shows a determinate progress bar along the bottom edge of the box, filled to percent (clamped to 0-100). The box stays editable: the bar is for data the box searches over still arriving, so the user can keep typing while it loads. Take it down with HideProgress. |
| Progress(int, int) | Shows a determinate progress bar at position of total - see Progress. |
Progress(float)
public SearchBox Progress(float percent)Shows a determinate progress bar along the bottom edge of the box, filled to percent (clamped to 0-100). The box stays editable: the bar is for data the box searches over still arriving, so the user can keep typing while it loads. Take it down with HideProgress.
Parameters
- percent float
public SearchBox ProgressIndeterminate()Shows an indeterminate progress bar sweeping along the bottom edge of the box, for a load whose size is not known. Take it down with HideProgress.
public SearchBox HideProgress()Takes down the progress set by Progress or ProgressIndeterminate. A bar kept up by ShowProgressWhile stays until its tasks end.
public SearchBox ShowProgressWhile(Task task, Action onEnded = null)Shows an indeterminate progress bar until task ends, however it ends. Overlapping calls keep the bar up until the last of their tasks has ended; a progress set by hand takes precedence while set.
public SearchBox NotUnderlined()Removes the underline from the component.
public SearchBox SearchAsYouType()Configures the search box to fire its callback after every keystroke (debounced) rather than only on submit.
public SearchBox OnSearch(SearchEventHandler onSearch)Registers a callback invoked when the search event fires.
public SearchBox OnCancel(SearchCancelledEventHandler onCancel)Registers what to do when the user calls off a search that is still running, and by registering it says a search can be called off: while IsBusy a pointer on the box then swaps the spinner for a cancel button, in the same place and at the same size, and pressing it raises this and nothing else. The box is not emptied and no search is raised - a handler that wants either does it itself (box.Clear() empties it and raises OnSearch with the empty query). Without a handler there is no cancel button and a busy box keeps its clear button, since nothing would be offered in its place.
public SearchBox SetKeyboardShortcut(params string[] keys)Registers a global keyboard shortcut that focuses this SearchBox when pressed, and renders a visual chip showing the shortcut on the right side of the box. Modifier names are case-insensitive ("Ctrl", "Cmd", "Meta", "Alt", "Shift"). Example: SetKeyboardShortcut("Ctrl", "K").
public SearchBox OnShortcut(Action onShortcut)What the shortcut set by SetKeyboardShortcut does instead of focusing the box - for a box that stands for a search happening somewhere else. Pass null to have it focus again.
public SearchBox Height(UnitSize unitSize)Gets or sets the CSS height of the component.
Samples
Basic SearchBox with Underline and Search-as-You-Type
The following sample demonstrates the creation of a SearchBox that is underlined, triggers search events automatically as the user types, and displays an alert with the search query.