2
0
mirror of https://github.com/tenrok/vue-select.git synced 2026-05-17 02:29:37 +03:00

Make sure selected value is an option after option changed and react to value property changes even if tracking value internally (#914)

* make sure selected tracked value is an option if possible

Before this case did not work correctly:

- Select was rendered with *no* options, but *with* a saved value
- Options were fetched by ajax and options prop was updated
- Reduce function if passed

What happens without this commit is that the selected tracked value
simply was the raw reduced value (previously saved). Which means that
displaying a label for example does not work if the label comes from the
unreduced option.

This commit makes sure that the internal tracked value is checked
against all options not only once the select is created but additionally
when options change.

* remove useless keys

- first key was always undefined
- second key was always the index which is not usefull at all since it
  changes based on the order

* add test for setting value after option changed

* correctly react to value property changes if tracking internally

fixes sagalbot#855, sagalbot#842

* add getOptionKey prop

* yarn upgrade doc

* add getOptionKey api doc and fix links

* yarn upgrade

* do not use key on slot

* fix label spec
This commit is contained in:
Markus
2019-09-13 21:18:04 +02:00
committed by Jeff Sagal
parent 8ef15a12d3
commit ebcdcc5c62
7 changed files with 3813 additions and 4036 deletions
+33 -1
View File
@@ -178,7 +178,7 @@ getOptionLabel: {
return console.warn(
`[vue-select warn]: Label key "option.${this.label}" does not` +
` exist in options object ${JSON.stringify(option)}.\n` +
'http://sagalbot.github.io/vue-select/#ex-labels'
'https://vue-select.org/api/props.html#getoptionlabel'
)
}
return option[this.label]
@@ -188,6 +188,38 @@ getOptionLabel: {
},
```
## getOptionKey
Callback to get an option key. If `option`
is an object and has an `id`, returns `option.id`
by default, otherwise tries to serialize `option`
to JSON.
The key must be unique for an option.
```js
getOptionKey: {
type: Function,
default(option) {
if (typeof option === 'object' && option.id) {
return option.id
} else {
try {
return JSON.stringify(option)
} catch(e) {
return console.warn(
`[vue-select warn]: Could not stringify option ` +
`to generate unique key. Please provide 'getOptionKey' prop ` +
`to return a unique key for each option.\n` +
'https://vue-select.org/api/props.html#getoptionkey'
)
return null
}
}
}
},
```
## onTab
Select the current value if `selectOnTab` is enabled