|
1 | 1 | ---
|
2 |
| -title: 'Dropdown Options' |
| 2 | +title: 'Options' |
3 | 3 | ---
|
4 | 4 |
|
5 |
| -# Dropdown Options |
| 5 | +# Options |
6 | 6 |
|
7 |
| -## Default format |
| 7 | +The `options` prop is the core configuration for populating the dropdown menu. Understanding how to structure your options is essential for effective component usage. |
8 | 8 |
|
9 |
| -When using the `options` prop, you can pass an array of objects to the component. Each object should have a `label` and a `value` property. The `label` property is used to display the option in the dropdown, and the `value` property is the value that will be set to the `v-model` when the option is selected. |
| 9 | +## Basic structure |
| 10 | + |
| 11 | +Each option requires two key properties: |
10 | 12 |
|
11 | 13 | ```vue
|
12 | 14 | <template>
|
13 |
| - <VueSelect :options="[{ label: 'Option #1', value: 'option_1' }]" /> |
| 15 | + <VueSelect |
| 16 | + :options="[ |
| 17 | + { label: 'JavaScript', value: 'js' }, |
| 18 | + { label: 'TypeScript', value: 'ts' } |
| 19 | + ]" |
| 20 | + /> |
14 | 21 | </template>
|
15 | 22 | ```
|
16 | 23 |
|
| 24 | +**Properties:** |
| 25 | + |
| 26 | +- `label`: Text displayed in the dropdown menu |
| 27 | +- `value`: Data bound to `v-model` when the option is selected |
| 28 | + |
17 | 29 | ::: info
|
18 |
| -If you are using TypeScript, make sure to read the [TypeScript usage](/typescript) section to leverage proper type-safety. |
19 |
| -::: |
| 30 | +For TypeScript users, import the `Option` type to ensure proper type checking: |
| 31 | + |
| 32 | +```ts |
| 33 | +import type { Option } from "vue3-select-component"; |
20 | 34 |
|
21 |
| -## Disabled option(s) |
| 35 | +const options: Option<string>[] = [{ label: 'JavaScript', value: 'js' }]; |
| 36 | +``` |
| 37 | +::: |
22 | 38 |
|
23 |
| -You can disable an option by adding a `disabled` property to the object. The `disabled` property should be a boolean. |
| 39 | +## Disabling options |
24 | 40 |
|
25 |
| -When an option is disabled, it cannot be selected nor focused/navigated to using the keyboard. |
| 41 | +Individual options can be disabled by adding the `disabled` property: |
26 | 42 |
|
27 | 43 | ```vue
|
28 | 44 | <template>
|
29 |
| - <VueSelect :options="[{ label: 'Option #1', value: 'option_1', disabled: true }]" /> |
| 45 | + <VueSelect |
| 46 | + :options="[ |
| 47 | + { label: 'Available', value: 'a1' }, |
| 48 | + { label: 'Unavailable', value: 'a2', disabled: true } |
| 49 | + ]" |
| 50 | + /> |
30 | 51 | </template>
|
31 | 52 | ```
|
32 | 53 |
|
33 |
| -## Passing extra properties |
| 54 | +Disabled options: |
| 55 | + |
| 56 | +- Cannot be selected |
| 57 | +- Cannot be focused with keyboard navigation |
| 58 | +- Have distinct visual styling |
| 59 | +- Include proper ARIA attributes for accessibility |
34 | 60 |
|
35 |
| -You can pass extra properties to the options object. The component will ignore them but you will be able to manipulate those extra properties using some props and slots. |
| 61 | +## Extended Properties |
| 62 | + |
| 63 | +Options can include additional properties beyond the standard `label`/`value` pair: |
36 | 64 |
|
37 | 65 | ```vue
|
38 | 66 | <template>
|
39 | 67 | <VueSelect
|
40 |
| - :options="[{ label: 'Option #1', value: 'option_1', extra: 'Extra data' }]" |
41 |
| - :get-option-label="(option) => `${option.label} - ${option.extra}`" |
42 |
| - /> |
| 68 | + :options="[ |
| 69 | + { |
| 70 | + label: 'JavaScript', |
| 71 | + value: 'js', |
| 72 | + version: 'ES2022', |
| 73 | + creator: 'Brendan Eich' |
| 74 | + } |
| 75 | + ]" |
| 76 | + :get-option-label="option => `${option.label} (${option.version})`" |
| 77 | + > |
| 78 | + <template #option="{ option }"> |
| 79 | + {{ option.label }} - Created by {{ option.creator }} |
| 80 | + </template> |
| 81 | + </VueSelect> |
43 | 82 | </template>
|
44 | 83 | ```
|
45 | 84 |
|
46 | 85 | ::: info
|
47 |
| -If you are using TypeScript, make sure to read the [TypeScript usage](/typescript) section to leverage proper type-safety. |
| 86 | +When using extended properties with TypeScript, extend the `Option` type: |
| 87 | + |
| 88 | +```ts |
| 89 | +type LanguageOption = Option<string> & { |
| 90 | + version: string; |
| 91 | + creator: string; |
| 92 | +}; |
| 93 | +``` |
| 94 | + |
| 95 | +For more details, see the [Extending option properties guide](./typescript.md#extending-option-properties). |
48 | 96 | :::
|
0 commit comments