react-sortable-tree

Drag-and-drop sortable component for nested data and hierarchies

Github星跟踪图

React Sortable Tree

NPM version
NPM license
NPM total downloads
NPM monthly downloads
Build Status
Coverage Status
PRs Welcome

A React component for Drag-and-drop sortable representation of hierarchical data. Checkout the demo for a demonstration of some basic features. Checkout the storybook for advanced usage.

Table of Contents

Getting started

Install react-sortable-tree using npm.

# NPM
npm install react-sortable-tree --save

# YARN
yarn add react-sortable-tree

ES6 and CommonJS builds are available with each distribution.
For example:

// This only needs to be done once; probably during your application's bootstrapping process.
import 'react-sortable-tree/style.css';

// You can import the default tree with dnd context
import SortableTree from 'react-sortable-tree';

// Or you can import the tree without the dnd context as a named export. eg
import { SortableTreeWithoutDndContext as SortableTree } from 'react-sortable-tree';

// Importing from cjs (default)
import SortableTree from 'react-sortable-tree/dist/index.cjs.js';
import SortableTree from 'react-sortable-tree';

// Importing from esm
import SortableTree from 'react-sortable-tree/dist/index.esm.js';

Usage

import React, { Component } from 'react';
import SortableTree from 'react-sortable-tree';
import 'react-sortable-tree/style.css'; // This only needs to be imported once in your app

export default class Tree extends Component {
  constructor(props) {
    super(props);

    this.state = {
      treeData: [
        { title: 'Chicken', children: [{ title: 'Egg' }] },
        { title: 'Fish', children: [{ title: 'fingerline'}] }
      ],
    };
  }

  render() {
    return (
      <div style={{ height: 400 }}>
        <SortableTree
          treeData={this.state.treeData}
          onChange={treeData => this.setState({ treeData })}
        />
      </div>
    );
  }
}

Need a hand turning your flat data into nested tree data?
Want to perform add/remove operations on the tree data without creating your own recursive function?
Check out the helper functions exported from tree-data-utils.js.

Themes

Using the theme prop along with an imported theme module, you can easily override the default appearance with another standard one.

Browser Compatibility, Browser, Works?, :------, :-----, Chrome, Yes, Firefox, Yes, Safari, Yes, IE 11, Yes, ## Troubleshooting

If it throws "TypeError: fn is not a function" errors in production

This issue may be related to an ongoing incompatibility between UglifyJS and Webpack's behavior. See an explanation at create-react-app#2376.

The simplest way to mitigate this issue is by adding comparisons: false to your Uglify config as seen here: https://github.com/facebookincubator/create-react-app/pull/2379/files

If it doesn't work with other components that use react-dnd

react-dnd only allows for one DragDropContext at a time (see: https://github.com/gaearon/react-dnd/issues/186). To get around this, you can import the context-less tree component via SortableTreeWithoutDndContext.

// before
import SortableTree from 'react-sortable-tree';

// after
import { SortableTreeWithoutDndContext as SortableTree } from 'react-sortable-tree';

Contributing

Please read the Code of Conduct. I actively welcome pull requests :)

After cloning the repository and running yarn install inside, you can use the following commands to develop and build the project.

# Starts a webpack dev server that hosts a demo page with the component.
# It uses react-hot-loader so changes are reflected on save.
yarn start

# Start the storybook, which has several different examples to play with.
# Also hot-reloaded.
yarn run storybook

# Runs the library tests
yarn test

# Lints the code with eslint
yarn run lint

# Lints and builds the code, placing the result in the dist directory.
# This build is necessary to reflect changes if you're
#  `npm link`-ed to this repository from another local project.
yarn run build

Pull requests are welcome!

License

MIT

主要指标

概览
名称与所有者frontend-collective/react-sortable-tree
主编程语言JavaScript
编程语言JavaScript (语言数: 2)
平台
许可证MIT License
所有者活动
创建于2016-07-22 08:36:25
推送于2025-08-21 15:45:28
最后一次提交2023-05-03 09:59:44
发布数53
最新版本名称v2.8.0 (发布于 2020-08-10 00:56:10)
第一版名称v0.1.2 (发布于 2016-10-13 11:12:49)
用户参与
星数5k
关注者数60
派生数0.9k
提交数554
已启用问题?
问题数599
打开的问题数297
拉请求数125
打开的拉请求数49
关闭的拉请求数177
项目设置
已启用Wiki?
已存档?
是复刻?
已锁定?
是镜像?
是私有?