diff --git a/src/components/interactive-builder/draggable-question.component.tsx b/src/components/interactive-builder/draggable-question.component.tsx index e082b04e..d2dce558 100644 --- a/src/components/interactive-builder/draggable-question.component.tsx +++ b/src/components/interactive-builder/draggable-question.component.tsx @@ -1,4 +1,5 @@ import React, { useCallback } from 'react'; +import classNames from 'classnames'; import { useDraggable } from '@dnd-kit/core'; import { CSS } from '@dnd-kit/utilities'; import { useTranslation } from 'react-i18next'; @@ -29,8 +30,8 @@ const DraggableQuestion: React.FC = ({ schema, sectionIndex, }) => { - const defaultEnterDelayInMs = 300; const { t } = useTranslation(); + const defaultEnterDelayInMs = 300; const draggableId = `question-${pageIndex}-${sectionIndex}-${questionIndex}`; const launchEditQuestionModal = useCallback(() => { @@ -57,25 +58,31 @@ const DraggableQuestion: React.FC = ({ }); }, [onSchemaChange, pageIndex, question, questionIndex, schema, sectionIndex]); - const { attributes, listeners, transform, isDragging, setNodeRef } = useDraggable({ + const { attributes, listeners, transform, isDragging, over, setNodeRef } = useDraggable({ id: draggableId, disabled: questionCount <= 1, }); - const style = { - transform: CSS.Translate.toString(transform), - }; - - const dragStyles = isDragging ? styles.isDragged : styles.normal; + const handleDuplicate = useCallback(() => { + if (!isDragging) { + handleDuplicateQuestion(question, pageIndex, sectionIndex); + } + }, [handleDuplicateQuestion, isDragging, question, pageIndex, sectionIndex]); return ( -
+
@@ -91,7 +98,7 @@ const DraggableQuestion: React.FC = ({ feedback={t('duplicated', 'Duplicated') + '!'} iconDescription={t('duplicateQuestion', 'Duplicate question')} kind="ghost" - onClick={() => !isDragging && handleDuplicateQuestion(question, pageIndex, sectionIndex)} + onClick={handleDuplicate} /> = ({ ); const handleDragEnd = (event: DragEndEvent) => { - const { active, delta } = event; + const { active, over } = event; if (active) { // Get the source information @@ -251,11 +251,15 @@ const InteractiveBuilder: React.FC = ({ const sourceSectionIndex = parseInt(activeIdParts[2]); const sourceQuestionIndex = parseInt(activeIdParts[3]); + // Get the destination information + const destination = over.id.toString().split('-'); + const destinationQuestionIndex = parseInt(destination[4]); + // Move the question within the same section const questions = schema.pages[sourcePageIndex].sections[sourceSectionIndex].questions; const questionToMove = questions[sourceQuestionIndex]; questions.splice(sourceQuestionIndex, 1); - questions.splice(sourceQuestionIndex + delta.y, 0, questionToMove); + questions.splice(destinationQuestionIndex, 0, questionToMove); const updatedSchema = { ...schema, diff --git a/translations/en.json b/translations/en.json index c1eb00c8..9f0cc9e4 100644 --- a/translations/en.json +++ b/translations/en.json @@ -50,6 +50,7 @@ "description": "Description", "descriptionPlaceholderText": "e.g. A form used to collect encounter data for clients in the Express Care program.", "downloadSchema": "Download schema", + "dragToReorder": "Drag to reorder", "duplicated": "Duplicated", "duplicateQuestion": "Duplicate question", "editButton": "Edit {{elementType}}", @@ -163,7 +164,6 @@ "relationship": "Relationship", "renderChanges": "Render changes", "renderingType": "Rendering type", - "reorderQuestion": "Reorder question", "required": "Required", "restoreDraft": "Restore draft", "retired": "Retired",