Skip to content

Auto Complete

🌏 Translated with the assistance of DeepSeek and ChatGPT

A text input field with suggestions.

Basic Usage

Pass options as the list of choices. Provide modelValue for controlled mode.
Omit it or set it to undefined for uncontrolled mode; in this case you can supply a defaultValue as the initial value.

<template>
	<px-space direction="vertical">
		<px-auto-complete
			placeholder="Please input"
			v-model="input"
			:options="options"
		></px-auto-complete>
		<px-auto-complete
			placeholder="Please input"
			:options="options"
			default-value="test"
		></px-auto-complete>
	</px-space>
</template>
<script setup lang="ts">
import { ref } from 'vue'

const input = ref('')

const options = ref<string[]>([
	'pear',
	'plum',
	'cherry',
	'blueberry',
	'raspberry',
	'blackberry',
	'lemon',
	'lime',
	'pomegranate',
	'apricot'
])
</script>

<style lang="css" scoped>
.px-auto-complete {
	width: 320px;
}
</style>

Disabled Options

Inside options, set disabled: true on any choice to disable it.

<template>
	<px-auto-complete
		placeholder="Please input"
		v-model="input"
		:options="options"
	></px-auto-complete>
</template>
<script setup lang="ts">
import { ref } from 'vue'

const input = ref('')

const options = ref(
	[
		'apple',
		'banana',
		'orange',
		'grape',
		'strawberry',
		'kiwi',
		'mango',
		'pineapple',
		'watermelon',
		'peach'
	].map((item, index) => {
		return {
			label: item,
			value: item,
			disabled: (index & 1) === 1
		}
	})
)
</script>

<style lang="css" scoped>
.px-auto-complete {
	width: 320px;
}
</style>

Grouping

options also accepts option groups.

<template>
	<px-auto-complete
		placeholder="Please input"
		v-model="input"
		:options="options"
	></px-auto-complete>
</template>
<script setup lang="ts">
import { ref } from 'vue'

const input = ref('')
const options = ref([
	{
		type: 'group',
		label: 'tropical fruits',
		children: [
			{ label: 'mango', value: 'mango' },
			{ label: 'pineapple', value: 'pineapple' },
			{ label: 'papaya', value: 'papaya' },
			{ label: 'dragon fruit', value: 'dragon fruit' },
			{ label: 'durian', value: 'durian' },
			{ label: 'lychee', value: 'lychee' },
			{ label: 'longan', value: 'longan' }
		]
	},
	{
		type: 'group',
		label: 'citrus fruits',
		children: [
			{ label: 'orange', value: 'orange' },
			{ label: 'lemon', value: 'lemon' },
			{ label: 'lime', value: 'lime' },
			{ label: 'grapefruit', value: 'grapefruit' },
			{ label: 'tangerine', value: 'tangerine' }
		]
	}
])
</script>

<style lang="css" scoped>
.px-auto-complete {
	width: 320px;
}
</style>

Remote Loading

Set the loading and showPopoverEmpty props and listen to the input event to reflect remote-loading UI states.

<template>
	<px-auto-complete
		placeholder="Please input"
		v-model="input"
		:options="options"
		:loading="loading"
		append
		@input="inputHandler"
	></px-auto-complete>
</template>
<script setup lang="ts">
import { ref } from 'vue'

const input = ref('')
const loading = ref(false)

const options = ref<string[]>([])

const data = [
	'pear',
	'plum',
	'cherry',
	'blueberry',
	'raspberry',
	'blackberry',
	'lemon',
	'lime',
	'pomegranate',
	'apricot'
]

const inputHandler = () => {
	options.value = []
	loading.value = true
	setTimeout(() => {
		options.value = data
		loading.value = false
	}, 6000)
}
</script>

<style lang="css" scoped>
.px-auto-complete {
	width: 320px;
}
</style>

Append Mode

After selecting an option, append it to the existing input instead of replacing the content by enabling the append prop.
Use the filter and shouldShowPopover functions to control which options appear and when the popover is shown.

<template>
	<px-auto-complete
		placeholder="Please input"
		v-model="input"
		:options="options"
		:filter="filter"
		:shouldShowPopover="shouldShowPopover"
		append
	></px-auto-complete>
</template>
<script setup lang="ts">
import { ref } from 'vue'

const input = ref('')

const options = ref<string[]>(['gmail.com', '163.com', 'qq.com'])

const filter = (_: string, options: string[]) => {
	return options
}
const shouldShowPopover = (value: string) => {
	return value.endsWith('@')
}
</script>

<style lang="css" scoped>
.px-auto-complete {
	width: 320px;
}
</style>

Custom Rendering

option slot customizes option rendering, group-label slot customizes group label rendering.

<template>
	<px-auto-complete placeholder="Please input" v-model="input" :options="options">
		<template #group-label="{ option }">
			{{ option.label }}
			<div
				style="margin-left: 12px; color: red; font-size: 16px"
				v-if="option.label === 'citrus fruits'"
			>
				HOT!
			</div>
		</template>
		<template #option="{ option }">
			{{ option.label }}
			<px-tag style="margin-left: 12px" v-if="option.value === 'orange'">NEW!</px-tag>
		</template>
	</px-auto-complete>
</template>
<script setup lang="ts">
import { ref } from 'vue'

const input = ref('')
const options = ref([
	{
		type: 'group',
		label: 'citrus fruits',
		children: [
			{ label: 'orange', value: 'orange' },
			{ label: 'lemon', value: 'lemon' },
			{ label: 'lime', value: 'lime' },
			{ label: 'grapefruit', value: 'grapefruit' },
			{ label: 'tangerine', value: 'tangerine' }
		]
	},
	{
		type: 'group',
		label: 'tropical fruits',
		children: [
			{ label: 'mango', value: 'mango' },
			{ label: 'pineapple', value: 'pineapple' },
			{ label: 'papaya', value: 'papaya' },
			{ label: 'dragon fruit', value: 'dragon fruit' },
			{ label: 'durian', value: 'durian' },
			{ label: 'lychee', value: 'lychee' },
			{ label: 'longan', value: 'longan' }
		]
	}
])
</script>

<style lang="css" scoped>
.px-auto-complete {
	width: 320px;
}
</style>

More Options

This AutoComplete has most of Input component's features.

Disabled, Readonly, Loading & Clearable

Shape

Size

Slot

prefix
suffix

Composite

Status

Expose

<template>
	<px-space direction="vertical">
		<h4>Disabled, Readonly, Loading & Clearable</h4>
		<px-space>
			<px-auto-complete
				placeholder="Please input"
				disabled
				:options="options"
			></px-auto-complete>
			<px-auto-complete
				placeholder="Please input"
				readonly
				:options="options"
			></px-auto-complete>
			<px-auto-complete
				placeholder="Please input"
				loading
				:options="options"
			></px-auto-complete>
			<px-auto-complete
				placeholder="Please input"
				clearable
				:options="options"
			></px-auto-complete>
		</px-space>
		<h4>Shape</h4>
		<px-space>
			<px-auto-complete
				placeholder="Please input"
				shape="round"
				:options="options"
			></px-auto-complete>
			<px-auto-complete placeholder="Please input" :options="options"></px-auto-complete>
		</px-space>
		<h4>Size</h4>
		<px-space>
			<px-auto-complete
				placeholder="Please input"
				size="small"
				:options="options"
			></px-auto-complete>
			<px-auto-complete placeholder="Please input" :options="options"></px-auto-complete>
			<px-auto-complete
				placeholder="Please input"
				size="large"
				:options="options"
			></px-auto-complete>
		</px-space>
		<h4>Slot</h4>
		<px-space>
			<px-auto-complete placeholder="Please input" :options="options">
				<template #prefix>prefix</template>
				<template #suffix>suffix</template>
			</px-auto-complete>
			<px-auto-complete placeholder="Please input" :options="options">
				<template #prepend>
					<IconBolt></IconBolt>
				</template>
			</px-auto-complete>
			<px-auto-complete placeholder="Please input" :options="options">
				<template #append>
					<IconBolt></IconBolt>
				</template>
			</px-auto-complete>
		</px-space>
		<h4>Composite</h4>
		<px-space>
			<px-input-group>
				<px-input-group-label>
					<IconBolt></IconBolt>
				</px-input-group-label>
				<px-auto-complete placeholder="Please input" :options="options"> </px-auto-complete>
				<px-button>Confirm</px-button>
			</px-input-group>
		</px-space>
		<h4>Status</h4>
		<px-space>
			<px-auto-complete placeholder="Please input" :options="options"> </px-auto-complete>
			<px-auto-complete placeholder="Please input" :options="options" status="success">
			</px-auto-complete>
			<px-auto-complete placeholder="Please input" :options="options" status="warning">
			</px-auto-complete>
			<px-auto-complete placeholder="Please input" :options="options" status="error">
			</px-auto-complete>
		</px-space>
		<h4>Expose</h4>
		<px-space direction="vertical">
			<px-space>
				<px-button theme="info" @click="focusHandler">Focus</px-button>
				<px-button theme="info" @click="blurHandler">Blur</px-button>
				<px-button theme="info" @click="selectHandler">Select</px-button>
				<px-button theme="warning" @click="clearHandler">Clear</px-button>
			</px-space>
			<px-auto-complete
				placeholder="Please input"
				:options="options"
				ref="autoCompleteRef"
			></px-auto-complete>
		</px-space>
	</px-space>
</template>

<script setup lang="ts">
import { IconBolt } from '@pixelium/web-vue/icon-hn/es'
import { ref } from 'vue'
import { AutoComplete } from '@pixelium/web-vue'

// If on-demand import
// import { AutoComplete } from '@pixelium/web-vue/es'

const autoCompleteRef = ref<InstanceType<typeof AutoComplete>>(null)

const focusHandler = () => {
	autoCompleteRef.value?.focus()
}
const blurHandler = () => {
	autoCompleteRef.value?.blur()
}
const clearHandler = () => {
	autoCompleteRef.value?.clear()
}
const selectHandler = () => {
	autoCompleteRef.value?.select()
}

const options = ref<string[]>([
	'pear',
	'plum',
	'cherry',
	'blueberry',
	'raspberry',
	'blackberry',
	'lemon',
	'lime',
	'pomegranate',
	'apricot'
])
</script>

<style lang="css" scoped>
.px-auto-complete {
	width: 320px;
}
</style>

API

AutoCompleteProps

AttributeTypeOptionalDefaultDescriptionVersion
modelValuestring | nullTrueValue of the auto complete input (controlled mode), supports v-model.0.0.2
defaultValuestring | nullTrueDefault value of the auto complete input (uncontrolled mode).0.0.2
optionsstringTrueList of options.0.0.2
placeholderstringTruePlaceholder text.0.0.2
disabledbooleanTruefalseWhether the input is disabled.0.0.2
readonlybooleanTruefalseWhether the input is read-only.0.0.2
clearablebooleanTruefalseWhether to show a clear button.0.0.2
loadingbooleanTruefalseWhether to show a loading state.0.0.2
showPopoverEmptybooleanTruefalseWhether to display the popover when the options list is empty.0.0.2
shouldShowPopover(value: string, optionsFiltered: (string | AutoCompleteOption | AutoCompleteGroupOption)[]) => booleanTrueFunction to determine whether to show the popover while inputting.0.0.2
filter(keyword: string, options: (string | AutoCompleteOption | AutoCompleteGroupOption)[]) => (string | AutoCompleteOption | AutoCompleteGroupOption)[]TrueFunction to filter the options.0.0.2
appendbooleanTruefalseAppend mode.0.0.2
size'medium' | 'large' | 'small'True'medium'Size of the auto complete input.0.0.2
shape'default' | 'round'True'default'Shape of the auto complete input.0.0.2
borderRadiusNumberOrPercentage | NumberOrPercentage[]True0.0.2
status'success' | 'warning' | 'error' | 'normal'True'normal'Form validation status.0.0.2
autofocusbooleanTruefalseNative <input> autofocus attribute.0.0.2

AutoCompleteEvents

EventParameterDescriptionVersion
inputvalue: string, e: EventCallback fired when the input value changes.0.0.2
update:modelValuevalue: stringCallback fired when modelValue is updated.0.0.2
changevalue: string, e: Event | undefinedCallback fired when the input content changes.0.0.2
clearvalue: stringCallback fired when the clear button is clicked.0.0.2
blure: FocusEventCallback fired when the input loses focus.0.0.2
focuse: FocusEventCallback fired when the input receives focus.0.0.2
selectvalue: string, option: string | AutoCompleteOption, e: MouseEventCallback fired when an option is selected.0.0.2

AutoCompleteSlots

SlotParameterDescriptionVersion
prefixPrefix content.0.0.2
suffixSuffix content.0.0.2
optionoption: string | AutoCompleteOptionCustom option content.0.0.2
group-labeloption: AutoCompleteGroupOptionLabel for option groups.0.0.2

AutoCompleteExpose

AttributeTypeOptionalDefaultDescriptionVersion
focus() => voidFalseFocus the control.0.0.2
blur() => voidFalseBlur the control.0.0.2
clear() => voidFalseClear the current input.0.0.2
select() => voidFalseSelect all text in the input.0.0.2

AutoCompleteOption, AutoCompleteGroupOption

ts
export interface Option<T = any> {
	value: T
	label: string
}

export interface GroupOption<T = any> {
	children: (Option<T> | string)[]
	type: typeof GROUP_OPTION_TYPE
}

export interface AutoCompleteOption extends Option<string> {
	value: string
	disabled?: boolean
}

export interface AutoCompleteGroupOption extends GroupOption {
	label: string
	key: string | number | symbol
	children: (AutoCompleteOption | string)[]
}