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:
@@ -21,7 +21,7 @@ describe("Labels", () => {
|
||||
Select.vm.open = true;
|
||||
expect(spy).toHaveBeenCalledWith(
|
||||
'[vue-select warn]: Label key "option.label" does not exist in options object {}.' +
|
||||
"\nhttp://sagalbot.github.io/vue-select/#ex-labels"
|
||||
"\nhttps://vue-select.org/api/props.html#getoptionlabel"
|
||||
);
|
||||
});
|
||||
|
||||
|
||||
@@ -23,4 +23,13 @@ describe("Reset on options change", () => {
|
||||
Select.setProps({options: ["four", "five", "six"]});
|
||||
expect(Select.vm.selectedValue).toEqual([]);
|
||||
});
|
||||
|
||||
it("should return correct selected value when the options property changes and a new option matches", () => {
|
||||
const Select = shallowMount(VueSelect, {
|
||||
propsData: { value: "one", options: [], reduce(option) { return option.value } }
|
||||
});
|
||||
|
||||
Select.setProps({options: [{ label: "oneLabel", value: "one" }]});
|
||||
expect(Select.vm.selectedValue).toEqual([{ label: "oneLabel", value: "one" }]);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -182,4 +182,19 @@ describe("When reduce prop is defined", () => {
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
it("reacts correctly when value propery changes", () => {
|
||||
const optionToChangeTo = { id: 1, label: "Foo" };
|
||||
const Select = shallowMount(VueSelect, {
|
||||
propsData: {
|
||||
value: 2,
|
||||
reduce: option => option.id,
|
||||
options: [optionToChangeTo, { id: 2, label: "Bar" }]
|
||||
}
|
||||
});
|
||||
|
||||
Select.setProps({ value: optionToChangeTo.id });
|
||||
|
||||
expect(Select.vm.selectedValue).toEqual([optionToChangeTo]);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user