react-sortable-hoc README

The project's README from github.com/clauderic/react-sortable-hoc. As one markdown file: README.md.

Warning

This library is no longer actively maintained. It will continue to receive critical security updates, but there are no new features planned. In future versions of React, the findDOMNode method will be deprecated. This method is a critical piece of the architecture of react-sortable-hoc, and the library will stop working in the future when that method is removed from react-dom.

All development efforts have been redirected towards @dnd-kit. It provides feature parity, built with a modern and extensible architecture, supports complex use-cases and has accessibility features built-in. New consumers are strongly encouraged to adopt @dnd-kit instead of adopting react-sortable-hoc.

Visit @dnd-kit github repository

React Sortable HOC

A set of higher-order components to turn any list into an animated, accessible and touch-friendly sortable list

npm version npm downloads license Gitter gzip size

Examples available here: http://clauderic.github.io/react-sortable-hoc/

Features

Installation

Using npm:

$ npm install react-sortable-hoc --save

Then, using a module bundler that supports either CommonJS or ES2015 modules, such as webpack:

// Using an ES6 transpiler like Babel
import {SortableContainer, SortableElement} from 'react-sortable-hoc';

// Not using an ES6 transpiler
var Sortable = require('react-sortable-hoc');
var SortableContainer = Sortable.SortableContainer;
var SortableElement = Sortable.SortableElement;

Alternatively, an UMD build is also available:

<script src="react-sortable-hoc/dist/react-sortable-hoc.umd.js"></script>

Usage

Basic Example

import React, {Component} from 'react';
import {render} from 'react-dom';
import {SortableContainer, SortableElement} from 'react-sortable-hoc';
import arrayMove from 'array-move';

const SortableItem = SortableElement(({value}) => <li>{value}</li>);

const SortableList = SortableContainer(({items}) => {
  return (
    <ul>
      {items.map((value, index) => (
        <SortableItem key={`item-${value}`} index={index} value={value} />
      ))}
    </ul>
  );
});

class SortableComponent extends Component {
  state = {
    items: ['Item 1', 'Item 2', 'Item 3', 'Item 4', 'Item 5', 'Item 6'],
  };
  onSortEnd = ({oldIndex, newIndex}) => {
    this.setState(({items}) => ({
      items: arrayMove(items, oldIndex, newIndex),
    }));
  };
  render() {
    return <SortableList items={this.state.items} onSortEnd={this.onSortEnd} />;
  }
}

render(<SortableComponent />, document.getElementById('root'));

That's it! React Sortable does not come with any styles by default, since it's meant to enhance your existing components.

More code examples are available here.

Why should I use this?

There are already a number of great Drag & Drop libraries out there (for instance, react-dnd is fantastic). If those libraries fit your needs, you should definitely give them a try first. However, most of those libraries rely on the HTML5 Drag & Drop API, which has some severe limitations. For instance, things rapidly become tricky if you need to support touch devices, if you need to lock dragging to an axis, or want to animate the nodes as they're being sorted. React Sortable HOC aims to provide a simple set of higher-order components to fill those gaps. If you're looking for a dead-simple, mobile-friendly way to add sortable functionality to your lists, then you're in the right place.

Prop Types

SortableContainer HOC

Property Type Default Description
axis String y Items can be sorted horizontally, vertically or in a grid. Possible values: x, y or xy
lockAxis String If you'd like, you can lock movement to an axis while sorting. This is not something that is possible with HTML5 Drag & Drop. Possible values: x or y.
helperClass String You can provide a class you'd like to add to the sortable helper to add some styles to it
transitionDuration Number 300 The duration of the transition when elements shift positions. Set this to 0 if you'd like to disable transitions
keyboardSortingTransitionDuration Number transitionDuration The duration of the transition when the helper is shifted during keyboard sorting. Set this to 0 if you'd like to disable transitions for the keyboard sorting helper. Defaults to the value set for transitionDuration if undefined
keyCodes Array {
  lift: [32],
  drop: [32],
  cancel: [27],
  up: [38, 37],
  down: [40, 39]
}
An object containing an array of keycodes for each keyboard-accessible action.
pressDelay Number 0 If you'd like elements to only become sortable after being pressed for a certain time, change this property. A good sensible default value for mobile is 200. Cannot be used in conjunction with the distance prop.
pressThreshold Number 5 Number of pixels of movement to tolerate before ignoring a press event.
distance Number 0 If you'd like elements to only become sortable after being dragged a certain number of pixels. Cannot be used in conjunction with the pressDelay prop.
shouldCancelStart Function Function This function is invoked before sorting begins, and can be used to programatically cancel sorting before it begins. By default, it will cancel sorting if the event target is either an input, textarea, select, option, or button.
updateBeforeSortStart Function This function is invoked before sorting begins. It can return a promise, allowing you to run asynchronous updates (such as setState) before sorting begins. function({node, index, collection, isKeySorting}, event)
onSortStart Function Callback that is invoked when sorting begins. function({node, index, collection, isKeySorting}, event)
onSortMove Function Callback that is invoked during sorting as the cursor moves. function(event)
onSortOver Function Callback that is invoked when moving over an item. function({index, oldIndex, newIndex, collection, isKeySorting}, e)
onSortEnd Function Callback that is invoked when sorting ends. function({oldIndex, newIndex, collection, isKeySorting}, e)
useDragHandle Boolean false If you're using the SortableHandle HOC, set this to true
useWindowAsScrollContainer Boolean false If you want, you can set the window as the scrolling container
hideSortableGhost Boolean true Whether to auto-hide the ghost element. By default, as a convenience, React Sortable List will automatically hide the element that is currently being sorted. Set this to false if you would like to apply your own styling.
lockToContainerEdges Boolean false You can lock

react-sortable-hoc (clauderic/react-sortable-hoc)

Searching Database…

这是什么?一套 React 高阶组件(HOC),可以把任意列表变成带动画、无障碍、支持触屏的可排序列表——横排、竖排或网格布局都可以。
它是一个开箱即用的排序组件,还是包装我已有的组件?它是一个库,不是拿来即用的组件:你需要用高阶组件包装现有的列表组件,它本身不带任何样式——它的定位就是增强你已有的东西。
怎么把它加到我的项目里?用 npm install react-sortable-hoc --save 安装,然后通过支持 CommonJS 或 ES2015 模块的打包工具(比如 webpack)引入;也有独立的 UMD 构建版本可用。
这个库现在还能放心用来做开发吗?开发已经停止:它只接收关键安全更新,而且依赖 findDOMNode——未来的 React 版本会移除这个 API,到那时这个库就无法使用了。
它能在哪里运行——桌面浏览器、手机,还是都行?只要 React 能运行,它就能在浏览器里运行。开箱即支持触屏操作,README 也把它定位成一种极简、对手机友好的列表排序方案。
用户能用键盘来调整顺序吗?可以——键盘排序是内置的无障碍功能,而且按键可以自定义配置:空格键负责拿起和放下,Escape 取消,方向键移动条目。
它支持 react-virtualized 这类虚拟列表吗?支持,它可以配合 react-virtualized、react-tiny-virtual-list 和 react-infinite 等虚拟化库一起使用。
为什么不直接用 HTML5 Drag & Drop API 或 react-dnd?作者说大多数拖拽库都依赖 HTML5 Drag & Drop API,而这个 API 在触屏设备、锁定拖动轴和动画排序这些场景下很麻烦;react-sortable-hoc 就是为了填补这些不足而做的。
我要开一个新项目——作者有什么建议?用 @dnd-kit,也就是同一位作者推出的继任项目:功能齐平,架构现代且易于扩展,支持复杂的使用场景,并内置无障碍支持。作者强烈建议新项目直接采用它。
Creator
clauderic
Size
21.8 MB
Stars
10.9K
Updated
CommitMay 26
Age
10y
Commits · 90 Days