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

tree-select

v1.4.0

Published

Customizable dropdown with a tree of options

Downloads

60

Readme

Tree select

A select that allows the user to choose multiple options from a tree of options.

Usage

Install with npm and import as a named import using the syntax of your choice:

import { TreeSelect } from 'tree-select';
var TreeSelect = require('tree-select').TreeSelect;

There are three required props on the component: data, the actual contents of the tree; and onChange and value to control the contents. This is the simplest usage of the component:

<TreeSelect
  data={data}
  onChange={value => this.setState({ value: value })}
  value={this.state.value}
 />

data

The shape of data is a list of items with fields name and children:

[
  { name: "Kitchen", children: [
    { name: "Dishwashing machine" },
    { name: "Teapot" }
  ] },
  { name: "Living room", children: [
    { name: "Sofa" },
    { name: "Chairs", children: [ { name: "Green chair" }, { name: "Blue char" } ] }
  ] }
]

Optional fields on data objects:

  • id. A freeform field for your own tracking purposes.
  • reactKey. A string to uniquely identify the object amongst its siblings in case you modify data on the fly. Without it, the performance of modifying data will suffer. More in React docs.
  • selectable. A corner-case feature where you can select some items as non-selectable. A scenario where a selectable parent contains non-selectable children is at the moment not supported.

onChange and value

The component is only available as a controlled component. This means you need to store the current value and update the component in response to a change event.

The value you provide in value and receive in onChange is a flat array of objects from data.

Options

Use required to make the component trigger the native error box and prevent the ancestor form from submitting:

<TreeSelect
  required
  />

Styling

Default looks on the dropdown are purposefully spartan. You should customize that using CSS and content replacements. There is also some work to improve sticky sections.

CSS

Style using CSS in accordance to your application using the ability to set custom class names on components:

<TreeSelect classNames={{
  filterBox: '',
  filterInput: '',
  topContainer: '',
  tree: '',
  treeBranch: '',
  treeCheckbox: '',
  treeItem: '',
  treeItemLabel: '',
  valueBox: '',
  valueBoxActive: '',
  valueButton: '',
  valueItem: '',
}} />

In addition, there are some data attributes that can narrow the styles down:

  • data-level on a treeItem, treeItemLabel, and valueItem allows to style value items depending on how deep they were in the tree.

It is recommended to load the styles using css-loader with modules enabled.

The default styles will not be changed without bumping a major version number.

Content style

You can modify the content the component shows. In the simplest case, you can provide a string, but any React element will do.

<TreeSelect customContent={{
  noResults: <div>No results found! :-(</div>,
  title: <h1>Choose your poison</h1>,
}} />

Sticky sections

By default, the top level of the tree items will be sticky: they will stay on the screen if the user is scrolling through a long list.

To make all levels sticky, you need to provide a function that knows the heights of all individual items to correctly calculate the placement. For instance, if every label in your tree is 18 pixels high, use the following function:

<TreeSelect labelTop={n => n * 18} />

About Citrusbyte

Citrusbyte

This software is lovingly maintained and funded by Citrusbyte. At Citrusbyte, we specialize in solving difficult computer science problems for startups and the enterprise.

At Citrusbyte we believe in and support open source software.

Citrusbyte and the Citrusbyte logo are trademarks or registered trademarks of Citrusbyte, LLC.