Skip to content

A continuation of HomeBox the inventory and organization system built for the Home User

fix: parent item search doesnt update properly

tonyaellied54c1ea8572e
diff --git a/frontend/components/Item/Selector.vue b/frontend/components/Item/Selector.vue
index a6658071..0ce026bf 100644
--- a/frontend/components/Item/Selector.vue
+++ b/frontend/components/Item/Selector.vue
@@ -39,7 +39,7 @@
{{ localizedNoResultsText }}
</div>
</CommandEmpty>
- <CommandList>
+ <CommandList :key="commandListKey">
<CommandGroup>
<CommandItem v-for="item in filtered" :key="itemKey(item)" :value="itemKey(item)" @select="select(item)">
<Check :class="cn('mr-2 h-4 w-4', isSelected(item) ? 'opacity-100' : 'opacity-0')" />
@@ -106,7 +106,7 @@
const id = useId();
const open = ref(false);
- const search = ref(props.search);
+ const search = useVModel(props, "search", emit);
const value = useVModel(props, "modelValue", emit);
const hasInitialSearch = ref(false);
@@ -142,20 +142,6 @@
}
);
- watch(
- () => props.search,
- val => {
- search.value = val;
- }
- );
-
- watch(
- () => search.value,
- val => {
- emit("update:search", val);
- }
- );
-
function isStrings(arr: string[] | ItemsObject[]): arr is string[] {
return arr.length > 0 && typeof arr[0] === "string";
}
@@ -194,19 +180,30 @@
}
const filtered = computed(() => {
- let baseItems = props.items;
+ // Explicitly depend on all reactive sources
+ const items = props.items;
+ const searchTerm = search.value;
+ const excludeItems = props.excludeItems;
+
+ let baseItems = items;
- if (!isStrings(baseItems) && props.excludeItems) {
- const excludeIds = props.excludeItems.map(i => i.id);
+ if (!isStrings(baseItems) && excludeItems) {
+ const excludeIds = excludeItems.map(i => i.id);
baseItems = baseItems.filter(item => !excludeIds?.includes(item.id));
}
- if (!search.value) return baseItems;
+
+ if (!searchTerm) return baseItems;
if (isStrings(baseItems)) {
- return baseItems.filter(item => item.toLowerCase().includes(search.value.toLowerCase()));
+ return baseItems.filter(item => item.toLowerCase().includes(searchTerm.toLowerCase()));
} else {
// Fuzzy search on itemText
- return fuzzysort.go(search.value, baseItems, { key: props.itemText, all: true }).map(i => i.obj);
+ return fuzzysort.go(searchTerm, baseItems, { key: props.itemText, all: true }).map(i => i.obj);
}
});
+
+ // Generate a unique key to force CommandList re-render when items change
+ const commandListKey = computed(() => {
+ return JSON.stringify(filtered.value.map(item => itemKey(item)));
+ });
</script>