| <template> |
| <span> |
| <div class="input-group"> |
| <select class="form-select form-select-sm" :id="id" :disabled="disabled" @input="handleInput" @change="handleInput" :value="modelValue"> |
| |
| <option v-for="opt in optionsToUse" :value="opt.val" v-bind:key="opt.val" :disabled="opt.disabled === true"> |
| {{ opt.description }} |
| </option> |
| </select> |
| </div> |
| <FormElementDescription :description="description"></FormElementDescription> |
| </span> |
| </template> |
| |
| <script lang="ts"> |
| import { randomID } from "@/Core/Utils/MiscUtils"; |
| import { Options, Vue } from "vue-class-component"; |
| import { Prop, Watch } from "vue-property-decorator"; |
| import { IUserArgOption } from "./FormFull/FormFullInterfaces"; |
| import FormElementDescription from "./FormElementDescription.vue"; |
| import { slugify } from "@/Core/Utils/StringUtils"; |
| |
| |
| |
| |
| @Options({ |
| components: { |
| FormElementDescription, |
| }, |
| }) |
| export default class FormSelect extends Vue { |
| @Prop({ required: true }) modelValue!: string; |
| @Prop({ default: randomID() }) id!: string; |
| @Prop({ default: false }) disabled!: boolean; |
| @Prop({ required: true }) options!: (string | IUserArgOption)[]; |
| @Prop({}) description!: string; |
| |
| |
| |
| |
| |
| |
| get optionsToUse(): IUserArgOption[] { |
| return this.options.map((o: string | IUserArgOption) => { |
| if (typeof o === "string") { |
| return { |
| description: o, |
| val: slugify(o), |
| }; |
| } else { |
| return o; |
| } |
| }); |
| } |
| |
| |
| |
| |
| |
| |
| handleInput(e: any) { |
| this.$emit("update:modelValue", e.target.value); |
| |
| |
| |
| this.$emit("onChange", e.target.value); |
| } |
| |
| |
| |
| |
| |
| private validateModelValue() { |
| |
| |
| const validValues = this.optionsToUse.map((opt) => opt.val); |
| if (this.modelValue && !validValues.includes(this.modelValue)) { |
| console.warn( |
| `FormSelect (id: ${this.id}): The provided modelValue "${this.modelValue}" is not a valid option. Available options are:`, |
| validValues |
| ); |
| } |
| } |
| |
| |
| |
| |
| @Watch("modelValue") |
| onModelValueChanged() { |
| this.validateModelValue(); |
| } |
| |
| |
| |
| |
| @Watch("options", { deep: true }) |
| onOptionsChanged() { |
| this.validateModelValue(); |
| } |
| |
| |
| |
| |
| mounted() { |
| this.validateModelValue(); |
| } |
| } |
| </script> |
| |
| |
| <style lang="scss"></style> |
| |