Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move among different containers #47

Closed
yanielbf opened this issue Jun 6, 2019 · 1 comment
Closed

Move among different containers #47

yanielbf opened this issue Jun 6, 2019 · 1 comment
Labels
question Further information is requested

Comments

@yanielbf
Copy link

yanielbf commented Jun 6, 2019

Hi, guys I need move draggable items among different containers. When i do it the property addedIndex in dropResult params of onDrop is null. How fix this. My code:

import React, {Component} from "react";
import {Container, Draggable} from "react-smooth-dnd";
import {applyDrag} from "./utils";
import _ from "lodash"

class Grid extends Component {
    state = {
        items: [
            {value: 1, order: 2000},
            {value: 2, order: 4000},
            {value: 3, order: 6000},
            {value: 4, order: 8000},
            {value: 5, order: 10000}]
    }

    onDrop = (dropResult) => {
        const {removedIndex, addedIndex, payload} = dropResult;
        const {items} = this.state;
        const index = addedIndex + (removedIndex > addedIndex ? -1 : 1);
        let orderResult = 0;
        if (!items[addedIndex] || !items[addedIndex]['order']) {
            orderResult = 2000;
        }
        if (index < 0) {
            orderResult = items[addedIndex]['order'] - 100;
        } else if (index >= items.length) {
            orderResult = items[addedIndex]['order'] + 100;
        } else {
            if (removedIndex > addedIndex) {
                orderResult = (items[addedIndex]['order'] + items[addedIndex - 1]['order']) / 2;
            } else {
                orderResult = (items[addedIndex + 1]['order'] + items[addedIndex]['order']) / 2;
            }
        }

        let newItems = items.slice(0);
        newItems[removedIndex]['order'] = orderResult;
        newItems = _.orderBy(newItems, ['order'], ['asc']);
        this.setState({items: newItems})
        return orderResult;
    }

    render() {
        const {items} = this.state;
        return <div>
            <Container
                groupName="milestone"
                getChildPayload={index => index}
                onDrop={this.onDrop}>
                {items.map(item => {
                    return <Draggable key={item.value}>
                        {item.value}
                    </Draggable>;
                })}
            </Container>
            <Container
                groupName="milestone"
                getChildPayload={index => index}
                onDrop={this.onDrop}>
                {items.map(item => {
                    return <Draggable key={item.value}>
                        {item.value}
                    </Draggable>;
                })}
            </Container>
        </div>;
    }


}

export default Grid;
@kutlugsahin kutlugsahin added the question Further information is requested label Oct 24, 2019
@kutlugsahin
Copy link
Owner

addedIndex will be null for first container and not null for second container. And I see that the same list is used by both containers which will result in some issues. Maintain 2 lists for two containers and modify them according to the dropResults. See demo application codes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants