0

我正在研究 React 和 DND,我正在尝试一个示例。例如,我有两列可以在它们之间来回拖放卡片,效果很好,现在我希望第二列包含不超过 4 个项目,但可以拖出项目。在我读到的文档中,这是通过将 ' isDropDisabled' 设置为true. 那我确实不能再拖到这个专栏了,我可以离开了。现在我尝试用this.state.isUnlocked变量来影响它。因此,如果有超过 4 个,则此值为 true,否则此值为 false。只有代码不响应this.state.isUnlocked。只有当我直接输入它但它不再是可变的。有没有人可以帮我解决这个问题?

到目前为止,这是我的代码:

import React, { Component } from 'react';
import ReactDOM from 'react-dom';
import { DragDropContext, Droppable, Draggable } from 'react-beautiful-dnd';
import { Row, Col, Card, CardHeader, CardBody, Button } from "shards-react";

// fake data generator
const getItems = (count, offset = 0) =>
    Array.from({ length: count }, (v, k) => k).map(k => ({
        id: `item-${k + offset}`,
        content: `item ${k + offset}`
    }));

// 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;
};

/**
 * Moves an item from one list to another list.
 */
const move = (source, destination, droppableSource, droppableDestination) => {
    const sourceClone = Array.from(source);
    const destClone = Array.from(destination);
    const [removed] = sourceClone.splice(droppableSource.index, 1);

    destClone.splice(droppableDestination.index, 0, removed);

    const result = {};
    result[droppableSource.droppableId] = sourceClone;
    result[droppableDestination.droppableId] = destClone;

    return result;
};

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
});

class qSortOverview extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            items: getItems(10),
            selected: getItems(2, 10),
            iUnlocked: false,
        };
        this.canvasRef = React.createRef();
      }


    /**
     * A semi-generic way to handle multiple lists. Matches
     * the IDs of the droppable container to the names of the
     * source arrays stored in the state.
     */
    id2List = {
        droppable: 'items',
        droppable2: 'selected'
    };

    getList = id => this.state[this.id2List[id]];

    onDragEnd = result => {
        const { source, destination } = result;

        // dropped outside the list
        if (!destination) {
            return;
        }

        if (source.droppableId === destination.droppableId) {
            const items = reorder(
                this.getList(source.droppableId),
                source.index,
                destination.index
            );

            let state = { items };

            if (source.droppableId === 'droppable2') {
                state = { selected: items };

            }

            this.setState(state);

        } else {
            const result = move(
                this.getList(source.droppableId),
                this.getList(destination.droppableId),
                source,
                destination
            );

            this.setState({
                items: result.droppable,
                selected: result.droppable2
            });
            //Linker rij
            console.log(this.state.items);
            if(this.state.items.length > 4){
                console.log('to much items');
            }
            //Rechter rij
            console.log(this.state.selected);
            if(this.state.selected.length > 4){
                this.setState({
                    isUnlocked: true,
                })

            }
        }
    };



    // Normally you would want to split things out into separate components.
    // But in this example everything is just done in one place for simplicity
    render() {
        return (
            <DragDropContext onDragEnd={this.onDragEnd}>
                <Row>
                <Col lg="6" md="6" sm="6" className="mb-4">
                <Droppable droppableId="droppable" >
                    {(provided, snapshot) => (
                        <div
                            ref={provided.innerRef}
                            style={getListStyle(snapshot.isDraggingOver)}>
                            {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>
                </Col>
                <Col lg="6 " md="6" sm="6" className="mb-4">
                <Droppable droppableId="droppable2" isDropDisabled={this.state.isUnlocked}>
                    {(provided, snapshot) => (
                        <div
                            ref={provided.innerRef}
                            style={getListStyle(snapshot.isDraggingOver)}>
                            {this.state.selected.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>
                </Col>
                </Row>
            </DragDropContext>
        );
    }
};

export default qSortOverview;

目前右列包含 2 个项目,当有 4 个项目时,应该不再可以添加项目,只需将其删除即可。

4

1 回答 1

0

您需要更改此行:

if (this.state.selected.length > 4) {
  this.setState({
    isUnlocked: true
  });
}

if (this.state.selected.length > 4) {
  this.setState({
    isUnlocked: true
  });
} else {
  this.setState({
    isUnlocked: false
  });
}
于 2020-12-26T14:57:40.723 回答