reactjs - 从react-beautiful-dnd水平列表中删除单个项目

标签 reactjs drag-and-drop react-redux

我有react-beautiful-dnd水平多个列表(6行,每行都有相同的项目),每个列表中都有相同的项目。 我想从每个列表中删除单个选定的项目,但只要有一个带有 onClick 的按钮组件就会在渲染列表本身时触发 onClick 。如何配置列表,以便在单击关闭/删除 (x) 按钮时从该列表中删除单个项目?

下面是我的代码,

    import React, { Component } from 'react';
    import { DragDropContext, Droppable, Draggable } from 'react-beautiful-dnd';
    import {Button, Icon} from 'semantic-ui-react'

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

    const grid = 12;
    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' : 'lightgray',

    // styles we need to apply on draggables
    ...draggableStyle,
    });

    const getListStyle = isDraggingOver => ({
    background: isDraggingOver ? 'lightblue' : 'white',
    display: 'flex',
    padding: grid,
    overflow: 'auto',
    });

    class DragAndDrop extends Component {
    constructor(props) {
        super(props);
        this.state = {
        items: this.props.uniqueEntries
        };
        this.onDragEnd = this.onDragEnd.bind(this)
        this.removeSubject = this.removeSubject.bind(this)
    }

    onDragEnd(result) {
        // dropped outside the list
        if (!result.destination) {
        return;
        }
        const items = reorder(
        this.state.items,
        result.source.index,
        result.destination.index
        );

        this.setState({
        items,
        });
    }

    componentWillReceiveProps(newProps){
        this.setState({
        items : newProps.uniqueEntries
        })

    }

    removeItem = (index) => {
        this.state.items.splice(index, 1)
    }

    render() {
        return (
        <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
                        )}
                        >
                        <Button icon size='mini'
                        style={{backgroundColor : 'lightgray' ,padding: 
                        '0', float: 'right'}}
                        onClick = {this.removeItem(index)}>
                        <Icon name='close' />
                        </Button>
                        {item.name}
                        </div>
                    )}
                    </Draggable>
                ))}
                {provided.placeholder}
                </div>
            )}
            </Droppable>
        </DragDropContext>
        );
    }
   }

    export default DragAndDrop

最佳答案

此错误的注释可能会影响您以后和 future 的查看者。 😋

你不应该直接改变状态。它会有一些副作用,或者根本没有副作用。

我还建议您生成唯一的 ID,因为如果您创建更多列表,它也会产生一些意想不到的结果,即使 react-beautiful-dnd 也不会注意到。

如果您想更新或删除状态数据,请使用setState()。首先使用副本修改现有数据。然后分配新值。

removeItem(index) {

// do magic here to filter out the unwanted element

// Update via 'setState'
        this.setState({
            items : newModifiedItems
        })
}

例如我的试用方法如下:

removeItem(e) {
        e.preventDefault();

        // give your single list a className so you can select them all
        const sourceList = document.querySelectorAll(".list-name");

        const arrList= Array.from(sourceList);

        // make shallow copy of your state data
        const newItems = Array.from(this.state.items);

        // Find index element from whole list Arr by traversing the DOM
        const removeItemIndex = arrList.indexOf(e.target.parentElement);

        // Remove it
        newItems.splice(removeItemIndex, 1);

        this.setState({
            items: newItems 
        })

    }

关于reactjs - 从react-beautiful-dnd水平列表中删除单个项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50612683/

相关文章:

javascript - 如何从 HTML 外部拖放 iframe?

python - 拖动 Tkinter 的窗口句柄?

javascript - 覆盖 redux-form-validators 中的默认消息

javascript - "Error watching file for changes: EMFILE"当初始化一个 react-native 项目时

reactjs - 无法将引用转发到组件 TypeScript 错误

javascript - token '&&' 在此版本中不是有效的语句分隔符

javascript - 如何在reactjs中将子项追加到光标位置

javascript - 这么多括号。我真的无法理解所有这些括号

performance - 如何使用 redux + Normalizr 减少渲染

javascript - react 和 Redux : Dispatching action not working