斗转昇移 發表於 2020-4-23 13:14:00

React拖拽组件react-sortable-hoc给子组件添加onClick失效问题

<p>实现使用&nbsp;react-sortable-hoc&nbsp;插件</p>
<p>基本使用先见官网:&nbsp;https://clauderic.github.io/react-sortable-hoc/#/basic-configuration/basic-usage?_k=0ronzp</p>
<p>github地址:https://github.com/clauderic/react-sortable-hoc/blob/master/examples/basic.js</p>
<p>&nbsp;点击事件绑定不上的解决方案:https://github.com/clauderic/react-sortable-hoc/issues/206</p>
<p>原因:<span>react-sortable-hoc已经使用了onClick事件。</span><span>因此,其中的任何内容都无法使用onClick。</span></p>
<p>&nbsp;</p>
<p>演示一种解决方案:&nbsp;您可以给&nbsp;SortableContainer&nbsp;组件上 使用distance prop设置触发排序之前要拖动的最小距离(例如,您可以设置10px的距离,例如:distance = {10}),&nbsp;这时候直接点击就不会立即触发SortableContainer&nbsp;上的点击事件。</p>
<p>从而解决问题。</p>
<p>&nbsp;</p>
<p>拖拽+点击&nbsp; 效果图 :</p>
<p><img src="https://img2020.cnblogs.com/blog/1249006/202004/1249006-20200423115746981-18655041.gif" alt=""></p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>先安装包</p>
<div class="cnblogs_code">
<pre>npm install react-sortable-hoc --save</pre>
</div>
<p>&nbsp;</p>
<p>主要部分代码演示:</p>
<div class="cnblogs_code">
<pre>import React, {Component} from 'react'<span style="color: rgba(0, 0, 0, 1)">;
import {render} from </span>'react-dom'<span style="color: rgba(0, 0, 0, 1)">;
import {Tooltip} from </span>'antd'<span style="color: rgba(0, 0, 0, 1)">;
import IconFont from </span>'../IconFont'; <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">IconFont</span><span style="color: rgba(0, 128, 0, 1)">
//</span><span style="color: rgba(0, 128, 0, 1)">拖拽</span>
import {sortableContainer, sortableElement} from 'react-sortable-hoc'<span style="color: rgba(0, 0, 0, 1)">;
import arrayMove from </span>'array-move'<span style="color: rgba(0, 0, 0, 1)">;
const SortableContainer </span>= sortableContainer( (props) =&gt; &lt;ul {...props}&gt;{props.children}&lt;/ul&gt; );
const SortableItem = sortableElement( (props) =&gt; &lt;li {...props}&gt;{props.children}&lt;/li&gt; );
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">拖拽end</span>
<span style="color: rgba(0, 0, 0, 1)">
class App extends Component {
state </span>=<span style="color: rgba(0, 0, 0, 1)"> {
    Routes: [
      {
            </span>"title": "企业管理"<span style="color: rgba(0, 0, 0, 1)">,
            </span>"icon": "icont3a"<span style="color: rgba(0, 0, 0, 1)">,
            </span>"isRenderMenu": <span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">,
            </span>"children"<span style="color: rgba(0, 0, 0, 1)">: [
                {
                  </span>"title": "企业通讯录"<span style="color: rgba(0, 0, 0, 1)">,
                  </span>"icon": "icona23"<span style="color: rgba(0, 0, 0, 1)">,
                  </span>"children"<span style="color: rgba(0, 0, 0, 1)">: [
                        {
                            </span>"path": "/admin/corporateBook"<span style="color: rgba(0, 0, 0, 1)">,
                            </span>"exact": <span style="color: rgba(0, 0, 255, 1)">false</span><span style="color: rgba(0, 0, 0, 1)">,
                            </span>"showInMenu": <span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">,
                            </span>"title": "企业通讯录"<span style="color: rgba(0, 0, 0, 1)">,
                            </span>"icon": ""<span style="color: rgba(0, 0, 0, 1)">
                        }
                  ]
                }
            ]
      },
      {
            </span>"title": "智能人事"<span style="color: rgba(0, 0, 0, 1)">,
            </span>"icon": "icont1a"<span style="color: rgba(0, 0, 0, 1)">,
            </span>"isRenderMenu": <span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">,
            </span>"children"<span style="color: rgba(0, 0, 0, 1)">: [
                {
                  </span>"title": "组织架构"<span style="color: rgba(0, 0, 0, 1)">,
                  </span>"icon": ""<span style="color: rgba(0, 0, 0, 1)">,
                  </span>"children"<span style="color: rgba(0, 0, 0, 1)">: [
                        {
                            </span>"path": "/admin/aptitudeHR/Framework"<span style="color: rgba(0, 0, 0, 1)">,
                            </span>"exact": <span style="color: rgba(0, 0, 255, 1)">false</span><span style="color: rgba(0, 0, 0, 1)">,
                            </span>"showInMenu": <span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">,
                            </span>"title": "部门/员工"<span style="color: rgba(0, 0, 0, 1)">,
                            </span>"icon": ""<span style="color: rgba(0, 0, 0, 1)">
                        },
                        {
                            </span>"path": "/admin/aptitudeHR/PostManage"<span style="color: rgba(0, 0, 0, 1)">,
                            </span>"exact": <span style="color: rgba(0, 0, 255, 1)">false</span><span style="color: rgba(0, 0, 0, 1)">,
                            </span>"showInMenu": <span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">,
                            </span>"title": "职位管理"<span style="color: rgba(0, 0, 0, 1)">,
                            </span>"icon": ""<span style="color: rgba(0, 0, 0, 1)">
                        },
                  ]
                }
            ]
      }
    ],
};

onSortEnd </span>= ({oldIndex, newIndex}) =&gt;<span style="color: rgba(0, 0, 0, 1)"> {
    </span><span style="color: rgba(0, 0, 255, 1)">this</span>.setState(({items}) =&gt;<span style="color: rgba(0, 0, 0, 1)"> ({
      items: arrayMove(items, oldIndex, newIndex),
    }));
};

render() {

    </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> (
      </span>&lt;<span style="color: rgba(0, 0, 0, 1)">SortableContainer
            </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">使子项点击事件可以正常执行的关键代码</span>
            distance = {10<span style="color: rgba(0, 0, 0, 1)">}
            onSortEnd</span>={<span style="color: rgba(0, 0, 255, 1)">this</span>.onSortEnd}&gt;<span style="color: rgba(0, 0, 0, 1)">
            {
                </span><span style="color: rgba(0, 0, 255, 1)">this</span>.state.Routes.map((item, index) =&gt;<span style="color: rgba(0, 0, 0, 1)"> (
                  </span>&lt;<span style="color: rgba(0, 0, 0, 1)">SortableItem
                        key</span>={`item-<span style="color: rgba(0, 0, 0, 1)">${index}`}
                        index</span>=<span style="color: rgba(0, 0, 0, 1)">{index}
                        value</span>=<span style="color: rgba(0, 0, 0, 1)">{index}
                  </span>&gt;
                        &lt;ul className='left-menu global-timestamp-1587610943136-left-menu'&gt;
                            &lt;<span style="color: rgba(0, 0, 0, 1)">li
                              className</span>={<span style="color: rgba(0, 0, 255, 1)">this</span>.state.currentLeftMenu === index ? 'active' : ''<span style="color: rgba(0, 0, 0, 1)">}
                              </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">点击事件可以正常执行</span>
                              onClick={<span style="color: rgba(0, 0, 255, 1)">this</span>.handleGo.bind(<span style="color: rgba(0, 0, 255, 1)">this</span>, item, index)}&gt;
                              &lt;Tooltip placement="right" title={item.title} mouseEnterDelay='0'&gt;
                                    &lt;div style={{textAlign: 'center'}}&gt;
                                        &lt;IconFont type={item.icon}/&gt;
                                    &lt;/div&gt;
                              &lt;/Tooltip&gt;
                            &lt;/li&gt;
                        &lt;/ul&gt;
                  &lt;/SortableItem&gt;
<span style="color: rgba(0, 0, 0, 1)">                ))
            }
      </span>&lt;/SortableContainer&gt;
<span style="color: rgba(0, 0, 0, 1)">    );
}
}

render(</span>&lt;App /&gt;, document.getElementById('root'));</pre>
</div>
<p>这样就可以了。</p>
<p>哦,注意&nbsp;拖拽时可能会造成样式丢失,原因是你的样式可能存在父级,因为拖拽时&nbsp;react-sorttable-hoc&nbsp;会帮我们复制一份拖拽的dom节点,放到body下,所以有父级样式就访问不到了。可以把样式直接写在body,注意class命名,别和其他冲突了。</p>
<p>我是这样写的:</p>
<div class="cnblogs_code"><img id="code_img_closed_23d80a7d-cfea-4b53-8673-7414bf7acedf" class="code_img_closed" src="https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif" alt=""><img id="code_img_opened_23d80a7d-cfea-4b53-8673-7414bf7acedf" class="code_img_opened" style="display: none" src="https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif" alt="">
<div id="cnblogs_code_open_23d80a7d-cfea-4b53-8673-7414bf7acedf" class="cnblogs_code_hide">
<pre><span style="color: rgba(128, 0, 0, 1)">//左侧拖拽菜单

ul.global-timestamp-1587610943136-left-menu.left-menu </span>{<span style="color: rgba(255, 0, 0, 1)">
margin-top</span>:<span style="color: rgba(0, 0, 255, 1)"> 10px</span>;<span style="color: rgba(255, 0, 0, 1)">
display</span>:<span style="color: rgba(0, 0, 255, 1)"> flex</span>;<span style="color: rgba(255, 0, 0, 1)">
flex-direction</span>:<span style="color: rgba(0, 0, 255, 1)"> column</span>;<span style="color: rgba(255, 0, 0, 1)">
align-items</span>:<span style="color: rgba(0, 0, 255, 1)"> center</span>;<span style="color: rgba(255, 0, 0, 1)">
width</span>:<span style="color: rgba(0, 0, 255, 1)">64px</span>;<span style="color: rgba(255, 0, 0, 1)">
font-size</span>:<span style="color: rgba(0, 0, 255, 1)"> 20px</span>;<span style="color: rgba(255, 0, 0, 1)">
color</span>:<span style="color: rgba(0, 0, 255, 1)"> rgba(255,255,255,.5)</span>;<span style="color: rgba(255, 0, 0, 1)">
li {
    display</span>:<span style="color: rgba(0, 0, 255, 1)"> flex</span>;<span style="color: rgba(255, 0, 0, 1)">
    flex-direction</span>:<span style="color: rgba(0, 0, 255, 1)"> column</span>;<span style="color: rgba(255, 0, 0, 1)">
    justify-content</span>:<span style="color: rgba(0, 0, 255, 1)"> center</span>;<span style="color: rgba(255, 0, 0, 1)">
    width</span>:<span style="color: rgba(0, 0, 255, 1)"> 40px</span>;<span style="color: rgba(255, 0, 0, 1)">
    height</span>:<span style="color: rgba(0, 0, 255, 1)"> 40px</span>;<span style="color: rgba(255, 0, 0, 1)">
    border-radius</span>:<span style="color: rgba(0, 0, 255, 1)"> 50%</span>;<span style="color: rgba(255, 0, 0, 1)">
    margin-bottom</span>:<span style="color: rgba(0, 0, 255, 1)"> 28px</span>;<span style="color: rgba(255, 0, 0, 1)">
    cursor</span>:<span style="color: rgba(0, 0, 255, 1)"> pointer</span>;
}<span style="color: rgba(128, 0, 0, 1)">
li:hover</span>{<span style="color: rgba(255, 0, 0, 1)">
    color</span>:<span style="color: rgba(0, 0, 255, 1)"> rgba(255,255,255,.9)</span>;
}<span style="color: rgba(128, 0, 0, 1)">
li.active </span>{<span style="color: rgba(255, 0, 0, 1)">
    color</span>:<span style="color: rgba(0, 0, 255, 1)"> rgba(255,255,255,1)</span>;<span style="color: rgba(255, 0, 0, 1)">
    background</span>:<span style="color: rgba(0, 0, 255, 1)">rgba(255,255,255,0.12)</span>;
}<span style="color: rgba(128, 0, 0, 1)">
}</span></pre>
</div>
<span class="cnblogs_code_collapse">View Code</span></div>
<p>结束结束。</p><br><br>
来源:https://www.cnblogs.com/taohuaya/p/12753946.html
頁: [1]
查看完整版本: React拖拽组件react-sortable-hoc给子组件添加onClick失效问题