🚨 Shai-Hulud Strikes Again:834 Packages Compromised.Technical Analysis →
Socket
Book a DemoInstallSign in
Socket

@idui/react-tree

Package Overview
Dependencies
Maintainers
1
Versions
23
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@idui/react-tree

React Tree Component

latest
Source
npmnpm
Version
2.0.4
Version published
Maintainers
1
Created
Source

Tree React Component

NPM Size JavaScript Style Guide Coverage Status LICENSE

  • Docs
  • Playground

Install

npm install --save @idui/react-tree
yarn add @idui/react-tree

See props in Docs

Basic Example

import React from 'react'
import Tree from '@idui/react-tree'

const nodes = [
    { label: 'Cake' },
    { label: 'Coffee', childNodes: [
        { label: 'Cappuccino' },
        { label: 'Latte' },
        { label: 'Americano' },
    ]},
]

function Example() {
  return <Tree nodes={nodes} />
}

Custom Tree

import React from 'react'
import styled from 'styled-components'
import Tree from '@idui/react-tree'

const CustomTree = styled(Tree)`
  border-left: 1px solid #aeaeae;
  margin-left: 3.5px;
`;

const CustomLeaf = styled.div`
  color: rgba(0, 0, 0, 0.7);
  margin-bottom: 2px;
  ${ifProp(
    'hasChildren',
    css`
      transition: color 0.3s ease-in-out;
      cursor: pointer;
      &:hover {
        color: rgba(0, 0, 0, 1);
      }
    `
  )};
`;

const renderCustomLeaf = ({ toggle, isOpen, icon, label, hasChildren }) => (
  <CustomLeaf hasChildren={hasChildren} onClick={toggle}>
    {hasChildren && (isOpen ? 'â–ľ' : 'â–¸') + ' '}
    {icon} {label}
  </CustomLeaf>
);

const nodes = [
{
  label: 'Cake',
  icon: '🍰',
  childNodes: [
    {
      label: 'Chocolate',
      icon: '🍫',
    },
    {
      label: 'Vanilla',
      icon: '🍬',
    },
    {
      label: 'Strawberry',
      icon: '🍓',
    },
  ],
}]

function Example() {
  return <Tree nodes={nodes} renderLeaf={renderCustomLeaf} />
}

Checkbox Tree

import React, { useState } from 'react'
import { CheckboxTree } from '@idui/react-tree'

const nodes = [
    { label: 'Cake', id: 'cake' },
    { label: 'Coffee', id: 'coffee', childNodes: [
        { label: 'Cappuccino', id: 'Cappuccino' },
        { label: 'Latte', id: 'Latte' },
        { label: 'Americano', id: 'Americano' },
    ]},
]

function Example() {
  const [checkedKeys, setCheckedKeys] = useState([]);
    return (
      <CheckboxTree
        {...props}
        checkedKeys={checkedKeys}
        nodes={nodes}
        onChange={setCheckedKeys}
      />
    );
}

Search in Tree

import React, { useState, useCallback } from 'react'
import styled from 'styled-components'
import Tree from '@idui/react-tree'

const nodes = [
    { label: 'Cake' },
    { label: 'Coffee', childNodes: [
        { label: 'Cappuccino' },
        { label: 'Latte' },
        { label: 'Americano' },
    ]},
]

const SearchTreeLeaf = styled.div`
  .highlight {
    background-color: #ffa7a7;
  }
`;

function Example() {
  const [search, setSearch] = useState('');
  const handleSearch = useCallback((e) => {
    setSearch(e.target.value);
  }, []);

  return (
    <div>
      <input type="search" onChange={handleSearch} />
      <Tree
        nodes={nodes}
        search={search}
        filterHighlighted
        renderLeaf={({ toggle, isOpen, label, hasChildren }) => (
          <SearchTreeLeaf onClick={toggle}>
            {hasChildren && (isOpen ? 'â–ľ' : 'â–¸') + ' '}
            <span dangerouslySetInnerHTML={{ __html: label }} />
          </SearchTreeLeaf>
        )}
      />
    </div>
  );
}

See more details in storybook

License

MIT © kaprisa57@gmail.com

Keywords

react

FAQs

Package last updated on 09 Feb 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts