Skip to main content

Select

A dropdown selector for choosing one or multiple values from a list.

Source: Re-exported from antd with VEF hook enhancements. Full documentation: Ant Design Select

When to Use

  • The content of the options is complex (e.g. with icons or descriptions).
  • The number of options is large and needs filtering.
  • Use Radio when there are fewer than 5 options.

Basic Usage

import { Select } from '@vef-framework-react/components';

const options = [
{ value: 'jack', label: 'Jack' },
{ value: 'lucy', label: 'Lucy' },
{ value: 'tom', label: 'Tom' },
];

export default function Demo() {
return (
<Select
style={{ width: 200 }}
placeholder="Select a person"
options={options}
onChange={(value) => console.log(value)}
/>
);
}

Multiple Selection

import { Select } from '@vef-framework-react/components';

export default function Demo() {
return (
<Select
mode="multiple"
style={{ width: '100%' }}
placeholder="Select multiple"
options={[
{ value: 'a', label: 'Option A' },
{ value: 'b', label: 'Option B' },
{ value: 'c', label: 'Option C' },
]}
/>
);
}

Searchable

import { Select } from '@vef-framework-react/components';

export default function Demo() {
return (
<Select
showSearch
style={{ width: 200 }}
placeholder="Search to select"
filterOption={(input, option) =>
(option?.label ?? '').toLowerCase().includes(input.toLowerCase())
}
options={[
{ value: '1', label: 'Beijing' },
{ value: '2', label: 'Shanghai' },
{ value: '3', label: 'Guangzhou' },
]}
/>
);
}

VEF Enhancement: useDataOptionsSelect

VEF provides useDataOptionsSelect to load options from an async data source with built-in loading state, pinyin search support, and caching.

import { Select, useDataOptionsSelect } from '@vef-framework-react/components';

async function fetchCities() {
const res = await fetch('/api/cities');
return res.json(); // returns DataOption[]
}

export default function Demo() {
const selectProps = useDataOptionsSelect({
queryKey: ['cities'],
queryFn: fetchCities,
filterable: true, // enables pinyin/text filtering
});

return <Select style={{ width: 200 }} {...selectProps} />;
}

useDataOptionsSelect Options

OptionTypeDefaultDescription
queryKeyQueryKeyrequiredTanStack Query cache key
queryFn() => Promise<TData[]>requiredAsync data fetcher
filterablebooleanfalseEnable client-side text/pinyin filtering
onFetch(data: TData[]) => voidCallback after data is fetched
select(data: TData) => DataOptionTransform raw data to DataOption
paramsTParamsQuery parameters (triggers refetch on change)

The hook returns SelectProps that can be spread directly onto <Select>.

API

PropTypeDefaultDescription
valuestring | string[]Selected value (controlled)
defaultValuestring | string[]Initial value (uncontrolled)
optionsSelectOption[]Option list
mode'multiple' | 'tags'Multi-select mode
placeholderstringPlaceholder text
disabledbooleanfalseDisable the select
loadingbooleanfalseShow loading state
showSearchbooleanfalseEnable search
allowClearbooleanfalseShow clear button
filterOptionboolean | functiontrueFilter function
size'large' | 'middle' | 'small''middle'Select size
status'error' | 'warning'Validation status
variant'outlined' | 'filled' | 'borderless''outlined'Visual variant
onChange(value, option) => voidChange handler
onSearch(value: string) => voidSearch handler
onClear() => voidClear handler

Best Practices

  • Use useDataOptionsSelect for remote data to avoid manual loading state management.
  • Set allowClear for optional fields.
  • For form usage, use form.Field.Select which integrates with the VEF form system.