Skip to content

AutoComplete

Rules

Basic Example

js
const rule = {
    type: "autoComplete",
    title: "Auto Complete",
    field: "auto",
    value: "xaboy",
    props: {
    	fetchSuggestions: function (queryString, cb) {
			cb([
				{value: queryString}, {value: queryString + queryString}
			]);
		}
	}
}

Props Configuration Examples

js
const rule = {
    type: "autoComplete",
    title: "Product Search",
    field: "product",
    props: {
        placeholder: "Enter product name",
        fetchSuggestions: function (queryString, cb) {
            // Call remote search API
            if (queryString) {
                searchProducts(queryString).then(res => {
                    cb(res.data.map(item => ({
                        value: item.name,
                        id: item.id
                    })));
                });
            } else {
                cb([]);
            }
        },
        clearable: true,
    },
}

Custom Suggestions

js
const rule = {
    type: "autoComplete",
    title: "User Search",
    field: "username",
    props: {
        placeholder: "Enter username",
        valueKey: "name",
        fetchSuggestions: function (queryString, cb) {
            cb([
                {name: queryString + '1', id: 1},
                {name: queryString + '2', id: 2},
            ]);
        },
    },
}

Debounce Delay

js
const rule = {
    type: "autoComplete",
    title: "Search Keyword",
    field: "keyword",
    props: {
        placeholder: "Enter keyword",
        debounce: 500,
        fetchSuggestions: function (queryString, cb) {
            // Execute search after 500ms delay
            cb([
                {value: queryString + 'Related Result 1'},
                {value: queryString + 'Related Result 2'},
            ]);
        },
    },
}

Events Examples

Listen to Selection Changes

js
const rule = {
    type: "autoComplete",
    title: "Product Search",
    field: "product",
    props: {
        placeholder: "Enter product name",
        fetchSuggestions: function (queryString, cb) {
            cb([
                {value: queryString + '1'},
                {value: queryString + '2'},
            ]);
        },
    },
    on: {
        select: (item) => {
            console.log('Selected suggestion:', item);
        },
        change: (value) => {
            console.log('Input value changed:', value);
        },
        blur: (event) => {
            console.log('Lost focus:', event);
        },
        focus: (event) => {
            console.log('Gained focus:', event);
        },
        clear: () => {
            console.log('Input cleared');
        },
    },
}

Fill Details After Selection

js
const rule = [
    {
        type: "autoComplete",
        title: "Product Search",
        field: "product",
        props: {
            placeholder: "Enter product name",
            fetchSuggestions: function (queryString, cb) {
                cb([
                    {value: 'iPhone 15', id: 1, price: 5999},
                    {value: 'MacBook Pro', id: 2, price: 12999},
                ]);
            },
        },
        inject: true,
        on: {
            select: ($inject, item) => {
                // After selecting product, automatically fill price
                if (item.price) {
                    $inject.api.setValue('price', item.price);
                }
            },
        },
    },
    {
        type: "input-number",
        field: "price",
        title: "Price",
        props: {
            disabled: true,
        },
    },
]

Complete configuration items: Element_AutoComplete

value :String

Props

Property NameDescriptionTypeDefault Value
placeholderPlaceholder textstring
clearableWhether it can be clearedbooleanfalse
disabledWhether AutoComplete component is disabledbooleanfalse
valueKeyKey name used for display in input suggestion objectstringvalue
debounceDebounce delay for getting input suggestions, in millisecondsnumber300
placementMenu popup positionenumbottom-start
fetchSuggestionsMethod for getting input suggestions, only when your input suggestion data resolves, return it by calling callback(data:[])Array / Function
triggerOnFocuswhether show suggestions when input focusbooleantrue
selectWhenUnmatchedIn case input has no matching suggestions, whether pressing Enter triggers select eventbooleanfalse
nameEquivalent to native input name attributestring
ariaLabelNative aria-label attributestring
hideLoadingWhether to hide loading icon when loading remotelybooleanfalse
popperClassClass name of dropdown liststring
teleportedWhether to insert dropdown list element under element pointed by append-tobooleantrue
appendToWhich DOM element the dropdown is mounted toCSSSelector / HTMLElement
highlightFirstItemWhether to highlight first item of remote search results by defaultbooleanfalse
fitInputWidthWhether dropdown width is the same as input boxbooleanfalse

Events

Event NameDetailsType
blurTriggered when selector input loses focusFunction
focusTriggered when selector input gains focusFunction
inputTriggered when Input value changesFunction
clearTriggered when clicking clear button generated by clearable attributeFunction
selectTriggered when clicking to select suggestionFunction
changeTriggered when Input value changesFunction

Slots

Slot NameDescriptionType
defaultCustom input suggestion contentobject
prefixInput header content-
suffixInput footer content-
prependInput prepend content, before prefix-
appendInput append content, after suffix-
loadingModify loading area content-

FormCreate is an open-source project released under the MIT License. Free for personal and commercial use.