TypeAheadComponent

open class TypeAheadComponent(valueStore: Store<String>?, items: Proposal) : Component<Input> , InputFormProperties, SeverityProperties, InputFieldProperties

This component class manages the configuration of a TypeAheadComponent and does the rendering.

The corresponding typeAhead function creates such a component. It offers the possibility to input some String and get some list of proposals to choose from. Internally this is achieved by adding some datalist to the input field (https://developer.mozilla.org/en-US/docs/Web/HTML/Element/datalist).

It is intentional that this component is built upon a built-in mechanism of HTML: Even if there is no way to style the choices list, it will work on any device out of the box, especially on mobiles, where the native selection methods differ extremely from the desktop browsers!

The proposals adapt dynamically to the current user input, so it is mandating to provide a function that gets the current string (it is called "draft" throughout this component) and returns a List of Strings as proposals. As this function should be easy to integrate with other fritz2's methodologies, the parameter and the return value are wrapped with a Flow. Have a look at Proposal for signature details.

The typical (and minimal) usage might look like this:

val proposals = listOf("Kotlin", "Scala", "Java", "OCaml", "Haskell").asProposals()
val choice = storeOf("")
typeAhead(value = choice, items = proposals) { }

For production UIs consider a really long static list of possibilities. For the above example it would be much better to use a selectField instead! Choose a typeAhead if the possibilities are too long for a select component that displays all possibilities at once, or if the possibilities are gathered from an external source, like a remote API.

The configuration deals basically with three big groups:

We focus on the latter ones in the examples:

// handle the events manually if separation of source and destination flow is needed:
val proposals = listOf("Kotlin", "Scala", "Java", "OCaml", "Clojure", "F#").asProposals()

val preselection = storeOf("Clojure")
val choice = storeOf("")
typeAhead(items = proposals) {
value(preselection.data)
events {
value handledBy choice.update
}
}

// enable to accept also none proposal input values:
// this mode resembles a web search: the user gets proposals, but can freely type and commit new Strings if
// no suggestion fits:
typeAhead(value = choice, items = proposals) {
strict(false) // `true` is default
}

// limit the proposal list: Show at maximum 5 first entries of the proposals list
typeAhead(value = choice, items = proposals) {
limit(5) // `20` is default
}

// set a threshold of minimum string size of a draft, so below this no proposals are generated
// this is especially useful if it is clear, that the smallest proposal is longer in size and there are
// so many results, a longer start value before searching improves the selectivity.
typeAhead(value = choice, items = proposals) {
draftThreshold(3) // start searching only after input of at least 3 chars; `1` is default
}

// react to typing speed or "cost" of a remote call: Change the timeout the component will wait with a new
// execution of the `items` function to fetch new proposals after last key stroke:
typeAhead(value = choice, items = /* some costly remote call */) {
debounce(1000) // wait one second in this case; `250` (ms) is default
}

See also

Parameters

valueStore

an instance of a StateStore for holding the state of the component

items

a function to create the valid proposals based upon the current State.draft value.

Constructors

TypeAheadComponent
Link copied to clipboard
js
fun TypeAheadComponent(valueStore: Store<String>?, items: Proposal)
an instance of a StateStore for holding the state of the component

Types

EventsContext
Link copied to clipboard
js
class EventsContext<String>(element: Tag<HTMLInputElement>, value: Flow<String>) : EventContext<HTMLInputElement>

Functions

enabled
Link copied to clipboard
js
open override fun enabled(value: Boolean)
open override fun enabled(value: Flow<Boolean>)
render
Link copied to clipboard
js
open override fun render(context: RenderContext, styling: BoxParams.() -> Unit, baseClass: StyleClass, id: String?, prefix: String): Input
Central method that should do the actual rendering of a component.
severity
Link copied to clipboard
js
open override fun severity(value: SeverityProperties.SeverityContext.() -> Severity)
Property to manage the severity value of the component.
severityClassOf
Link copied to clipboard
js
open override fun severityClassOf(severityStyle: SeverityStyles): Flow<StyleClass>
This function manages the task to map a value of the Severity enumeration to a corresponding style defined within the SeverityStyles interface.

Properties

debounce
Link copied to clipboard
js
val debounce: ComponentProperty<Long>
disabled
Link copied to clipboard
js
open override val disabled: DynamicComponentProperty<Boolean>
draftThreshold
Link copied to clipboard
js
val draftThreshold: ComponentProperty<Int>
events
Link copied to clipboard
js
val events: ComponentProperty<TypeAheadComponent.EventsContext<String>.() -> Unit>
limit
Link copied to clipboard
js
val limit: ComponentProperty<Int>
placeholder
Link copied to clipboard
js
open override val placeholder: DynamicComponentProperty<String>
readonly
Link copied to clipboard
js
open override val readonly: DynamicComponentProperty<Boolean>
severity
Link copied to clipboard
js
open override val severity: NullableDynamicComponentProperty<Severity?>
size
Link copied to clipboard
js
open override val size: ComponentProperty<FormSizesStyles.() -> Style<BasicParams>>
strict
Link copied to clipboard
js
val strict: ComponentProperty<Boolean>
value
Link copied to clipboard
js
val value: DynamicComponentProperty<String>
variant
Link copied to clipboard
js
open override val variant: ComponentProperty<InputFieldVariants.() -> Style<BasicParams>>