fix: some condition not met make node not show

This commit is contained in:
Methapon Metanipat 2024-10-16 13:34:14 +07:00
parent a51c250ccb
commit 7db7813a9e

View file

@ -93,22 +93,16 @@ function toggleExpand(node: Node, ancestor?: []) {
emits('open', node, ancestor);
}
function filterNode(text: string, node: Node, ancestor?: Node[]) {
if (!text) return;
function visibleNode(text: string, node: Node, ancestor?: Node[]): boolean {
if (!text) return true;
const getTitle = (n: Node): string => n[props.keyTitle || 'title'];
const getSubtitle = (n: Node): string => n[props.keySubtitle || 'subtitle'];
const recursiveFilter = (n: Node): boolean => {
if (!n.children) {
return getTitle(n).includes(text) || getSubtitle(n).includes(text);
}
return n.children.some((v) => recursiveFilter(v));
};
return (
ancestor?.some(
[node, ...(ancestor || [])].some(
(v) => getTitle(v).includes(text) || getSubtitle(v).includes(text),
) || recursiveFilter(node)
) || (node.children || []).some((v) => visibleNode(text, v, []))
);
}
</script>
@ -124,7 +118,7 @@ function filterNode(text: string, node: Node, ancestor?: Node[]) {
<template v-for="(node, i) in nodes" :key="i">
<div
class="tree-item"
v-if="filterText ? filterNode(filterText, node, ancestorNode) : true"
v-if="filterText ? visibleNode(filterText, node, ancestorNode) : true"
>
<slot
v-if="$slots['item']"