Skip to content

Commit

Permalink
feat(components/tree): wip render tree tsx component
Browse files Browse the repository at this point in the history
Signed-off-by: Braden Mars <[email protected]>
  • Loading branch information
BradenM committed Mar 5, 2024
1 parent 9a3f43f commit 2cc3214
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions src/components/tree/Recurse.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import type { TreeNode } from './node';
import { findUniqueNodeTypes, Tree } from './node';
import type { FunctionalComponent, Slot, VNode } from 'vue';

export const RenderTree: FunctionalComponent<{
tree: Tree<any>;
seen?: Set<any>;
sub?: boolean;
}> = (props, context) => {
const nodeTypes = computed(() => [...findUniqueNodeTypes(props.tree)]);

const vnodes: [Slot, { node: TreeNode<any>; subtree?: VNode }][] = [];

const slotV = Object.fromEntries(
nodeTypes.value.map((nt) => [nt, context.slots[nt]]),
);
console.log('slotv:', slotV);

// const seen = props.seen ?? new Set<TreeNode<any>>();
let seen = toRef(props, 'seen');
seen = seen?.value ?? ref(new Set());

props.tree.dfs((node) => {
// if (props.sub) return;
// if (seen.value.has(node)) return;
seen.value.add(node);
const slot = slotV[node.constructor.name] ?? context.slots.default;
if (slot) {
const subtree = node.isLeaf ? undefined : new Tree(node.clone());
vnodes.push([slot, { node, subtree, self: slot, seen: seen.value }]);
}
});

return (
<div>
{{
default: () => vnodes.map(([slot, slotProps]) => slot(slotProps)),
}}
</div>
);
};

RenderTree.props = {
tree: {
type: Object,
required: true,
},
};

Check warning on line 48 in src/components/tree/Recurse.tsx

View check run for this annotation

Codecov / codecov/patch

src/components/tree/Recurse.tsx#L1-L48

Added lines #L1 - L48 were not covered by tests

0 comments on commit 2cc3214

Please sign in to comment.