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

@programmermark/ztree

v0.0.9

Published

使用ztree.js创建的react树型组件.

Downloads

18

Readme

使用 ztree.js 创建的树型组件

安装

npm i @programmermark/ztree

参数说明

id: 节点 id,唯一标识

pid: 父级节点 id,没有父节点时,该参数为 undefined

checked: checkbox 是否选中

open: 节点是否展开,

clickSelf: 为 true 时,点击事件只会选中自身; 为 false 时,点击事件会选中自身和子节点

除此之外,节点还可以自定义属性。具体请参看代码中的 typescript 注释。

代码示例


import React, { useState } from "react";
import "./App.css";
import { Tree, ITreeNode } from "@programmermark/ztree";

function App() {
  const [treeData, setTreeData] = useState<ITreeNode[]>([
    {
      id: "10000",
      pId: undefined,
      name: "湖北省",
      checked: true,
      open: false,
    },
    {
      id: "10001",
      pId: "10000",
      name: "武汉市",
      checked: true,
      open: true,
      clickSelf: true /** 为true时,点击事件只会选中自身 */,
    },
    {
      id: "10010",
      pId: "10001",
      name: "汉口",
      checked: true,
      open: true,
    },
    {
      id: "10011",
      pId: "10001",
      name: "武昌",
      checked: true,
      open: true,
    },
    {
      id: "10012",
      pId: "10001",
      name: "汉阳",
      checked: true,
      open: true,
    },
    {
      id: "10002",
      pId: "10000",
      name: "孝感市",
      checked: true,
      open: true,
    },
    {
      id: "10020",
      pId: "10002",
      name: "云梦",
      checked: true,
      open: true,
    },
    {
      id: "10021",
      pId: "10002",
      name: "大悟",
      checked: true,
      open: true,
    },
    {
      id: "10022",
      pId: "10002",
      name: "应城",
      checked: true,
      open: true,
    },
  ]);

  const handleUpdateExpandNode = (id: string, open: boolean) => {
    setTreeData((treeData) => {
      const newTreeData = treeData.map((node) => {
        if (node.id === id) {
          // 更新当前节点的展开状态
          return {
            ...node,
            open,
          };
        }
        return node;
      });
      return newTreeData;
    });
  };

  const handleUpdateNodes = (ids: string[], checked: boolean) => {
    setTreeData((treeData) => {
      const newTreeData = treeData.map((node) => {
        if (ids.includes(node.id)) {
          // 更新当前节点的选中状态
          return {
            ...node,
            checked,
          };
        }
        return node;
      });
      return newTreeData;
    });
  };

  return (
    <div className="App">
      <Tree
        nodes={treeData}
        updateExpandNode={handleUpdateExpandNode}
        updateNodes={handleUpdateNodes}
      />
    </div>
  );
}

export default App;