2023-01-28 20:53:00 +00:00
|
|
|
<template>
|
2023-02-10 02:47:41 +00:00
|
|
|
<FormAutocomplete2 v-if="locations" v-model="value" :items="locations" display="name" label="Parent Location">
|
|
|
|
<template #display="{ item, selected, active }">
|
2023-01-28 20:53:00 +00:00
|
|
|
<div>
|
2023-02-10 02:47:41 +00:00
|
|
|
<div class="flex w-full">
|
|
|
|
{{ cast(item.value).name }}
|
|
|
|
<span
|
|
|
|
v-if="selected"
|
|
|
|
:class="['absolute inset-y-0 right-0 flex items-center pr-4', active ? 'text-white' : 'text-primary']"
|
|
|
|
>
|
|
|
|
<Icon name="mdi-check" class="h-5 w-5" aria-hidden="true" />
|
|
|
|
</span>
|
|
|
|
</div>
|
|
|
|
<div v-if="cast(item.value).name != cast(item.value).treeString" class="text-xs mt-1">
|
|
|
|
{{ cast(item.value).treeString }}
|
2023-01-28 20:53:00 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</template>
|
2023-02-10 02:47:41 +00:00
|
|
|
</FormAutocomplete2>
|
2023-01-28 20:53:00 +00:00
|
|
|
</template>
|
|
|
|
|
|
|
|
<script lang="ts" setup>
|
2023-02-10 02:47:41 +00:00
|
|
|
import { FlatTreeItem, useFlatLocations } from "~~/composables/use-location-helpers";
|
2023-01-28 20:53:00 +00:00
|
|
|
import { LocationSummary } from "~~/lib/api/types/data-contracts";
|
|
|
|
|
|
|
|
type Props = {
|
|
|
|
modelValue?: LocationSummary | null;
|
|
|
|
};
|
|
|
|
|
2023-02-10 02:47:41 +00:00
|
|
|
// Cast the type of the item to a FlatTreeItem so we can get type "safety" in the template
|
|
|
|
// Note that this does not actually change the type of the item, it just tells the compiler
|
|
|
|
// that the type is FlatTreeItem. We must keep this in sync with the type of the items
|
|
|
|
function cast(value: any): FlatTreeItem {
|
|
|
|
return value as FlatTreeItem;
|
|
|
|
}
|
2023-01-28 20:53:00 +00:00
|
|
|
|
2023-02-10 02:47:41 +00:00
|
|
|
const props = defineProps<Props>();
|
2023-01-28 20:53:00 +00:00
|
|
|
const value = useVModel(props, "modelValue");
|
|
|
|
|
2024-02-29 19:45:05 +00:00
|
|
|
const locations = useFlatLocations();
|
2023-01-28 20:53:00 +00:00
|
|
|
const form = ref({
|
|
|
|
parent: null as LocationSummary | null,
|
|
|
|
search: "",
|
|
|
|
});
|
|
|
|
|
|
|
|
// Whenever parent goes from value to null reset search
|
|
|
|
watch(
|
|
|
|
() => value.value,
|
|
|
|
() => {
|
|
|
|
if (!value.value) {
|
|
|
|
form.value.search = "";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
</script>
|