日期排序react_react-sortable - 一个可排序的React列表组件

React Sortable

68747470733a2f2f64617669642d646d2e6f72672f64616e69656c73746f636b732f72656163742d736f727461626c652e73766768747470733a2f2f696d672e736869656c64732e696f2f6e706d2f762f72656163742d736f727461626c652e73766768747470733a2f2f696d672e736869656c64732e696f2f6e706d2f64742f72656163742d736f727461626c652e7376673f6d61784167653d32353932303030

Higher-order component for creating sortable lists with minimalistic implementation and without polyfills. Using just React.js and HTML5 DragEvent interface.

Mainly tested in latest stable Webkit, Firefox and IE.

Check out demo and source.

Installation

To install a stable release use:

npm i react-sortable --save

Example

First import the necessary dependencies.

import React from 'react';

import ReactDOM from 'react-dom';

import { sortable } from 'react-sortable';

Then create a component for the single item of the list.

class Item extends React.Component {

render() {

return (

{this.props.children}

)

}

}

var SortableItem = sortable(Item);

And create component for the whole list, which will be the main component.

class SortableList extends React.Component {

state = {

items: this.props.items

};

onSortItems = (items) => {

this.setState({

items: items

});

}

render() {

const { items } = this.state;

var listItems = items.map((item, i) => {

return (

key={i}

onSortItems={this.onSortItems}

items={items}

sortId={i}>{item}

);

});

return (

{listItems}

)

}

};

Now you can pass a list of items to the main component and render the whole result.

var items = [

"Gold",

"Crimson",

"Hotpink",

"Blueviolet",

"Cornflowerblue",

"Skyblue",

"Lightblue",

"Aquamarine",

"Burlywood"

]

ReactDOM.render(

,

document.body

);

You can see this simple working demo in the ./example folder. For visual styling, you can add className of your choice.

How it works

Component will automatically attach the necessary drag event handlers.

It expects the following properties to be defined:

key (position of item in virtaul dom recommendation)

onSortItems (function called when an item is moved - dispatching if Redux action)

items (array of items to be sorted)

sortId (index of item in array)

Differences from react-dnd

fewer lines of code, easier to understand and modify

can handle both horizontal and vertical dragging

code is documented and covered with unit tests

Touch support

Unfortunately, at the moment there is no support of this interface in mobile browsers.

Purpose of this repo

This repository was published back in 2014 and was pretty much the very first implementation of drag and drop sortable list for React.js. Nowadays since there are other repositories which are well maintained (react-beautiful-dnd, react-sortable-hoc), I can recommend to use some of them in your project. This repository is now rather a showcase of what can be done just with simple React.js component and bare HTML5 API, having as few lines of code as possible. It can serve as inspiration for somebody who would like to reimplement this functionality from scratch.

Mainteners

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值