Profectus/src/components/fields/Select.vue

94 lines
2.2 KiB
Vue
Raw Normal View History

2021-05-22 15:29:06 -05:00
<template>
<div class="field">
2022-01-24 22:23:30 -06:00
<span class="field-title" v-if="titleComponent"><component :is="titleComponent" /></span>
2022-01-13 22:25:47 -06:00
<VueNextSelect
:options="options"
2022-01-13 22:25:47 -06:00
v-model="value"
@update:model-value="onUpdate"
:min="1"
label-by="label"
:placeholder="placeholder"
:close-on-select="closeOnSelect"
/>
</div>
2021-05-22 15:29:06 -05:00
</template>
2022-01-13 22:25:47 -06:00
<script setup lang="ts">
2022-03-03 21:39:48 -06:00
import "components/common/fields.css";
2022-06-26 19:17:22 -05:00
import type { CoercableComponent } from "features/feature";
import { computeOptionalComponent, unwrapRef } from "util/vue";
import { ref, toRef, watch } from "vue";
2022-01-13 22:25:47 -06:00
import VueNextSelect from "vue-next-select";
import "vue-next-select/dist/index.css";
2022-01-13 22:25:47 -06:00
export type SelectOption = { label: string; value: unknown };
const props = defineProps<{
2022-01-24 22:25:34 -06:00
title?: CoercableComponent;
modelValue?: unknown;
options: SelectOption[];
placeholder?: string;
closeOnSelect?: boolean;
}>();
2022-01-13 22:25:47 -06:00
const emit = defineEmits<{
(e: "update:modelValue", value: unknown): void;
}>();
const titleComponent = computeOptionalComponent(toRef(props, "title"), "span");
2022-01-13 22:25:47 -06:00
const value = ref<SelectOption | null>(
props.options.find(option => option.value === props.modelValue) ?? null
);
watch(toRef(props, "modelValue"), modelValue => {
if (unwrapRef(value) !== modelValue) {
value.value = props.options.find(option => option.value === modelValue) ?? null;
}
});
function onUpdate(value: SelectOption) {
emit("update:modelValue", value.value);
}
2021-05-22 15:29:06 -05:00
</script>
<style>
2021-07-24 17:08:52 -05:00
.vue-select {
2021-05-22 15:29:06 -05:00
width: 50%;
2021-09-04 16:51:41 -05:00
border-radius: var(--border-radius);
2021-05-22 15:29:06 -05:00
}
2021-07-24 17:08:52 -05:00
.field-buttons .vue-select {
width: unset;
2021-05-22 15:29:06 -05:00
}
2021-07-24 17:08:52 -05:00
.vue-select,
.vue-dropdown {
2021-09-04 16:51:41 -05:00
border-color: var(--outline);
2021-05-22 15:29:06 -05:00
}
2021-07-24 17:08:52 -05:00
.vue-dropdown {
2021-09-04 16:51:41 -05:00
background: var(--raised-background);
2021-05-22 15:29:06 -05:00
}
2021-07-24 17:08:52 -05:00
.vue-dropdown-item {
color: var(--foreground);
2021-09-04 16:51:41 -05:00
}
.vue-dropdown-item,
.vue-dropdown-item * {
transition-duration: 0s;
2021-05-22 15:29:06 -05:00
}
2021-07-24 17:08:52 -05:00
.vue-dropdown-item.highlighted {
2021-09-04 16:51:41 -05:00
background-color: var(--highlighted);
2021-05-22 15:29:06 -05:00
}
2021-06-20 23:29:55 -05:00
2021-07-24 17:08:52 -05:00
.vue-dropdown-item.selected,
.vue-dropdown-item.highlighted.selected {
background-color: var(--bought);
2021-06-20 23:29:55 -05:00
}
2021-09-04 16:51:41 -05:00
.vue-input input::placeholder {
color: var(--link);
}
2021-05-22 15:29:06 -05:00
</style>