mirror of
https://github.com/hay-kot/homebox.git
synced 2025-08-04 08:40:28 +00:00
wip: location selector improvements
This commit is contained in:
parent
2e55e9ab45
commit
8f1299fab4
5 changed files with 101 additions and 29 deletions
|
@ -6,7 +6,7 @@
|
||||||
<div class="dropdown dropdown-top sm:dropdown-end">
|
<div class="dropdown dropdown-top sm:dropdown-end">
|
||||||
<div class="relative">
|
<div class="relative">
|
||||||
<input
|
<input
|
||||||
v-model="isearch"
|
v-model="internalSearch"
|
||||||
tabindex="0"
|
tabindex="0"
|
||||||
class="input w-full items-center flex flex-wrap border border-gray-400 rounded-lg"
|
class="input w-full items-center flex flex-wrap border border-gray-400 rounded-lg"
|
||||||
@keyup.enter="selectFirst"
|
@keyup.enter="selectFirst"
|
||||||
|
@ -26,9 +26,11 @@
|
||||||
class="dropdown-content mb-1 menu shadow border border-gray-400 rounded bg-base-100 w-full z-[9999] max-h-60 overflow-y-scroll"
|
class="dropdown-content mb-1 menu shadow border border-gray-400 rounded bg-base-100 w-full z-[9999] max-h-60 overflow-y-scroll"
|
||||||
>
|
>
|
||||||
<li v-for="(obj, idx) in filtered" :key="idx">
|
<li v-for="(obj, idx) in filtered" :key="idx">
|
||||||
<button type="button" @click="select(obj)">
|
<div type="button" @click="select(obj)">
|
||||||
{{ usingObjects ? obj[itemText] : obj }}
|
<slot name="display" v-bind="{ item: obj }">
|
||||||
</button>
|
{{ usingObjects ? obj[itemText] : obj }}
|
||||||
|
</slot>
|
||||||
|
</div>
|
||||||
</li>
|
</li>
|
||||||
<li class="hidden first:flex">
|
<li class="hidden first:flex">
|
||||||
<button disabled>
|
<button disabled>
|
||||||
|
@ -52,6 +54,7 @@
|
||||||
modelValue: string | ItemsObject;
|
modelValue: string | ItemsObject;
|
||||||
items: ItemsObject[] | string[];
|
items: ItemsObject[] | string[];
|
||||||
itemText?: keyof ItemsObject;
|
itemText?: keyof ItemsObject;
|
||||||
|
itemSearch?: keyof ItemsObject | null;
|
||||||
itemValue?: keyof ItemsObject;
|
itemValue?: keyof ItemsObject;
|
||||||
search?: string;
|
search?: string;
|
||||||
noResultsText?: string;
|
noResultsText?: string;
|
||||||
|
@ -63,21 +66,34 @@
|
||||||
modelValue: "",
|
modelValue: "",
|
||||||
items: () => [],
|
items: () => [],
|
||||||
itemText: "text",
|
itemText: "text",
|
||||||
itemValue: "value",
|
|
||||||
search: "",
|
search: "",
|
||||||
|
itemSearch: null,
|
||||||
|
itemValue: "value",
|
||||||
noResultsText: "No Results Found",
|
noResultsText: "No Results Found",
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const searchKey = computed(() => props.itemSearch || props.itemText);
|
||||||
|
|
||||||
function clear() {
|
function clear() {
|
||||||
select(value.value);
|
select(value.value);
|
||||||
}
|
}
|
||||||
|
|
||||||
const isearch = ref("");
|
const internalSearch = ref("");
|
||||||
watch(isearch, () => {
|
|
||||||
internalSearch.value = isearch.value;
|
watch(
|
||||||
});
|
() => props.search,
|
||||||
|
val => {
|
||||||
|
internalSearch.value = val;
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
watch(
|
||||||
|
() => internalSearch.value,
|
||||||
|
val => {
|
||||||
|
emit("update:search", val);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
const internalSearch = useVModel(props, "search", emit);
|
|
||||||
const value = useVModel(props, "modelValue", emit);
|
const value = useVModel(props, "modelValue", emit);
|
||||||
|
|
||||||
const usingObjects = computed(() => {
|
const usingObjects = computed(() => {
|
||||||
|
@ -102,9 +118,9 @@
|
||||||
() => {
|
() => {
|
||||||
if (value.value) {
|
if (value.value) {
|
||||||
if (typeof value.value === "string") {
|
if (typeof value.value === "string") {
|
||||||
isearch.value = value.value;
|
internalSearch.value = value.value;
|
||||||
} else {
|
} else {
|
||||||
isearch.value = value.value[props.itemText] as string;
|
internalSearch.value = value.value[searchKey.value] as string;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
@ -131,16 +147,16 @@
|
||||||
}
|
}
|
||||||
|
|
||||||
const filtered = computed(() => {
|
const filtered = computed(() => {
|
||||||
if (!isearch.value || isearch.value === "") {
|
if (!internalSearch.value || internalSearch.value === "") {
|
||||||
return props.items;
|
return props.items;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isStrings(props.items)) {
|
if (isStrings(props.items)) {
|
||||||
return props.items.filter(item => item.toLowerCase().includes(isearch.value.toLowerCase()));
|
return props.items.filter(item => item.toLowerCase().includes(internalSearch.value.toLowerCase()));
|
||||||
} else {
|
} else {
|
||||||
return props.items.filter(item => {
|
return props.items.filter(item => {
|
||||||
if (props.itemText && props.itemText in item) {
|
if (searchKey.value && searchKey.value in item) {
|
||||||
return (item[props.itemText] as string).toLowerCase().includes(isearch.value.toLowerCase());
|
return (item[searchKey.value] as string).toLowerCase().includes(internalSearch.value.toLowerCase());
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
});
|
});
|
||||||
|
|
|
@ -12,11 +12,22 @@
|
||||||
<FormTextArea v-model="form.description" label="Location Description" />
|
<FormTextArea v-model="form.description" label="Location Description" />
|
||||||
<FormAutocomplete
|
<FormAutocomplete
|
||||||
v-model="form.parent"
|
v-model="form.parent"
|
||||||
|
v-model:search="form.search"
|
||||||
:items="locations"
|
:items="locations"
|
||||||
item-text="name"
|
item-text="display"
|
||||||
item-value="id"
|
item-value="id"
|
||||||
|
item-search="name"
|
||||||
label="Parent Location"
|
label="Parent Location"
|
||||||
/>
|
>
|
||||||
|
<template #display="{ item }">
|
||||||
|
<div>
|
||||||
|
<div>
|
||||||
|
{{ item.name }}
|
||||||
|
</div>
|
||||||
|
<div v-if="item.name != item.display" class="text-xs mt-1">{{ item.display }}</div>
|
||||||
|
</div>
|
||||||
|
</template>
|
||||||
|
</FormAutocomplete>
|
||||||
<div class="modal-action">
|
<div class="modal-action">
|
||||||
<BaseButton type="submit" :loading="loading"> Create </BaseButton>
|
<BaseButton type="submit" :loading="loading"> Create </BaseButton>
|
||||||
</div>
|
</div>
|
||||||
|
@ -26,7 +37,6 @@
|
||||||
|
|
||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
import { LocationSummary } from "~~/lib/api/types/data-contracts";
|
import { LocationSummary } from "~~/lib/api/types/data-contracts";
|
||||||
import { useLocationStore } from "~~/stores/locations";
|
|
||||||
const props = defineProps({
|
const props = defineProps({
|
||||||
modelValue: {
|
modelValue: {
|
||||||
type: Boolean,
|
type: Boolean,
|
||||||
|
@ -34,14 +44,14 @@
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
const locationStore = useLocationStore();
|
const locations = await useFlatLocations();
|
||||||
|
|
||||||
const locations = computed(() => locationStore.allLocations);
|
|
||||||
const modal = useVModel(props, "modelValue");
|
const modal = useVModel(props, "modelValue");
|
||||||
const loading = ref(false);
|
const loading = ref(false);
|
||||||
const focused = ref(false);
|
const focused = ref(false);
|
||||||
const form = reactive({
|
const form = reactive({
|
||||||
name: "",
|
name: "",
|
||||||
|
search: "",
|
||||||
description: "",
|
description: "",
|
||||||
parent: null as LocationSummary | null,
|
parent: null as LocationSummary | null,
|
||||||
});
|
});
|
||||||
|
@ -56,6 +66,7 @@
|
||||||
function reset() {
|
function reset() {
|
||||||
form.name = "";
|
form.name = "";
|
||||||
form.description = "";
|
form.description = "";
|
||||||
|
form.search = "";
|
||||||
form.parent = null;
|
form.parent = null;
|
||||||
focused.value = false;
|
focused.value = false;
|
||||||
modal.value = false;
|
modal.value = false;
|
||||||
|
|
44
frontend/composables/use-location-helpers.ts
Normal file
44
frontend/composables/use-location-helpers.ts
Normal file
|
@ -0,0 +1,44 @@
|
||||||
|
import { Ref } from "vue";
|
||||||
|
import { TreeItem } from "~~/lib/api/types/data-contracts";
|
||||||
|
|
||||||
|
export interface FlatTreeItem {
|
||||||
|
id: string;
|
||||||
|
name: string;
|
||||||
|
display: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function flatTree(tree: TreeItem[]): Ref<FlatTreeItem[]> {
|
||||||
|
const v = ref<FlatTreeItem[]>([]);
|
||||||
|
|
||||||
|
// turns the nested items into a flat items array where
|
||||||
|
// the display is a string of the tree hierarchy separated by breadcrumbs
|
||||||
|
|
||||||
|
function flatten(items: TreeItem[], display: string) {
|
||||||
|
for (const item of items) {
|
||||||
|
v.value.push({
|
||||||
|
id: item.id,
|
||||||
|
name: item.name,
|
||||||
|
display: display + item.name,
|
||||||
|
});
|
||||||
|
if (item.children) {
|
||||||
|
flatten(item.children, display + item.name + " > ");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
flatten(tree, "");
|
||||||
|
|
||||||
|
return v;
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function useFlatLocations(): Promise<Ref<FlatTreeItem[]>> {
|
||||||
|
const api = useUserApi();
|
||||||
|
|
||||||
|
const locations = await api.locations.getTree();
|
||||||
|
|
||||||
|
if (!locations) {
|
||||||
|
return ref([]);
|
||||||
|
}
|
||||||
|
|
||||||
|
return flatTree(locations.data.items);
|
||||||
|
}
|
|
@ -6,13 +6,17 @@ export type LocationsQuery = {
|
||||||
filterChildren: boolean;
|
filterChildren: boolean;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export type TreeQuery = {
|
||||||
|
withItems: boolean;
|
||||||
|
};
|
||||||
|
|
||||||
export class LocationsApi extends BaseAPI {
|
export class LocationsApi extends BaseAPI {
|
||||||
getAll(q: LocationsQuery = { filterChildren: false }) {
|
getAll(q: LocationsQuery = { filterChildren: false }) {
|
||||||
return this.http.get<Results<LocationOutCount>>({ url: route("/locations", q) });
|
return this.http.get<Results<LocationOutCount>>({ url: route("/locations", q) });
|
||||||
}
|
}
|
||||||
|
|
||||||
getTree() {
|
getTree(tq = { withItems: false }) {
|
||||||
return this.http.get<Results<TreeItem>>({ url: route("/locations/tree") });
|
return this.http.get<Results<TreeItem>>({ url: route("/locations/tree", tq) });
|
||||||
}
|
}
|
||||||
|
|
||||||
create(body: LocationCreate) {
|
create(body: LocationCreate) {
|
||||||
|
|
|
@ -179,12 +179,9 @@
|
||||||
<DetailsSection :details="details" />
|
<DetailsSection :details="details" />
|
||||||
</BaseCard>
|
</BaseCard>
|
||||||
|
|
||||||
<section v-if="location && location.items.length > 0">
|
<template v-if="location && location.items.length > 0">
|
||||||
<BaseSectionHeader class="mb-5"> Items </BaseSectionHeader>
|
<ItemViewSelectable :items="location.items" />
|
||||||
<div class="grid grid-cols-1 sm:grid-cols-2 md:grid-cols-3 gap-4">
|
</template>
|
||||||
<ItemCard v-for="item in location.items" :key="item.id" :item="item" />
|
|
||||||
</div>
|
|
||||||
</section>
|
|
||||||
|
|
||||||
<section v-if="location && location.children.length > 0">
|
<section v-if="location && location.children.length > 0">
|
||||||
<BaseSectionHeader class="mb-5"> Child Locations </BaseSectionHeader>
|
<BaseSectionHeader class="mb-5"> Child Locations </BaseSectionHeader>
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue