在 React 中實現(xiàn)拖拽功能-使用插件 react-beautiful-dnd

拖拽功能在平時開發(fā)中是很常見的典挑,這篇文章主要使用react-beautiful-dnd插件實現(xiàn)此功能琳骡。
非常好用煌往,附上GitHub地址:https://github.com/atlassian/react-beautiful-dnd

react-beautiful-dnd.png

廢話不多說,上代碼糠爬。

安裝及引入

// 1.引入
# yarn
yarn add react-beautiful-dnd

# npm
npm install react-beautiful-dnd --save
// 2.使用
import { DragDropContext, Droppable, Draggable } from "react-beautiful-dnd";

具體使用

// 樣式相關(guān) 代碼
const grid = 8;
// 垂直樣式
// const getItemStyle = (isDragging, draggableStyle) => ({
//     // some basic styles to make the items look a bit nicer
//     userSelect: "none",
//     padding: grid * 2,
//     margin: `0 0 ${grid}px 0`,
//
//     // change background colour if dragging
//     background: isDragging ? "lightgreen" : "grey",
//
//     // styles we need to apply on draggables
//     ...draggableStyle
// });
// const getListStyle = isDraggingOver => ({
//     background: isDraggingOver ? "lightblue" : "lightgrey",
//     padding: grid,
//     width: 250,
// });

// 水平樣式
const getItemStyle = (isDragging, draggableStyle) => ({
    // some basic styles to make the items look a bit nicer
    userSelect: 'none',
    padding: grid * 2,
    margin: `0 ${grid}px 0 0`,

    // change background colour if dragging
    background: isDragging ? 'lightgreen' : 'grey',

    // styles we need to apply on draggables
    ...draggableStyle,
});
const getListStyle = isDraggingOver => ({
    background: isDraggingOver ? 'lightblue' : 'lightgrey',
    display: 'flex',
    padding: grid,
    overflow: 'auto',
});

class App extends React.Component {
    constructor(props) {
        super(props)
        this.state = {
            items: [
                {id: 'item-0', content: 'hello'},
                {id: 'item-1', content: 'I'},
                {id: 'item-2', content: 'am'},
                {id: 'item-3', content: '卡'},
                {id: 'item-4', content: '特'},
                {id: 'item-5', content: '洛'},
            ]
        };
    }

    // a little function to help us with reordering the result
    reOrder = (list, startIndex, endIndex) => {
        const result = Array.from(list);
        const [removed] = result.splice(startIndex, 1);
        result.splice(endIndex, 0, removed);
        return result;
    };

    onDragEnd = (result) => {
        // dropped outside the list
        if (!result.destination) {
            return;
        }

        const items = this.reOrder(
            this.state.items,
            result.source.index,
            result.destination.index
        );

        this.setState({
            items
        });
    }

    render () {
        return (
            <div className="App">
                
                <DragDropContext onDragEnd={this.onDragEnd}>
                    <Droppable droppableId="droppable" direction="horizontal">
                        {(provided, snapshot) => (
                            <div
                                ref={provided.innerRef}
                                style={getListStyle(snapshot.isDraggingOver)}
                                {...provided.droppableProps}
                            >
                                {this.state.items.map((item, index) => (
                                    <Draggable key={item.id} draggableId={item.id} index={index}>
                                        {(provided, snapshot) => (
                                            <div
                                                ref={provided.innerRef}
                                                {...provided.draggableProps}
                                                {...provided.dragHandleProps}
                                                style={getItemStyle(
                                                    snapshot.isDragging,
                                                    provided.draggableProps.style
                                                )}
                                            >
                                                {item.content}
                                            </div>
                                        )}
                                    </Draggable>
                                ))}
                                {provided.placeholder}
                            </div>
                        )}
                    </Droppable>
                </DragDropContext>
                
            </div>
        );
    }
}

export default App;

注意寇荧,

<Droppable droppableId="droppable" direction="horizontal"> 

中的 direction 屬性可以控制是水平方向還是垂直方向,配合相關(guān) getItemStyle 和 getListStyle 的代碼执隧,可做到揩抡。

最后,效果展示

react-beautiful-dnd-demo.gif

后記
其他好用拖拽組件集合:
1.react-grid-layout https://github.com/STRML/react-grid-layout

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末镀琉,一起剝皮案震驚了整個濱河市峦嗤,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌屋摔,老刑警劉巖烁设,帶你破解...
    沈念sama閱讀 222,807評論 6 518
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異钓试,居然都是意外死亡装黑,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,284評論 3 399
  • 文/潘曉璐 我一進店門弓熏,熙熙樓的掌柜王于貴愁眉苦臉地迎上來恋谭,“玉大人,你說我怎么就攤上這事硝烂』穑” “怎么了铜幽?”我有些...
    開封第一講書人閱讀 169,589評論 0 363
  • 文/不壞的土叔 我叫張陵滞谢,是天一觀的道長串稀。 經(jīng)常有香客問我,道長狮杨,這世上最難降的妖魔是什么母截? 我笑而不...
    開封第一講書人閱讀 60,188評論 1 300
  • 正文 為了忘掉前任,我火速辦了婚禮橄教,結(jié)果婚禮上清寇,老公的妹妹穿的比我還像新娘。我一直安慰自己护蝶,他們只是感情好华烟,可當我...
    茶點故事閱讀 69,185評論 6 398
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著持灰,像睡著了一般盔夜。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上堤魁,一...
    開封第一講書人閱讀 52,785評論 1 314
  • 那天喂链,我揣著相機與錄音,去河邊找鬼妥泉。 笑死椭微,一個胖子當著我的面吹牛,可吹牛的內(nèi)容都是我干的盲链。 我是一名探鬼主播蝇率,決...
    沈念sama閱讀 41,220評論 3 423
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼刽沾!你這毒婦竟也來了瓢剿?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 40,167評論 0 277
  • 序言:老撾萬榮一對情侶失蹤悠轩,失蹤者是張志新(化名)和其女友劉穎间狂,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體火架,經(jīng)...
    沈念sama閱讀 46,698評論 1 320
  • 正文 獨居荒郊野嶺守林人離奇死亡鉴象,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,767評論 3 343
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了何鸡。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片纺弊。...
    茶點故事閱讀 40,912評論 1 353
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖骡男,靈堂內(nèi)的尸體忽然破棺而出淆游,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 36,572評論 5 351
  • 正文 年R本政府宣布犹菱,位于F島的核電站拾稳,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏腊脱。R本人自食惡果不足惜访得,卻給世界環(huán)境...
    茶點故事閱讀 42,254評論 3 336
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望陕凹。 院中可真熱鬧悍抑,春花似錦、人聲如沸杜耙。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,746評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽佑女。三九已至记靡,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間珊豹,已是汗流浹背簸呈。 一陣腳步聲響...
    開封第一講書人閱讀 33,859評論 1 274
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留店茶,地道東北人蜕便。 一個月前我還...
    沈念sama閱讀 49,359評論 3 379
  • 正文 我出身青樓,卻偏偏與公主長得像贩幻,于是被迫代替她去往敵國和親轿腺。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 45,922評論 2 361