代码之家  ›  专栏  ›  技术社区  ›  31113

为地图中的每个元素连接dragsource(拖放:react dnd)

  •  0
  • 31113  · 技术社区  · 6 年前

    如何使每一个项目都是可行的? 作为 map() 函数返回我决定传递的新数组 connectDragSource 但我还是回到了 可下垂的阿雷 安装 可下拉每个项目 (形状)

    for..in ,请 forEach , for..of 也不能得到想要的结果

    有人解决了这个问题吗?

    import React, { Component } from "react";
    import { Segment, Icon } from "semantic-ui-react";
    import { DragSource } from "react-dnd";
    import ShapeItem from "./ShapeItem";
    
    class Shapes extends Component {
    
      displayShapes = (shapes, connectDragSource) =>
        shapes.length > 0 &&
        shapes.map((shape) =>
          connectDragSource(
            <div key={shape.id}>
              <Segment>
                <Icon name={shape.name} size={shape.size} color={shape.color} />
              </Segment>
            </div>
          )
        );
    
      render() {
        const { shapes, connectDragSource} = this.props;
        return (
          <div>
            {this.displayShapes(shapes, connectDragSource)}
          </div>
        );
      }
    }
    
    const spec = {
      beginDrag(props) {
        return {
          shapeId: props.shapes.id
        };
      }
    };
    
    const collect = (connect, monitor) => ({
      connectDragSource: connect.dragSource(spec),
      isDragging: monitor.isDragging()
    });
    
    export default DragSource("shape", spec, collect)(Shapes);
    

    至于文件 http://react-dnd.github.io 只找到这个: 将元素作为可拖动节点进行反应。为此,请更换 element 具有 this.props.connectDragSource(element) 在渲染函数中。

    1 回复  |  直到 6 年前
        1
  •  1
  •   Ishwor Timilsina    6 年前

    您可能需要创建一个单独的组件来渲染每个项并使其成为拖动源。您还需要从规范中的candrag函数返回true。

    免责声明:我没有测试下面的代码。

    夏普

    import React, { Component } from "react";
    import Item from './item.js';
    
    class Shapes extends Component {    
        render() {
            const { shapes } = this.props;
            return (
                <div>
                    {
                        shapes.length > 0 && 
                        shapes.map((shape) => <Item key={shape.id} shape={shape} />)
                    }
                </div>
            );
        }
    }
    
    export default Shapes;
    

    项目管理系统

    import React, { Component } from "react";
    import { Segment, Icon } from "semantic-ui-react";
    import { DragSource } from "react-dnd";
    
    class Item extends Component {    
        render() {
            const { shape, connectDragSource} = this.props;
            return connectDragSource(
                <div>
                     <Segment>
                         <Icon name={shape.name} size={shape.size} color={shape.color} />
                     </Segment>
                </div>
            )
        }
    }
    
    const spec = {
        beginDrag(props) {
            return {
                shapeId: props.shapes.id
            };
        },
        canDrag(props, monitor) {
            return true;
        },
        endDrag(props, monitor) {
            console.log("You dropped .... into " + monitor.getDropResult());
        }
    };
    
    const collect = (connect, monitor) => ({
      connectDragSource: connect.dragSource(spec),
      isDragging: monitor.isDragging()
    });
    
    export default DragSource("shape", spec, collect)(Item);