1.0.4 • Published 7 years ago

react-treeviewselect v1.0.4

Weekly downloads
3
License
MIT
Repository
github
Last release
7 years ago

react-treeviewselect

A react based select control where the drop down is a tree view.

React/JS is not my first language, so open to suggestions for improvements as I have not seen a useful version of this control elsewhere, so I want this to be robust.

Features

  • Attempts to be WAI-ARIA compliant compliant although not thoroughly tested. Support for ARIA attributes and keyboard interactions. Issues welcome
  • Mobile friendly
  • Full control over item rendering.
  • Can use with any structure, as it uses callbacks for composing tree.
  • Typescript typings.

Installation

yarn add react-treeviewselect

or

npm install react-treeviewselect --save

Basic Usage

import { TreeViewSelect } from "react-treeviewselect";

function renderManagerLink(manager) {
    return <span
        key={manager.id}
    >
        {manager.name}
    </span>;
}

function renderSelectedManager(manager) {
    return <div className="d-inline-block">{manager.name}</div>;
}

function getChildren(item) {
    return item.reports;
}

function getParent(item) {
    return item.manager;
}

function getKey(item) {
    return item.id;
}

function onSelectedItemChange(item: IManager) {
    console.log(item.name);
}

class Example extends React.Component {
  constructor(props) {
        super(props);

        this.state = {};
        this.onSelectedItemChange = this.onSelectedItemChange.bind(this);
    }

  render() {

    return (
      <TreeViewSelect
                        style={{ width: "20rem" }}
                        defaultCollapsed={1}
                        renderSelectedItem={renderSelectedManager}
                        selectedItem={manager}
                        item={hierarchy.manager}
                        getChildren={getChildren}
                        getParent={getParent}
                        getKey={getKey}
                        renderItem={renderManagerLink}
                        onSelectedItemChange={onSelectedItemChange}
                    />
    );
  }
}

Props

PropTypeRequiredDescription
classNameStringSet a class name for outer TreeViewSelect element.
styleObjectSets a style for TreeViewSelect, can be useful to set a fixed width.
defaultCollapsedNumberUse this to define what level should default to collapsed. Can be useful for big trees to keep just the first few levels open by default.
renderSelectedItemFunctionHow do we render the selected item.
selectedItemObjectCurrently selected item.
itemObjectRoot node of the tree.
getChildrenFunctionUsed on a node to get the children. This allows a flexible data structure as input.
getParentFunctionGet parent for an item. This is used to make sure that selected items are not collapsed.
getKeyFunctionGet the key for a node. Used for item equivalency as well as for React key.
renderItemFunctionUsed to render an item in the treeview.
onSelectedItemChangeFunctionCallback when the selected item changes.

License

MIT

1.0.4

7 years ago

1.0.3

7 years ago

1.0.2

7 years ago

1.0.1

7 years ago

1.0.0

7 years ago