Skip to content

AutoComplete

Rules

Basic Example

js
const rule = {
    type: "autoComplete",
    title: "Auto-Complete",
    field: "auto",
    value: "xaboy",
    inject: true,
    props: {
    	search: function (inject, value) {
            inject.self.props.options = !value ? [] : [
                {value: value}, 
                {value: value + value}, 
                {value: value + value + value}
            ];
        }
	}
}

Props Configuration Examples

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

Custom Options

js
const rule = {
    type: "autoComplete",
    title: "User Search",
    field: "username",
    inject: true,
    props: {
        placeholder: "Enter username",
        search: function (inject, value) {
            inject.self.props.options = [
                {value: value + '1', label: value + '1'},
                {value: value + '2', label: value + '2'},
            ];
        }
    },
}

Events Examples

Listen to Selection Changes

js
const rule = {
    type: "autoComplete",
    title: "Product Search",
    field: "product",
    inject: true,
    props: {
        placeholder: "Enter product name",
        search: function (inject, value) {
            inject.self.props.options = [
                {value: value + '1'},
                {value: value + '2'},
            ];
        },
    },
    on: {
        select: (value, option) => {
            console.log('Selected suggestion:', value, option);
        },
        change: (value) => {
            console.log('Input value changed:', value);
        },
        blur: () => {
            console.log('Component blurred');
        },
        focus: () => {
            console.log('Component focused');
        },
        search: (value) => {
            console.log('Search:', value);
        },
    },
}

Fill Details After Selection

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

Complete configuration items: Ant-design-vue_AutoComplete

value :String

Props

ParameterDescriptionTypeDefault
allowClearSupport clear, effective in single selection modebooleanfalse
autofocusAuto focusbooleanfalse
backfillWhen using keyboard to select options, fill the selected item back into the input boxbooleanfalse
borderedWhether to have borderbooleantrue
clearIconCustom clear button using slotslot<CloseCircleFilled />
default (custom input)Custom input boxslot<Input />
defaultActiveFirstOptionWhether to highlight the first option by defaultbooleantrue
defaultOpenWhether to expand dropdown menu by defaultboolean-
disabledWhether disabledbooleanfalse
popupClassNameclassName attribute of dropdown menustring-
dropdownMatchSelectWidthDropdown menu and selector have the same width. By default, min-width will be set, which will be ignored when the value is less than the selector width. false will disable virtual scrollingboolean | numbertrue
dropdownMenuStyleCustom style for dropdown menuobject
filterOptionWhether to filter based on input items. When it is a function, it will receive inputValue and option as parameters. When option meets the filter condition, it should return true, otherwise return false.boolean or function(inputValue, option)true
openWhether to expand dropdown menuboolean-
optionCustom node through option slotv-slot:option="{value, label, [disabled, key, title]}"-
optionsData source for auto-completeDataSourceItemType[]
placeholderInput box hintstring | slot-
statusSet validation status'error' | 'warning'-

Events

Event NameDescriptionCallback Parameters
blurCallback when losing focusfunction()
changeCallback when option is selected or input value changesfunction(value)
dropdownVisibleChangeCallback when dropdown menu expandsfunction(open)
focusCallback when gaining focusfunction()
searchCalled when searching for completion itemsfunction(value)
selectCalled when selected, parameter is the selected item's valuefunction(value, option)
clearCallback when clearing contentfunction

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