首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >禁用基web DnD列表中的拖放

禁用基web DnD列表中的拖放
EN

Stack Overflow用户
提问于 2022-05-17 14:06:37
回答 1查看 42关注 0票数 0

实际上,我有一个来自BaseWeb https://baseweb.design/components/dnd-list/的拖放列表,而不是像示例显示的那样有字符串,我有一个博客的组件,比如文本部分、一些输入等等。我使用这个列表轻松地重新排序我的组件,但是我遇到了一个问题,如果我想要点击进入我的文本输入,我拖动我的组件,而不是进入。

我用React Quill作为文本编辑器

下面是我的列表代码:

代码语言:javascript
复制
                        initialState={{
                            items: componentsArray
                        }}
                        onChange={({oldIndex, newIndex}) =>
                            setComponentsArray(newIndex === -1 ?
                            arrayRemove(componentsArray, oldIndex) :
                            arrayMove(componentsArray, oldIndex, newIndex))
                        }
                        className=""
                        overrides={{
                            DragHandle: <FontAwesomeIcon icon={Icons.faLeftRight} />,
                        }}
                    />
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-05-30 12:04:01

尝试取消onmousedown BaseWeb的处理程序,用于您需要的无需拖动的元素。

代码语言:javascript
复制
import React, { useState, useEffect, useRef } from 'react';
import { List, arrayMove } from 'baseui/dnd-list';
import { StatefulSelect } from 'baseui/select';
import { v4 as uuidv4 } from 'uuid';

const RulesTab = () => {
  const [items, setItems] = useState([{ id: uuidv4() }, { id: uuidv4() }]);
  const dndRootRef = useRef(null);

  useEffect(() => {
    // override base-web's mousedown event handler
    dndRootRef.current.addEventListener('mousedown', (el) => {
      let isFreeOfDrag = false;
      let currentEl = el.target;

      // check if the element you clicked is inside the "free-of-drag block"
      do {
        if (currentEl.getAttribute('test-id') === 'free-of-drag') {
          isFreeOfDrag = true;
          break;
        } else {
          currentEl = currentEl.parentElement;
        }
      } while (currentEl);

      // invoke el.stopPropagation(); if the element you clicked is inside the "free-of-drag block"
      if (isFreeOfDrag) {
        el.stopPropagation();
      }
    });
  }, []);

  return (
    <List
      items={items.map(({ id }, index) => (
        <div style={{ display: 'flex' }} test-id="free-of-drag" key={id}>
          <StatefulSelect
            options={[
              { label: 'AliceBlue', id: '#F0F8FF' },
              { label: 'AntiqueWhite', id: '#FAEBD7' },
            ]}
            placeholder="Select color"
          />
        </div>
      ))}
      onChange={({ oldIndex, newIndex }, el) => setItems(arrayMove(items, oldIndex, newIndex))}
      overrides={{
        Root: {
          props: {
            ref: dndRootRef,
          },
        },
      }}
    />
  );
};

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72275510

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档