Skip to content

AutoComplete

Rules

Basic Example

js
const rule = {
    type: "autoComplete",
    title: "Auto Complete",
    field: "auto",
    value: "xaboy",
    inject: true,
    props: {}
}

Props Configuration Examples

Static Options

js
const rule = {
    type: "autoComplete",
    title: "Product Name",
    field: "product",
    value: "",
    props: {
        options: [
            "iPhone 15 Pro",
            "MacBook Pro",
            "iPad Air",
            "AirPods Pro",
        ],
        placeholder: "Enter or select product",
        clearable: true,
    }
}
js
const rule = {
    type: "autoComplete",
    title: "Search Products",
    field: "keyword",
    value: "",
    props: {
        options: [],
        placeholder: "Enter product name",
        clearable: true,
        loading: false,
    },
    inject: true,
    on: {
        'update:value': ($inject, value) => {
            if (value && value.length >= 2) {
                // Call search API
                searchProducts(value).then(res => {
                    $inject.api.updateRule('keyword', {
                        props: {
                            options: res.data.map(item => item.name)
                        }
                    });
                });
            }
        },
    },
}

Clear After Select

js
const rule = {
    type: "autoComplete",
    title: "Product Name",
    field: "product",
    value: "",
    props: {
        options: [
            "iPhone 15 Pro",
            "MacBook Pro",
        ],
        placeholder: "Enter or select product",
        clearAfterSelect: true,
    }
}

Events Examples

Handle Input and Selection

js
const rule = {
    type: "autoComplete",
    title: "Product Name",
    field: "product",
    value: "",
    props: {
        options: [
            "iPhone 15 Pro",
            "MacBook Pro",
        ],
        placeholder: "Enter or select product",
        clearable: true,
    },
    on: {
        'update:value': (value) => {
            console.log('Input value changed:', value);
        },
        select: (value) => {
            console.log('Selected option:', value);
        },
        blur: () => {
            console.log('Lost focus');
        },
        focus: () => {
            console.log('Gained focus');
        },
    },
}

Linkage Update After Selection

js
const rule = [
    {
        type: "autoComplete",
        title: "Product Name",
        field: "product",
        value: "",
        props: {
            options: [],
            placeholder: "Enter or select product",
        },
        inject: true,
        on: {
            select: ($inject, value) => {
                // Auto-fill product info from selection
                getProductInfo(value).then(res => {
                    $inject.api.setValue('price', res.data.price);
                    $inject.api.setValue('stock', res.data.stock);
                });
            },
        },
    },
    {
        type: "input-number",
        field: "price",
        title: "Price",
        props: {
            disabled: true,
        },
    },
    {
        type: "input-number",
        field: "stock",
        title: "Stock",
        props: {
            disabled: true,
        },
    },
]

Complete configuration items: naive-ui_AutoComplete

value :String

Props

NameTypeDefault ValueDescriptionVersion
blur-after-selectbooleanfalseWhether to blur after selection
clear-after-selectbooleanfalseWhether to clear after selection
clearablebooleanfalseWhether AutoComplete supports clearing
disabledbooleanfalseWhether AutoComplete is disabled
get-show(value: string) => booleanundefinedDetermine whether to show menu based on input value in focused state
input-propsHTMLInputAttributesundefinedProperties of input element in AutoComplete
loadingbooleanfalseWhether to show loading state
optionsArray<string | AutoCompleteOption | AutoCompleteGroupOption>[]Custom options for AutoComplete
placeholderstring'请输入'Hint message for AutoComplete
placement'top-start' | 'top' | 'top-end' | 'right-start' | 'right' | 'right-end' | 'bottom-start' | 'bottom' | 'bottom-end' | 'left-start' | 'left' | 'left-end' | 'bottom-start'Popup position of AutoComplete2.25.0
render-label(option: SelectOption | SelectGroupOption, selected: boolean) => VNodeChildundefinedOption label render function2.24.0
render-option(info: { node: VNode, option: SelectOption | SelectGroupOption, selected: boolean }) => VNodeChildundefinedOption render function2.24.0
size'small' | 'medium' | 'large''medium'Size of AutoComplete
on-blur(event: FocusEvent) => voidundefinedCallback function triggered on blur
on-focus(event: FocusEvent) => voidundefinedCallback function triggered on focus
on-select(value: string) => voidundefinedCallback function triggered on select
on-update:value(value: string | null) => voidundefinedCallback function triggered when controlled data updates

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