React-dnd: ๋™์ผํ•œ ๊ตฌ์„ฑ ์š”์†Œ์—์„œ DragSource ๋ฐ DropTarget ์‚ฌ์šฉํ•˜์ง€๋งŒ DropTarget์—์„œ ์‘๋‹ต ์—†์Œ

์— ๋งŒ๋“  2018๋…„ 04์›” 12์ผ  ยท  3์ฝ”๋ฉ˜ํŠธ  ยท  ์ถœ์ฒ˜: react-dnd/react-dnd

์ด๊ฒƒ์„ ๋” ๋งŽ์€ ๋„์›€๋ง ํฌ๋Ÿผ์œผ๋กœ ์‚ฌ์šฉํ•˜๊ฒŒ ๋˜์–ด ์ฃ„์†กํ•ฉ๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ๋งŽ์€ ๋…ธ๋ ฅ์—๋„ ๋ถˆ๊ตฌํ•˜๊ณ  ์ด ์‚ฌ์šฉ ์‚ฌ๋ก€์—์„œ DropTarget์œผ๋กœ๋ถ€ํ„ฐ ์–ด๋–ค ์‘๋‹ต๋„ ๋ฐ›์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.

๋“œ๋กญ ์ด๋ฒคํŠธ๋ฅผ ์Šค์Šค๋กœ ๋“ฑ๋กํ•  ์ˆ˜ ์žˆ๋Š” ์ผ๋ จ์˜ '์นดํ…Œ๊ณ ๋ฆฌ ์นด๋“œ'๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  ์‹ถ์Šต๋‹ˆ๋‹ค.

๋‚ด๊ฐ€ ์—ฌ๊ธฐ์—์„œ ๋ˆˆ์— ๋„๋Š” ๋ง์ฝ์„ ํ”ผ์šฐ๊ณ  ์žˆ์Šต๋‹ˆ๊นŒ?

DragSource ๊ธฐ๋Šฅ์ด ์ž‘๋™ํ•˜๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์ง€๋งŒ DropTarget ์‚ฌ์–‘ ๊ธฐ๋Šฅ์—์„œ ๊ธฐ๋กํ•˜๋Š” ์–ด๋–ค ๊ฒƒ๋„ ์ฝ˜์†”์— ํ‘œ์‹œ๋˜์ง€ ์•Š๊ณ  ์™„์ „ํžˆ ์ ์šฉ๋˜์ง€ ์•Š์€ ๊ฒƒ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

import React, { Component } from 'react'
import { DropTarget, DragSource } from 'react-dnd'
import _ from 'lodash'

export const ItemTypes = {
    CATEGORY: 'category-item'
}

class CategoryTreeFork extends Component {

    componentWillReceiveProps = (nextProps) => {
        console.log("Next cat props", nextProps)
    }

    render(){
        var { thisCat, childCat, level, dropEvent } = this.props
        const { connectDragSource, connectDropTarget, isDragging, isOver } = this.props

        return connectDragSource(
            <div className="category-tree-fork">
                <div className={`category-list-item level-${level}`}
                    style={{
                        opacity: isDragging ? 0.5 : 1,
                        backgroundColor: isOver ? "red" : "silver"
                    }}>
                    {thisCat.name}
                </div>
            </div>
        )   
    }   
}

/* get dragged */
const sourceSpec = { 
    beginDrag(props){
        return { category_id: props.thisCat.category_id }
    }   
}

const sourceCollect  = (connect, monitor) => {
    return {
        connectDragSource: connect.dragSource(),
        isDragging: monitor.isDragging()
    }
}


/* get dragged onto */
const targetSpec  = {
    drop(props, monitor, component) {
        const item = monitor.getItem()
        console.log("me or my friend?", item)
    },
    hover(props, monitor, component){
        console.log("yo what you hoverin there for homie?")
    }
}

const targetCollect = (connect, monitor) => {
    return {
        connectDropTarget: connect.dropTarget(),
        isOver: monitor.isOver()
    }
}


export default _.flow([
    DragSource(ItemTypes.CATEGORY, sourceSpec, sourceCollect),
    DropTarget(ItemTypes.CATEGORY, targetSpec, targetCollect)
])(CategoryTreeFork)

๊ฐ€์žฅ ์œ ์šฉํ•œ ๋Œ“๊ธ€

๊ฒ€์ƒ‰ ์—”์ง„์—์„œ ์˜ค๋Š” ์‚ฌ๋žŒ๋“ค์„ ์œ„ํ•ด _.flow ๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  ๋™์ผํ•œ ๊ตฌ์„ฑ ์š”์†Œ์—์„œ DragSource ๋ฐ DropTarget์„ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์ฐพ๋Š” ๋ฐฉ๋ฒ•์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

export default
    DragSource(typeA, specA, collectA)(
        DropTarget(typeB, specB, collectB)(
            Component));

๋ชจ๋“  3 ๋Œ“๊ธ€

DX๋Š” ์‹ ๊ฒฝ์“ฐ์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๋ Œ๋”์—์„œ connectDropTarget์„ ํ˜ธ์ถœํ•˜๋Š” ๊ฒƒ์„ ์žŠ์€ ์งํ›„์— ๊นจ๋‹ฌ์•˜์Šต๋‹ˆ๋‹ค. ์ˆ˜์ • ์‚ฌํ•ญ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

 return connectDropTarget(connectDragSource(
            <div className="category-tree-fork">
                <div className={`category-list-item level-${level}`}
                    style={{
                        opacity: isDragging ? 0.5 : 1,
                        backgroundColor: isOver ? "red" : "silver"
                    }}>
                    {thisCat.name}
                </div>
            </div>

์ด๊ฒƒ์€ ์ง€๊ธˆ ๋‚ด ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ–ˆ์Šต๋‹ˆ๋‹ค. "๋‹จ์ˆœํ•œ" ์ •๋ ฌ ๊ฐ€๋Šฅํ•œ ์˜ˆ์ œ๋ฅผ ๋”ฐ๋ฅด๊ณ  ์ด์ค‘ ๋ฐ์ฝ”๋ ˆ์ดํ„ฐ๋ฅผ ์ˆ˜ํ–‰ํ–ˆ์ง€๋งŒ connectDropTarget ๋ฅผ ์ ์šฉํ•˜์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค.

๊ฒ€์ƒ‰ ์—”์ง„์—์„œ ์˜ค๋Š” ์‚ฌ๋žŒ๋“ค์„ ์œ„ํ•ด _.flow ๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  ๋™์ผํ•œ ๊ตฌ์„ฑ ์š”์†Œ์—์„œ DragSource ๋ฐ DropTarget์„ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์ฐพ๋Š” ๋ฐฉ๋ฒ•์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

export default
    DragSource(typeA, specA, collectA)(
        DropTarget(typeB, specB, collectB)(
            Component));
์ด ํŽ˜์ด์ง€๊ฐ€ ๋„์›€์ด ๋˜์—ˆ๋‚˜์š”?
0 / 5 - 0 ๋“ฑ๊ธ‰