SearchField extends KompElement

An input with debounced search and results display via floater or inline list. Provide a search function that returns results, and optionally a renderResult function to customize how each result is displayed.

Example

JS
new SearchField({
    placeholder: 'Search...',
    search: async (query) => {
        const response = await fetch(`/api/search?q=${query}`);
        return response.json();
    },
    result: (item) => item.name,
    select: (item, query) => console.log(item)
})
HTML
<komp-search-field placeholder="Search..."></komp-search-field>

Constructor

new SearchField(options)
options : Object optional
search : function optional

performs the search and returns results. Receives (query:string), returns Promise<Array>

select : function optional

what to do with result, return false to prevent default behavior. Receives (result, query)

result : function optional

renders a single result label/content. Receives (result)

placeholder : string optional

placeholder text for the input

inline : boolean optional

if true, renders results inline instead of in a floater

minLength : number optional

minimum query length before searching

debounce : number optional

milliseconds to debounce search input

empty : string | HTMLElement | function | false optional

content shown when no results found. Set to false to disable.

input : function | HTMLInputElement optional

custom input element or function that returns one

floater : Object optional

options passed to Floater for the results floater

content : string | HTMLElement | Array | Object optional

content to append to element. Passed to Dolla's content

Instance Properties

Attributes settable via constructor options

Methods overridable via constructor options

Methods to auto-bind to this

Event names that can be bound via onEventName constructor options

CSS injected once per component via adoptedStyleSheets

Attributes to observe for changes. Triggers changed(attribute, was, now) and [attribute]Changed(was, now) callbacks.

Instance Methods

Listen for events on another element, automatically cleaned up when this component disconnects

Parameters

element : HTMLElement

element to listen on

eventType : string

event type

args : *

additional arguments passed to addEventListener

Called every time an observed attribute changes. Attribute must be listed in static watch.

Parameters

attribute : string

the attribute that changed

was : *

previous value

now : *

new value

Called when element is connected to the DOM

Called when element is disconnected from the DOM

Called once per instantiation, but only after element is connected to the DOM

Remove element. Fires beforeRemove, calls optional callback, removes from DOM, then fires afterRemove.

Parameters

callback : function optional

async callback called between beforeRemove and afterRemove

Returns

Render a single result item as a button. Assignable via constructor options.

Parameters

result : *

a single result from the search

Returns

HTMLButtonElement

Render the full results list, including empty state. Assignable via constructor options.

Parameters

results : Array

array of search results

Returns

Array | *

content to render

Trigger an event on this element

Parameters

eventName : string

event name to trigger

args : *

additional arguments

Events

Fired after the element is connected to the DOM and initialized

Fired after the element is disconnected from the DOM

Fired after the element is removed

Fired before the element is connected to the DOM

Fired before the element is disconnected from the DOM

Fired before the element is removed

Fired when results are rendered

Properties

results : Array

the search results

query : string

the search query

Fired when a result is selected

Properties

result : *

the selected result

query : string

the search query

event : Event

the originating event