Skip to content

Search Field

A search input with a search icon at its trailing end, which a clear button replaces while it has text. Escape clears it, and clearing returns focus to the input.

Decision

When Use
Filtering a list the page holds in memory Search Field.
Feeding a Data Table's setSearch from a search that sits elsewhere on the page Search Field.
A Paginated Table Not the Search Field. It has its own search.
A Data Table or a Filtered List Its search option, which draws one.

Usage

import { createSearchField } from './configurationpage?name=myplugin_stolon.js';

var search = createSearchField({ placeholder: 'alice', onInput: function (value) { applyFilter(value); } });
container.appendChild(search.element);

Classes: stolon-search, stolon-search-clear. The handle is { element, input, getValue, setValue, setSize, focus, destroy }.

States and Modifiers

  • onInput is debounced by delay, 250 ms by default, and also runs after a clear. onSearch runs at once on Enter.
  • setValue does not call onInput.
  • The input is type="search" with the browser's own marks hidden. The search icon shows while it is empty, and the clear button takes its place while it has text.
  • The labels default to "Search" and "Clear". label and clearLabel replace them.
  • size is small, medium (default), or large, and sizes its fields on the size scale. setSize(size) changes it later.

Reference

Generated from the code by scripts/build-docs.mjs. Edit the code, not this section.

createSearchField

function createSearchField(options?: SearchFieldOptions): SearchFieldHandle

A search input with a search icon at its trailing end, which a clear button replaces while it has text. Escape clears it, and clearing returns focus to the input.

Option Type What it does
value string first text [optional]
placeholder string a valid example search [optional]
label string aria-label of the input (default 'Search')
clearLabel string title and aria-label of the clear button (default 'Clear')
delay number milliseconds to wait after typing before onInput runs (default 250)
size 'small' | 'medium' | 'large' 'small', 'medium', or 'large' for its fields (default 'medium')
onInput (value: string): void function(value) called after typing stops, and after a clear [optional]
onSearch (value: string): void function(value) called at once on Enter [optional]
Handle member Type
input HTMLInputElement
getValue (): string
setValue (text?: string): void
setSize (size?: 'small' | 'medium' | 'large'): void
focus (): void
destroy (): void
Class What it looks like
stolon-search Search input with a search icon at its trailing end, which a clear button replaces while it has text

Example

Example Code

<!-- title: Search Field -->
<div id="mount"></div>
<script type="module">
const base = await window.__baseReady;
const view = document.getElementById('view');
const mount = document.getElementById('mount');
const note = document.createElement('div');
note.className = 'stolon-status-text';
mount.appendChild(base.createSearchField({ placeholder: 'alice', onInput: (value) => { note.textContent = 'Searched "' + value + '"'; } }).element);
mount.appendChild(note);
</script>