1

I am implementing a list sort function in React with Drag & Drop function.

However, my code works normally with the onDrag event on the web, but it doesn't work at all with the onTouch event.

My web application is for mobile devices, so I don't know how to handle it.

I think it should be solved with the onTouchMove event.

how can i solve it??

//Parents.tsx
const handleSort = (e: React.TouchEvent<HTMLLIElement>) => {
    console.log(e.targetTouches[0]);

    let _question = [...items];

    const draggedItemContent = _question.splice(dragItem.current, 1)[0];

    //위치 변경
    _question.splice(dragOverItem.current, 0, draggedItemContent);

    dragItem.current = null;
    dragOverItem.current = null;

    setItems(_question);
  };

  return (
    <Option>
      {items.map((item: string, index: number) => {
        return (
          <MultipleSelectionInput
            hasDeleteBtn={item.length > 1}
            setItems={setItems}
            dragOverItem={dragOverItem}
            dragItem={dragItem}
            id={index}
            name={item}
            key={item}
            onDragEnd={handleSort}
          />
        );
      })}
    </Option>
  );
//MultipleSelectionInput.tsx
const MultipleSelectionInput = ({ name, dragItem, dragOverItem, hasDeleteBtn, onDragEnd, id, setItems }: IProps) => {
  const [inputContent, setInputContent] = useState("");

  const onChangeHandler = (e: React.ChangeEvent<HTMLInputElement>) => {
    setInputContent(e.target.value);
  };
  return (
    <MultipleSelectionLi draggable onTouchMove={(e) => onDragEnd(e)} onDragOver={(e) => e.preventDefault()}>
      <SlideArrow />
      <InputContainer>
        <input placeholder="선택지 입력" type="text" name="multiple-select-input" onChange={(e) => onChangeHandler(e)} />
        {hasDeleteBtn ? <CloseCircle /> : null}
      </InputContainer>
    </MultipleSelectionLi>
  );
};

If there is a site or YouTube that I can refer to, please share it.

What I want is a list order change via drag for mobile devices.

김정수
  • 611
  • 4
  • 9
  • 22

1 Answers1

2

This demo can probably help you: https://codesandbox.io/s/bycn8?file=/src/dndPolyfill.js

However, at least in my opinion, a touch is not the same as a drag. Usually you would only want the drag to start after a long touch on the element and not immediatly. Maybe this points you in the right direction: How to detect a long touch pressure with javascript for android and iphone?

Fabian Sievert
  • 810
  • 5
  • 15