gpt4 book ai didi

reactjs - React DnD 拖动时显示整个列表

转载 作者:行者123 更新时间:2023-12-03 13:44:20 35 4
gpt4 key购买 nike

我正在尝试集成React DnD使用 Material UIListListItem并且,拖动时,整个列表显示为拖动的元素。我已尽我所能理解这些示例,这就是我所拥有的

import React, { Component, PropTypes } from 'react';
import { Random } from 'meteor/random';
import LocalizedComponent from '/client/components/LocalizedComponent';
// MUI
import { List, ListItem } from 'material-ui/List';
// ---
import { DragDropContext, DragSource, DropTarget } from 'react-dnd';
import { findDOMNode } from 'react-dom';

import HTML5Backend from 'react-dnd-html5-backend';


const itemSource = {
beginDrag(props) {
return {
id: props.id,
index: props.index
};
},
};

const itemTarget = {
hover(props, monitor, component) {
const dragIndex = monitor.getItem().index;
const hoverIndex = props.index;

// Don't replace items with themselves
if (dragIndex === hoverIndex) {
return;
}

// Determine rectangle on screen
const hoverBoundingRect = findDOMNode(component).getBoundingClientRect();

// Get vertical middle
const hoverMiddleY = (hoverBoundingRect.bottom - hoverBoundingRect.top) / 2;

// Determine mouse position
const clientOffset = monitor.getClientOffset();

// Get pixels to the top
const hoverClientY = clientOffset.y - hoverBoundingRect.top;

// Only perform the move when the mouse has crossed half of the items height
// When dragging downwards, only move when the cursor is below 50%
// When dragging upwards, only move when the cursor is above 50%

// Dragging downwards
if (dragIndex < hoverIndex && hoverClientY < hoverMiddleY) {
return;
}

// Dragging upwards
if (dragIndex > hoverIndex && hoverClientY > hoverMiddleY) {
return;
}

// Time to actually perform the action
props.onMoveItem(dragIndex, hoverIndex);

// Note: we're mutating the monitor item here!
// Generally it's better to avoid mutations,
// but it's good here for the sake of performance
// to avoid expensive index searches.
monitor.getItem().index = hoverIndex;
},
};


class SortableListComponent extends Component {

render() {
const { children, onMoveItem } = this.props;
let index = 0;

return (
<List>
{ React.Children.map(children, child => React.cloneElement(child, {
id: Random.id(),
index: index++,
onMoveItem: onMoveItem
})) }
</List>
);
}
}

SortableListComponent.propTypes = {
onMoveItem: PropTypes.func.isRequired
};


class SortableListItemComponent extends Component {

render() {
const {
id,
index,
isDragging,
connectDragSource,
connectDropTarget,
onMoveItem,
...other
} = this.props;
const opacity = 1; // isDragging ? 0 : 1;

return connectDragSource(connectDropTarget(
<div style={{ opacity }}>
<ListItem { ...other } disabled={ isDragging } />
</div>
));
}
}
SortableListItemComponent.propTypes = {
connectDragSource: PropTypes.func.isRequired,
connectDropTarget: PropTypes.func.isRequired,
id: PropTypes.any.isRequired,
index: PropTypes.number.isRequired,
isDragging: PropTypes.bool.isRequired,
onMoveItem: PropTypes.func.isRequired,
};


export const SortableList = DragDropContext(HTML5Backend)(SortableListComponent);

export const SortableListItem = DropTarget('SortableListItem', itemTarget, connect => ({
connectDropTarget: connect.dropTarget(),
}))(DragSource('SortableListItem', itemSource, (connect, monitor) => ({
connectDragSource: connect.dragSource(),
isDragging: monitor.isDragging(),
}))(SortableListItemComponent));

基本上,我用 List 代替 SortableList,用 ListItem 代替 SortableListItem,这就是我在拖动

enter image description here

我做错了什么?

编辑

例如,这是一个示例用法

<SortableList>
{ actions.map((action, index) => (
<SortableListItem id={ action.name } key={ index }
primaryText={ (index + 1) + '. ' + action.name }
onTouchTap={ this.handleActionEdit.bind(this, index) }
/>
)) }
</SortableList>

<SortableList>
{ actions.map((action, index) => (
<SortableListItem id={ action.name } key={ action.name }
primaryText={ (index + 1) + '. ' + action.name }
onTouchTap={ this.handleActionEdit.bind(this, index) }
/>
)) }
</SortableList>

等等

它不会改变任何事情。

最佳答案

我也遇到了类似的问题,发现这在 Chrome 上无法正常工作。请引用https://github.com/react-dnd/react-dnd/issues/832对于类似的问题。用户的以下评论帮助我解决了这个问题。

@kaiomagalhaes 在我的例子中,我遇到这个问题是因为行的子元素之一(单元格内容)实际上具有大于行高的高度,但被可见性隐藏:隐藏在CSS中。所以dragSource有行的宽度和隐藏控件的高度。

希望您会发现这有帮助。

关于reactjs - React DnD 拖动时显示整个列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42234575/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com