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

ui-utils-kit

v1.1.0

Published

ui-utils-kit 是一个高效的偏业务前端工具函数库

Downloads

265

Readme

UI Utils Kit

简介

ui-utils-kit 是一个包含常用树形数据操作和管理工具的前端库。它提供了多种功能,包括构建树形结构、扁平化树结构、更新节点选中状态、查找相关节点等,帮助开发者更高效地操作树形数据。

安装

通过 npm 安装:

npm install ui-utils-kit

或通过 yarn 安装:

yarn add ui-utils-kit

使用方法

1. 构建树形结构

buildTree 用于根据给定的节点数据构建树形结构。

参数

  • nodesArray<TreeNode>):节点数据数组,每个节点包含 idpid 等属性。
  • preserveChildrenboolean,可选):是否保留原节点的 children 属性。

示例

import { buildTree } from 'ui-utils-kit';

const nodes = [
  { id: 1, pid: null, name: 'Root' },
  { id: 2, pid: 1, name: 'Child 1' },
  { id: 3, pid: 1, name: 'Child 2' },
];

const tree = buildTree(nodes);
console.log(tree);

2. 将树形结构转换为扁平化数组

treeToArr 将树形数据扁平化为数组,便于操作。

参数

  • dataArray<TreeNode>):树形数据,包含 id, name 和可选的 children 属性。

示例

import { treeToArr } from 'ui-utils-kit';

const tree = [
  { id: 1, pid: null, name: 'Root', children: [{ id: 2, pid: 1, name: 'Child 1' }] },
];

const flatArray = treeToArr(tree);
console.log(flatArray);

3. 更新树节点选中状态

updateTreeCheckStatus 用于更新树形结构中节点的选中状态,包括子节点和父节点的状态。

参数

  • treeArray<TreeNode>):树形结构的节点数组。
  • selectedNodesArray<TreeNode>):选中的节点数组。

示例

import { updateTreeCheckStatus } from 'ui-utils-kit';

const tree = [
  { id: 1, pid: null, name: 'Root', check: 'Unchecked', children: [{ id: 2, pid: 1, name: 'Child 1', check: 'Unchecked' }] },
];

const selectedNodes = [{ id: 2, pid: 1, name: 'Child 1', check: 'Checked' }];

const updatedTree = updateTreeCheckStatus(tree, selectedNodes);
console.log(updatedTree);

4. 查找相关节点

searchTreeWithRelations 根据关键词查找相关节点,并返回匹配的节点及其父子节点。

参数

  • treeArrArray<TreeNode>):树形数据数组。
  • keywordsstring):用于查找的关键词,匹配节点的 name 属性。
  • markboolean,可选):是否标记匹配的节点。

示例

import { searchTreeWithRelations } from 'ui-utils-kit';

const tree = [
  { id: 1, pid: null, name: 'Root' },
  { id: 2, pid: 1, name: 'Child 1' },
  { id: 3, pid: 1, name: 'Child 2' },
];

const results = searchTreeWithRelations(tree, 'Child 1');
console.log(results);

5. 从树形结构中移除指定节点

removeNodesFromTree 用于从树形数据中移除指定的节点及其子节点。

参数

  • treeArrArray<TreeNode>):树形结构数组。
  • selectedNodesArray<TreeNode>):要移除的节点数组。

示例

import { removeNodesFromTree } from 'ui-utils-kit';

const tree = [
  { id: 1, pid: null, name: 'Root' },
  { id: 2, pid: 1, name: 'Child 1' },
  { id: 3, pid: 1, name: 'Child 2' },
];

const nodesToRemove = [{ id: 2, pid: 1, name: 'Child 1' }];
const filteredTree = removeNodesFromTree(tree, nodesToRemove);
console.log(filteredTree);

6. 合并节点到树形结构

mergeTrees 将新的节点合并到现有的树形结构中。

参数

  • treeArrArray<TreeNode>):现有的树形结构数组。
  • nodesArray<TreeNode>):要合并的节点数组。

示例

import { mergeTrees } from 'ui-utils-kit';

const tree = [
  { id: 1, pid: null, name: 'Root' },
];

const newNodes = [
  { id: 2, pid: 1, name: 'Child 1' },
];

const mergedTree = mergeTrees(tree, newNodes);
console.log(mergedTree);

7. 选择相关节点

selectRelatedNodes 用于选择与选中节点相关的父节点和子节点。

参数

  • treeDataArray<TreeNode>):树形结构数组。
  • selectedNodesArray<TreeNode>):选中的节点数组。

示例

import { selectRelatedNodes } from 'ui-utils-kit';

const tree = [
  { id: 1, pid: null, name: 'Root' },
  { id: 2, pid: 1, name: 'Child 1' },
  { id: 3, pid: 1, name: 'Child 2' },
];

const selectedNodes = [{ id: 2, pid: 1, name: 'Child 1' }];
const relatedNodes = selectRelatedNodes(tree, selectedNodes);
console.log(relatedNodes);

类型

TreeNode 类型

每个节点对象都应该包含以下属性:

  • id (string | number):节点的唯一标识符。
  • pid (string | number | null):节点的父节点标识符。根节点的 pidnull
  • name (string):节点的名称。
  • children (TreeNode[]):子节点数组,仅对树形结构有效。
  • check (CheckStatus):节点的选中状态,可选值为 'Checked' | 'Unchecked' | 'HalfChecked'

CheckStatus 枚举

  • Checked:已选中。
  • Unchecked:未选中。
  • HalfChecked:半选中。

🏆 License