Skip to content

Commit

Permalink
fix: anx-select v-model bug
Browse files Browse the repository at this point in the history
automatically set default value with v-model when component is mounted
remove selectedIndex property, because the value should be set via
v-model
  • Loading branch information
pkrumplanx committed Aug 23, 2021
1 parent b3f4b00 commit 8b10d3f
Showing 1 changed file with 18 additions and 4 deletions.
22 changes: 18 additions & 4 deletions src/components/AnxSelect/AnxSelect.vue
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,6 @@ export default class AnxSelect extends Vue {
@Prop({ default: "anx-select" }) id!: string;
/** This is the label that will be displayed as description for the select input field */
@Prop({ default: "" }) label!: string;
/** This is the index that should be selected */
@Prop({ default: 0 }) selectedIndex!: number;
/**
* This are the options for the select input field.
* The properry should be an array of objcets with the following two properties: <br>
Expand All @@ -76,6 +74,8 @@ export default class AnxSelect extends Vue {
@Prop({ required: true }) options!: Array<{ value: string; text: string }>;
/** The width for the select field */
@Prop({ default: "100%" }) width!: string;
/** This is the value that can be used via **v-model** */
@Prop({ default: null }) value!: string;
/**
* If this property is set, a default required validation will be applied
*/
Expand All @@ -96,8 +96,8 @@ export default class AnxSelect extends Vue {
return "";
}
private selected = this.options[this.selectedIndex].value;
private selectedText = this.options[this.selectedIndex].text;
private selected = this.getOptionFromValue(this.value).value;
private selectedText = this.getOptionFromValue(this.value).text;
private show = false;
private error: string[] = [];
private dynamicHeight = false;
Expand Down Expand Up @@ -193,6 +193,20 @@ export default class AnxSelect extends Vue {
/** This emits the input event with the selected user input */
this.$emit("input", this.selected);
}
/**
* Returns the options object by providing a value
* If no options is found, the first options in the array is returned
*/
private getOptionFromValue(value: string): { value: string; text: string } {
for (let i = this.options.length - 1; i > 0; i--) {
if (this.options[i].value === value) {
return this.options[i];
}
}
return this.options[0];
}
}
</script>

Expand Down

0 comments on commit 8b10d3f

Please sign in to comment.