简体   繁体   English

(已解决)从流中移除 Draggable React 组件而不跳转的最佳方法?

[英](Resolved )Best way to remove Draggable React component from flow without jumping?

I have Note components that are rendered using notes.map() , each with position: static .我有使用notes.map()呈现的 Note 组件,每个组件都有position: static The notes are draggable using react-draggable npm module.使用react-draggable npm 模块可以拖动笔记。

The functionality I'm trying to achieve is when a note is deleted, to not affect the position of notes that have been dragged by the user.我想要实现的功能是在删除笔记时,不影响用户拖动的笔记的位置。 I've attempted to set position: absolute for notes that have been dragged.我试图为已拖动的音符设置position: absolute However, this causes the note to 'jump' in position once (happens when removed from flow).但是,这会导致音符“跳跃”一次(从流中移除时发生)。

-- Initial State: -- 初始状态: 初始状态 -- After first drag attempt, test note jumps on top of other note: -- 在第一次拖动尝试后,测试音符跳到其他音符的​​顶部: 第一次拖动尝试后,组件跳转 -- Able to drag normally after first attempt: -- 第一次尝试后可以正常拖动: 第一次尝试后能够正常拖动

I've included relevant code for Notes.jsx component:我已经包含了 Notes.jsx 组件的相关代码:

function Note(props) {
const [dragDisabled, setDragDisabled] = useState(true);
const [beenDragged, setBeenDragged] = useState(props.beenDragged);
const [position, setPosition] = useState({ xPos: props.xPos, yPos: props.yPos });

// Drag Note Functions
function handleClick() {
    setDragDisabled(prevValue => {
        return !prevValue;
    })
}

function firstDrag(event) {
    if (!beenDragged) {
        axios.post("api/note/beenDragged", {id: props.id})
            .then(setBeenDragged(true));
    }
}

function finishDrag(event, data) {
    setPosition({ xPos: data.x, yPos: data.y });
    
}

useEffect(() => {
    axios.post("/api/note/updateposition", {position, id: props.id });
}, [position]);

return <Draggable
    disabled={dragDisabled}
    onStart={firstDrag}
    onStop={finishDrag}
    defaultPosition={{ x: props.xPos, y: props.yPos }}
    // position={location}
>
    <div className='note' style={{position: beenDragged ? 'absolute' : 'static'}}>

        <h1>{props.title}</h1>
        <p>{props.content}</p>

        <button onClick={handleClick}>
            {dragDisabled ? <LockIcon /> : <LockOpenIcon />}
        </button>
        <EditPopup title={props.title} content={props.content} editNote={editNote} />
        <DeletePopup deleteNote={deleteNote} />

    </div>
</Draggable>
}

and for my CSS styling对于我的 CSS 样式

.note {
background: #fff;
/* background-image: url("https://www.transparenttextures.com/patterns/notebook-dark.png"); */
background-image: url("https://www.transparenttextures.com/patterns/lined-paper-2.png");
border-radius: 7px;
box-shadow: 0 2px 5px rgb(120, 150, 179);
padding: 10px;
width: 240px;
margin: 16px;
float: left;
}

Relevant code for how App.jsx renders the notes: App.jsx 如何呈现注释的相关代码:

function App() {
const [notes, setNotes] = useState([]);

return (
    <div id="bootstrap-override">
        {notes.map((note) => {
            return <Note
                key={note._id}
                id={note._id}
                title={note.title}
                content={note.content}
                xPos={note.xPos}
                yPos={note.yPos}
                beenDragged={note.beenDragged}
                deleteNote={deleteNote}
                editNote={editNote}
            />
        })}
    </div>);
}

Any help is much appreciated, thank you!非常感谢任何帮助,谢谢!

I solved the issue not directly, but rather using a different approach to get the result I was looking for.我不是直接解决了这个问题,而是使用不同的方法来获得我想要的结果。

To summarize, I used position: absolute for all notes, and created them at random x,y coordinates within a boundary.总而言之,我对所有音符都使用了position: absolute ,并在边界内的随机 x,y 坐标处创建了它们。 This way deleted notes would not affect the position of existing notes.这样删除的笔记不会影响现有笔记的位置。

Hope this helps anyone with similar issues!希望这可以帮助任何有类似问题的人!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM