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

Sort (create time by default, and draggable) #5

Open
Tracked by #2
aahei opened this issue Jan 25, 2024 · 1 comment
Open
Tracked by #2

Sort (create time by default, and draggable) #5

aahei opened this issue Jan 25, 2024 · 1 comment

Comments

@aahei
Copy link
Collaborator

aahei commented Jan 25, 2024

No description provided.

@aahei aahei mentioned this issue Jan 25, 2024
21 tasks
@aahei
Copy link
Collaborator Author

aahei commented Jan 25, 2024

import { useState } from "react";
import { closestCenter, DndContext } from "@dnd-kit/core";
import {
    arrayMove,
    SortableContext,
    useSortable,
    verticalListSortingStrategy,
} from "@dnd-kit/sortable";
import { CSS } from "@dnd-kit/utilities";


const data = [
    // 数据样本
    {
        id: 1,
        name: "Samaria",
    },
    {
        id: 2,
        name: "Gauthier",
    },
    {
        id: 3,
        name: "Mellisa",
    },
    {
        id: 4,
        name: "Arabela",
    },
    {
        id: 5,
        name: "Devon",
    },
    {
        id: 6,
        name: "Stacee",
    },
    {
        id: 7,
        name: "Federica",
    },
    {
        id: 8,
        name: "Jecho",
    },
    {
        id: 9,
        name: "Alasteir",
    },
    {
        id: 10,
        name: "Elston",
    },
];

const SortableItem = ({ user }) => {
    //每一行的小Item,可以用改用:
    // <ul><li></li></ul>
    //结构,也可以使用其他制表方法

    const {
        attributes,
        listeners,
        setNodeRef,
        transform,
        transition,
    } = useSortable({ id: user.id });

    const style = {
        transition,
        transform: CSS.Transform.toString(transform),
        // 以下CSS都可以更改
        padding: "10px",
        border: "1px solid black",
        fontSize: "18px",
        width: "400px",
    };

    return (
        <div
            ref={setNodeRef}
            style={style}
            {...attributes}
            {...listeners}
            className="user"
        >
            {user.name}
        </div>
    );
};

const SortedListDnd = () => {
    //Draggable Item的Container

    const [users, setUsers] = useState(data);

    const onDragEnd = (event) => {
        const { active, over } = event;
        if (active.id === over.id) {
            return;
        }
        setUsers((users) => {
            const oldIndex = users.findIndex((user) => user.id === active.id);
            const newIndex = users.findIndex((user) => user.id === over.id);
            return arrayMove(users, oldIndex, newIndex);
        });
    };

    const style = {
        // 以下CSS都可以更改
        display: "flex",
        flexDirection: "column",
        alignItems: "center",
    };

    return (
        <div className="users" style={style}>
            <div>
                <DndContext collisionDetection={closestCenter} onDragEnd={onDragEnd}>
                    <SortableContext items={users} strategy={verticalListSortingStrategy}>
                        {users.map((user) => (
                            <SortableItem key={user.id} user={user} />
                        ))}
                    </SortableContext>
                </DndContext>
            </div>
        </div>
    );
};
export default SortedListDnd;

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

No branches or pull requests

1 participant