You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This isn't a common use-case. You should use the `label` and `value` properties of the option object when possible.
9
+
Doing this will break the type-safety of the component.
10
+
Read more about [`getOptionLabel` and `getOptionValue` props](../props.md).
11
+
:::
12
+
13
+
In the rare case you need to use different properties for the `label` and `value` of an option, you can use the `getOptionLabel` and `getOptionValue` props.
14
+
15
+
If you're using TypeScript, be sure to read the [type-safety guide for these props](../typescript.md#using-custom-label-value-with-options) section.
Copy file name to clipboardExpand all lines: docs/props.md
+13-7Lines changed: 13 additions & 7 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -182,7 +182,7 @@ Aria attributes to be passed to the select control to improve accessibility.
182
182
183
183
Callback function to determine if the current option should match the search query. This function is called for each option and should return a boolean.
184
184
185
-
The `label` is provided as a convenience, using `getOptionLabel` or `getMultiValueLabel` depending on the `isMulti` prop.
185
+
The `label` is provided as a convenience, processed from `getOptionLabel` prop.
186
186
187
187
::: info
188
188
By default, the following callback function is used `(option, label, search) => label.toLowerCase().includes(search.toLowerCase())`
@@ -202,11 +202,13 @@ By default, the following callback function is used `(option, label, search) =>
202
202
(option) =>option.label;
203
203
```
204
204
205
-
A function to get the label of an option. This is useful when you want to use a property different from `label` as the label of the option.
205
+
Resolves option data to a string to render the option label.
206
206
207
-
This function is used to display the options in the dropdown, and to display the selected option (**single-value**) in the select.
207
+
This function can be used if you don't want to use the standard `option.label` as the label of the option.
208
208
209
-
## getMultiValueLabel
209
+
The label of an option is displayed in the dropdown and as the selected option (**single-value**) in the select.
210
+
211
+
## getOptionValue
210
212
211
213
**Type**:
212
214
@@ -217,9 +219,13 @@ This function is used to display the options in the dropdown, and to display the
217
219
**Default**:
218
220
219
221
```ts
220
-
(option) =>option.label;
222
+
(option) =>option.value;
221
223
```
222
224
223
-
A function to get the label of an option. This is useful when you want to use a property different from `label` as the label of the option.
225
+
Resolves option data to a string to compare options and specify value attributes.
226
+
227
+
This function can be used if you don't want to use the standard `option.value` as the value of the option.
224
228
225
-
This function is used to display the selected options (**multi-value**) in the select.
Copy file name to clipboardExpand all lines: docs/typescript.md
+57-13Lines changed: 57 additions & 13 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -6,13 +6,13 @@ title: 'TypeScript'
6
6
7
7
In order to provide flexibility with TypeScript, Vue 3 Select Component has been written in TypeScript. This means that you can take advantage of TypeScript's type checking and autocompletion features.
8
8
9
-
## About generics in TypeScript & Vue
9
+
## Generics with Vue & TypeScript
10
10
11
11
Vue 3 Select Component uses a feature that has been released on Vue 3.3 called [**Generics**](https://vuejs.org/api/sfc-script-setup.html#generics).
12
12
13
13
Generics allow you to define a type that can be used in multiple places with different types. This is useful when you want to create a component that can be used with different types of data.
14
14
15
-
A common type you'll see is the `Option` type, which is used to define the optionsof the select component.
15
+
A common type taking use of the Vue Generic is the `Option` type, which is used to define the `:options` prop of the select component:
16
16
17
17
```ts
18
18
typeOption<T> = {
@@ -22,18 +22,21 @@ type Option<T> = {
22
22
};
23
23
```
24
24
25
-
## Custom optionvalue
25
+
## Customizing `option.value` type
26
26
27
27
::: info
28
28
Ensure you are familiar with the [`:options` prop](/props#options) before reading this section.
29
29
:::
30
30
31
-
By default, the `value` property of the option object is a `string`. However, it is possible to use a custom type, such as a `number` or a complex object.
31
+
By default, the `value` property of the option object is a `string`. However, it is possible to use a different type, such as a `number`.
32
+
33
+
To do this, import the `Option` type from the component and define a custom type that extends the `Option` type with a generic type:
32
34
33
35
```vue
34
36
<script setup lang="ts">
37
+
import type { Option } from "vue3-select-component";
35
38
import { ref } from "vue";
36
-
import VueSelect, { type Option } from "vue3-select-component";
39
+
import VueSelect from "vue3-select-component";
37
40
38
41
// Define a custom type for the option value.
39
42
// It takes a generic type that defines the type of the `value` property.
`getOptionValue` and `getOptionLabel` props are not compatible with the type-safety of the component. Therefore, you should use them with caution and only as a last resort.
149
+
:::
150
+
151
+
If you're using the `getOptionValue` or `getOptionLabel` props, there are a few gotchas to be aware of with the types:
152
+
153
+
- Local array of options cannot be typed as `Option<T>[]`
154
+
- When passing the array of options to the component, you need to cast it to `unknown` then `Option<T>[]`.
155
+
156
+
Here's an example usage of the `getOptionValue` and `getOptionLabel` props with TypeScript:
157
+
158
+
```vue
159
+
<script setup lang="ts">
160
+
import type { Option } from "vue3-select-component";
161
+
162
+
const activeRole = ref<string>("");
163
+
164
+
// You cannot type the `roleOptions` as `Option<string>[]`.
165
+
const roleOptions = [
166
+
{ id: "Admin", key: "admin" },
167
+
{ id: "User", key: "user" },
168
+
{ id: "Guest", key: "guest" },
169
+
];
170
+
</script>
171
+
172
+
<template>
173
+
<!-- Casting of the `roleOptions` must be done at the `:options` prop-level. -->
174
+
<VueSelect
175
+
v-model="activeRole"
176
+
:options="(roleOptions as unknown as Option<string>[])"
177
+
:is-multi="false"
178
+
:get-option-label="option => (option.id as string)"
179
+
:get-option-value="option => (option.key as string)"
0 commit comments