evcc-io/assets/js/components/Config/DeviceModal/TemplateSelector.vue
andig 06f6eb1d26
HEMS: add templates (#25832)
Co-authored-by: Michael Geers <michael@geers.tv>
Co-authored-by: premultiply <4681172+premultiply@users.noreply.github.com>
2026-06-27 14:32:13 +02:00

96 lines
2.1 KiB
Vue

<template>
<FormRow :id="`${deviceType}Template`" :label="$t(`config.${deviceType}.template`)">
<select
v-if="isNew"
:id="`${deviceType}Template`"
ref="select"
v-model="modelProxy"
class="form-select w-100"
:disabled="disabled"
@change="changed"
>
<template v-for="group in groups" :key="group.label">
<optgroup
v-if="group.options?.length"
:label="$t(`config.${deviceType}.${group.label}`)"
>
<option
v-for="option in group.options"
:key="option.name"
:value="option.template"
>
{{ option.name }}
</option>
</optgroup>
</template>
</select>
<div v-else class="d-flex gap-2 align-items-stretch">
<input
:id="`${deviceType}Template`"
type="text"
:value="productName || $t('config.general.customOption')"
disabled
class="form-control"
/>
<slot name="action" />
</div>
</FormRow>
</template>
<script lang="ts">
import { defineComponent, type PropType } from "vue";
import FormRow from "../FormRow.vue";
import { type DeviceType } from "@/types/evcc";
export interface TemplateOption {
name: string;
template: string;
}
export interface PrimaryOption {
name: string;
template: string;
}
export interface TemplateGroup {
label: string;
options: TemplateOption[];
}
export default defineComponent({
name: "TemplateSelector",
components: { FormRow },
props: {
deviceType: String as PropType<DeviceType>,
isNew: Boolean,
modelValue: String as PropType<string | null>,
productName: String,
groups: Array as PropType<TemplateGroup[]>,
disabled: Boolean,
},
emits: ["update:modelValue", "change"],
computed: {
modelProxy: {
get() {
return this.modelValue;
},
set(value: string) {
this.$emit("update:modelValue", value);
},
},
},
methods: {
changed(e: Event) {
this.$emit("change", e);
},
getProductName() {
const select = this.$refs["select"] as HTMLSelectElement;
return select.options[select.selectedIndex]?.text || "";
},
},
});
export function customTemplateOption(name: string, template = "custom") {
return { name, template, group: "" };
}
</script>