npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@hanmotec/tsui-tree-view

v0.1.8

Published

[中文文档](https://github.com/hanmo-chn/svelte-tree-view/blob/main/README-CN.md)

Downloads

64

Readme

Tree View Component

中文文档

This is a Svelte component that allows you to display a tree structure of TreeNode objects, where each node can be expanded or collapsed to reveal or hide its child nodes.

Installation

you can install it via npm.

npm i -D @hanmotec/tsui-tree-view

TreeNode Interface and Tree Converter Function

TreeNode Interface

The TreeNode interface defines the structure of a tree node object. It has the following properties:

value: a string that represents the value of the node.
text: a string that represents the display text for the node.
data: an optional property that can hold additional data for the node.
children: an optional array of TreeNode objects that represent the child nodes of the current node.
parent: an optional property that represents the parent node of the current node.
Here's an example of how to create a TreeNode object:

const node: TreeNode = {
    value: 'node1',
    text: 'Node 1',
    data: { someData: 'example' },
    children: [],
    parent: null
};

Tree Converter Functions

The buildTree function takes an array of objects and converts it into a tree structure. The function takes the following parameters:

arr: the array of objects to convert. keyField: the name of the field in the objects that contains the node value.
textField: the name of the field in the objects that contains the display text for the node.
parentField: the name of the field in the objects that contains the value of the parent node.
rootParentCode: an optional value that represents the root node of the tree.
Here's an example of how to use the buildTree function:

const data = [
    { id: 1, name: 'Node 1', parentId: null },
    { id: 2, name: 'Node 1.1', parentId: 1 },
    { id: 3, name: 'Node 1.2', parentId: 1 },
    { id: 4, name: 'Node 2', parentId: null },
    { id: 5, name: 'Node 2.1', parentId: 4 }
];

const tree = buildTree(data, 'id', 'name', 'parentId');

This will create a tree structure with the following nodes:

[
    {
        value: '1',
        text: 'Node 1',
        data: { id: 1, name: 'Node 1', parentId: null },
        children: [
            {
                value: '2',
                text: 'Node 1.1',
                data: { id: 2, name: 'Node 1.1', parentId: 1 },
                children: []
            },
            {
                value: '3',
                text: 'Node 1.2',
                data: { id: 3, name: 'Node 1.2', parentId: 1 },
                children: []
            }
        ],
        parent: null
    },
    {
        value: '4',
        text: 'Node 2',
        data: { id: 4, name: 'Node 2', parentId: null },
        children: [
            {
                value: '5',
                text: 'Node 2.1',
                data: { id: 5, name: 'Node 2.1', parentId: 4 },
                children: []
            }
        ],
       

Usage

To use this component, you will need to import it into your Svelte app and provide it with an array of TreeNode objects to display. You can also customize the icons used for the expand/collapse buttons and the leaf nodes, as well as apply a custom CSS style.

Here's an example of how to use the component:

<script>
    import TreeView from "@hanmotec/tsui-tree-view";
    
    const nodes = [
        {
            value: '1',
            text: 'Node 1',
            data: { id: 1, name: 'Node 1', parentId: null },
            children: [
                {
                    value: '2',
                    text: 'Node 1.1',
                    data: { id: 2, name: 'Node 1.1', parentId: 1 },
                    children: []
                },
                {
                    value: '3',
                    text: 'Node 1.2',
                    data: { id: 3, name: 'Node 1.2', parentId: 1 },
                    children: []
                }
            ],
            parent: null
        },
        {
            value: '4',
            text: 'Node 2',
            data: {id: 4, name: 'Node 2', parentId: null},
            children: [
                {
                    value: '5',
                    text: 'Node 2.1',
                    data: {id: 5, name: 'Node 2.1', parentId: 4},
                    children: []
                }
            ]
        }
    ];
</script>

<TreeView nodes={nodes} style="font-size: 14px" />

Props

The TreeView component accepts the following props:

nodes: an array of TreeNode objects to display in the tree. expand: a boolean indicating whether the tree should be fully expanded or collapsed by default. expandIcon: the icon to use for expanded nodes. foldIcon: the icon to use for collapsed nodes. leafIcon: the icon to use for leaf nodes. style: a string containing CSS styles to apply to the tree container.

Events

The TreeView component emits a select event when a node is clicked. The event object contains a detail property with the selected TreeNode object.

To handle the select event, you can add an event listener to the component instance:

<script>
    import TreeView from "@hanmotec/tsui-tree-view";
    
    const nodes = [...];
    
    const handleNodeSelected = (e) => {
        const selectedNode = e.detail;
        console.log(`Selected node: ${selectedNode.name}`);
    };
</script>

<TreeView nodes={nodes} on:select={handleNodeSelected} />

License This project is licensed under the MIT license.

Contributing We welcome contributions to this package! If you have an idea for a feature or would like to report a bug, please open an issue. If you'd like to contribute code, please fork the repository and create a pull request.