Skip to main content
5.1.0Next5.1.05.0.35.0.25.0.15.0.04.0.94.0.84.0.73.2.03.1.03.0.13.0.02.1.12.0.101.0.0
Version: 5.1.0

AutoComplete

Auto-complete is an input field that helps the user with suggestions as they are typing their input. When a user starts typing, a list of selectable suggestions will appear, making it easier to get the input right and to understand the options.

Basic Usage

<triton-auto-complete
options="['any','options']"
></triton-auto-complete>
<TritonAutoComplete 
options="['any','options']"
/>
<html> 
<head>
<script type="module">
import { defineCustomElements } from '~node_modules/@kognifai/triton-cl-core/loader/index.es2017.js';
defineCustomElements();
</script>
</head>
<body>
<triton-auto-complete
options="['any','options']"
></triton-auto-complete>
</body>
</html>

TritonAutoComplete is an extension of the TritonInput Component, and inherits the input properties. This includes properties such as icons, required, label etc.

Behavior

When the user inputs characters into the AutoComplete, different options will appear. TritonAutoComplete has two different ways of creating the options, but both will look the same to the user.

AutoCompleteOption

The most common way to add options to TritonAutoComplete is by using the AutoCompleteOption type.

In TypeScript, AutoCompleteOptions are defined like this:

type AutoCompleteOption = {
key: string;
value: string | number;
label?: string;
options?: {
[args: string]: any;
}
}
<triton-auto-complete
label="Select airport using AutoCompleteOptions"
placeholder="Type airport in Norway"
options={[
{
key: 'OSL',
value: 'Oslo Lufthavn Gardemoen'
},
...
]}
></triton-auto-complete>

Array of Strings

When using TritonAutoComplete with vanilla JS, or a framework that does not support rich data as properties, options should be passed as a string array. This is simply done by adding an array of strings to the options property.

<triton-auto-complete 
label="Select country as string"
placeholder="No country selected"
options='["Afghanistan","Albania", ... ,"Zambia","Zimbabwe" ]'
></triton-auto-complete>

Trigger After

This property sets after which amount of input characters the search fires. Note that the default for this is 1 (meaning that the search will fire when the 2nd character is entered).

<triton-auto-complete placeholder="Search will fire immediately" options="['any','options']" trigger-after="0"></triton-auto-complete>
<triton-auto-complete placeholder="Search will fire on the 3rd character" options="[''some','other', 'options']" trigger-after="2"></triton-auto-complete>

Custom Search Function

In some cases implementers will want to have a custom callback function to handle the search, instead of the default handling. This can be acchieved by using the searchOptionsCallback property. The passed callback function will override the default options handling. The function passed should return the list of options to display.

Note: this is an advanced feature

<triton-auto-complete searchOptionsCallback="customSearchCallback"></triton-auto-complete>
const customSearchCallback: AutoCompleteOptions = (searchTxt: string) => {
/* Your custom search logic here */
}

Playground

API

Properties

PropertyDescriptionTypeDefault
clear-inputIf true, and the input has a value, a clear-button will appear. Clicking it clears the input fieldbooleantrue
clear-on-editIf true, the value will be cleared when the input gets focus. Defaults to true when type is password, otherwise, defaults to falseboolean-
disabledIf true, the component will look disabled and not be interactablebooleanfalse
errorIf true the input element will be displayed as invalidbooleanfalse
error-textError text displayed at the bottom of the field, if error is truestring-
force-selectionIf true and input value is not valid, the best match will be selected on blur if there exist some one or more matches in the visible listbooleanfalse
helper-textA brief text for the user, at the bottom of the fieldstring-
icon-trailingDisplay icon on the right side of the inputIcon'search'
is-loadingShould be set to true if the component is loadingbooleanfalse
labelThe label displayed for the fieldstring-
nameThe name of the control, which is submitted with the form datastring-
no-results-labelThe string displayed if the search returns no optionsstringNoresults
optionsArray of options to populate the auto copmlete result listAutoCompleteOption[] string string[]-
placeholderInstructional text that shows before the input has a valuestring-
requiredIf true, the user must fill in a value before submitting a formbooleanfalse
result-limitMax allowed items in the auto complete result listnumber8
searchOptionsCallbackIf callback function is set the default internal filtering of options will NOT happen.The callback function will be executed upon triggering of onInputChange eventand should return options that will be displayed(searchTerm:string)=>Promise<string string[] AutoCompleteOption[]>-
trigger-afterAutocomplete will not trigger until more than the given amount of characters have been entered in the fieldnumber1
valueThe value of the inputstring''

Events

NameTypeDescriptionExtra
atBlurCustomEvent<void>Emitted when the input loses focus.-
atChangeCustomEvent<InputValue>Emitted when the value has changed.-
atFocusCustomEvent<void>Emitted when the input has focus.-
atInputCustomEvent<KeyboardEvent>Emitted when a keyboard input occurred-