React:如何使用可拖动方式创建拖动手柄?

huangapple go评论81阅读模式
英文:

React : How to create a Drag Handle with draggable?

问题

以下是翻译好的部分:

我无法找到如何使react组件只能在点击拖动按钮时才能拖动的答案(点击元素内的任何内容都不会触发拖动事件

以下是Material UIJSX代码:

<Box
    draggable={false}
    onDragStart={onDragStart(props.data)}
>

    {/* 标题 */}
    <div>可拖动元素 1</div>


    {/* 按钮:拖动手柄 */}
    <div className="test-drag-handle">

        <IconButton // MUI 按钮
            draggable
            onClick={(e) => {}}
            sx={{
                color: 'rgb(19, 140, 126)',
                caretColor: 'rgb(19, 140, 126)'
            }}
        >
            {/* 拖动手柄:图标 */}
            <Icon
                path={mdiArrowAll}
                size={'22px'}
                className=""
                style={{
                    color: 'rgb(19, 140, 126)',
                    caretColor: 'rgb(19, 140, 126)'
                }}
            />
        </IconButton>

    </div>


    {/* 按钮:编辑 */}
    <Box sx={{
        mr: 1
    }}>

		... 编辑按钮

    </Box>


    {/* 按钮:克隆 */}
    <div className="mr-4">

		... 克隆按钮

    </div>


    {/* 按钮:删除 */}
    	... 删除按钮

</Box>

看起来是这样的:
React:如何使用可拖动方式创建拖动手柄?

我希望只有在点击拖动按钮时才能使元素可拖动

点击元素内的任何内容都不会触发拖动事件,除非点击拖动按钮

React:如何使用可拖动方式创建拖动手柄?

我应该怎么做才能实现这个效果?

英文:

I am unable to find an answer on how to make a react component draggable only when the Drag Button is clicked (Clicking anything inside the element wont trigger the drag event)

Here is the Material UI JSX code:

<Box
draggable={false}
onDragStart={onDragStart(props.data)}
>
{/* Title */}
<div>Draggable Element 1</div>
{/* Button : Drag Handle */}
<div className="test-drag-handle">
<IconButton // MUI Button
draggable
onClick={(e) => {}}
sx={{
color: 'rgb(19, 140, 126)',
caretColor: 'rgb(19, 140, 126)'
}}
>
{/* Drag Handle: Icon */}
<Icon
path={mdiArrowAll}
size={'22px'}
className=""
style={{
color: 'rgb(19, 140, 126)',
caretColor: 'rgb(19, 140, 126)'
}}
/>
</IconButton>
</div>
{/* Button : Edit */}
<Box sx={{
mr: 1
}}>
... Edit Button
</Box>
{/* Button : Clone */}
<div className="mr-4">
... Clone Button
</div>
{/* Button : Delete */}
... Delete Button
</Box>

and it looks like this:
React:如何使用可拖动方式创建拖动手柄?

I want to make the element draggable only when the Drag Button is clicked

Clicked anything inside the element will not trigger the drag event, except the Drag Button

React:如何使用可拖动方式创建拖动手柄?

What should I do it work?

答案1

得分: 0

当按钮被点击时,需要更新状态。

const [disabled, setDisabled] = useState(false);

const toggleDraggable = () => {
    setDisabled(!disabled);
};

<Box
    draggable={disabled}
    onDragStart={onDragStart(props.data)}
>

    {/* 标题 */}
    <div>可拖动元素 1</div>

    {/* 按钮:拖动处理 */}
    <div className="test-drag-handle">

        <IconButton // MUI 按钮
            draggable
            onClick={(e) => {toggleDraggable();}}
            sx={{
                color: 'rgb(19, 140, 126)',
                caretColor: 'rgb(19, 140, 126)'
            }}
        >
            {/* 拖动处理:图标 */}
            <Icon
                path={mdiArrowAll}
                size={'22px'}
                className=""
                style={{
                    color: 'rgb(19, 140, 126)',
                    caretColor: 'rgb(19, 140, 126)'
                }}
            />
        </IconButton>

    </div>

    {/* 按钮:编辑 */}
    <Box sx={{
        mr: 1
    }}>

        ... 编辑按钮

    </Box>

    {/* 按钮:克隆 */}
    <div className="mr-4">

        ... 克隆按钮

    </div>

    {/* 按钮:删除 */}
        ... 删除按钮

</Box>
英文:

You need to update the state when the button was clicked.

const [disabled, setDisabled] = useState(false);
const toggleDraggable = () =&gt; {
setDisabled(!disabled);
};
&lt;Box
draggable={disabled}
onDragStart={onDragStart(props.data)}
&gt;
{/* Title */}
&lt;div&gt;Draggable Element 1&lt;/div&gt;
{/* Button : Drag Handle */}
&lt;div className=&quot;test-drag-handle&quot;&gt;
&lt;IconButton // MUI Button
draggable
onClick={(e) =&gt; {toggleDraggable();}}
sx={{
color: &#39;rgb(19, 140, 126)&#39;,
caretColor: &#39;rgb(19, 140, 126)&#39;
}}
&gt;
{/* Drag Handle: Icon */}
&lt;Icon
path={mdiArrowAll}
size={&#39;22px&#39;}
className=&quot;&quot;
style={{
color: &#39;rgb(19, 140, 126)&#39;,
caretColor: &#39;rgb(19, 140, 126)&#39;
}}
/&gt;
&lt;/IconButton&gt;
&lt;/div&gt;
{/* Button : Edit */}
&lt;Box sx={{
mr: 1
}}&gt;
... Edit Button
&lt;/Box&gt;
{/* Button : Clone */}
&lt;div className=&quot;mr-4&quot;&gt;
... Clone Button
&lt;/div&gt;
{/* Button : Delete */}
... Delete Button
&lt;/Box&gt;

huangapple
  • 本文由 发表于 2023年6月12日 13:50:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/76453911.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定