-
Notifications
You must be signed in to change notification settings - Fork 124
Expand file tree
/
Copy pathdraggable-question.component.tsx
More file actions
201 lines (193 loc) · 6.39 KB
/
draggable-question.component.tsx
File metadata and controls
201 lines (193 loc) · 6.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
import React, { useCallback, useState } from 'react';
import classNames from 'classnames';
import { SortableContext, useSortable } from '@dnd-kit/sortable';
import { useTranslation } from 'react-i18next';
import { IconButton } from '@carbon/react';
import { Draggable, Edit, TrashCan, Copy } from '@carbon/react/icons';
import { showModal, ChevronDownIcon, ChevronUpIcon } from '@openmrs/esm-framework';
import MarkdownWrapper from '../markdown-wrapper/markdown-wrapper';
import type { FormField } from '@openmrs/esm-form-engine-lib';
import type { Schema } from '@types';
import styles from './draggable-question.scss';
interface DraggableQuestionProps {
handleDuplicateQuestion: (question: FormField, pageId: number, sectionId: number, questionId?: number) => void;
onSchemaChange: (schema: Schema) => void;
pageIndex: number;
question: FormField;
questionCount: number;
questionIndex: number;
schema: Schema;
sectionIndex: number;
subQuestionIndex?: number;
children?: React.ReactNode;
}
const DraggableQuestion: React.FC<DraggableQuestionProps> = ({
handleDuplicateQuestion,
onSchemaChange,
pageIndex,
question,
questionCount,
questionIndex,
schema,
sectionIndex,
subQuestionIndex,
children,
}) => {
const { t } = useTranslation();
const defaultEnterDelayInMs = 300;
const [isCollapsed, setIsCollapsed] = useState(true);
const toggleCollapse = () => {
if (question.questions) {
setIsCollapsed(!isCollapsed);
}
};
const launchEditQuestionModal = useCallback(() => {
const dispose = showModal('question-modal', {
formField: question,
closeModal: () => dispose(),
onSchemaChange,
schema,
questionIndex,
pageIndex,
sectionIndex,
subQuestionIndex,
});
}, [onSchemaChange, pageIndex, question, questionIndex, schema, sectionIndex, subQuestionIndex]);
const launchDeleteQuestionModal = useCallback(() => {
const dispose = showModal('delete-question-modal', {
closeModal: () => dispose(),
pageIndex,
sectionIndex,
question,
questionIndex,
subQuestionIndex,
onSchemaChange,
schema,
});
}, [onSchemaChange, pageIndex, question, questionIndex, schema, sectionIndex, subQuestionIndex]);
const { attributes, listeners, setNodeRef, active, isDragging, over, isOver } = useSortable({
id: question.id,
data: {
type: subQuestionIndex === null || subQuestionIndex === undefined ? 'question' : 'obsQuestion',
question: {
handleDuplicateQuestion,
onSchemaChange,
pageIndex,
sectionIndex,
question,
questionCount,
questionIndex,
subQuestionIndex,
schema,
},
},
disabled: questionCount <= 1 && (subQuestionIndex === undefined || subQuestionIndex === null),
});
const handleDuplicate = useCallback(() => {
if (!isDragging) {
if (subQuestionIndex !== undefined && subQuestionIndex !== null) {
handleDuplicateQuestion(question, pageIndex, sectionIndex, questionIndex);
} else {
handleDuplicateQuestion(question, pageIndex, sectionIndex);
}
}
}, [handleDuplicateQuestion, isDragging, question, pageIndex, sectionIndex, questionIndex, subQuestionIndex]);
return (
<div
ref={setNodeRef}
className={classNames(styles.question, {
[styles.dragContainer]: true,
[styles.dragContainerWhenDragging]: isDragging,
[styles.dropZone]: over?.id === question.id,
[styles.droppable]: active?.id === question.id,
})}
>
<div
className={classNames(styles.questionContainer, {
[styles.obsGroup]: question?.questions && question?.questions.length > 0,
})}
onClick={toggleCollapse}
>
<div className={styles.iconAndName}>
<div ref={setNodeRef} {...attributes} {...listeners}>
<IconButton
className={styles.dragIcon}
enterDelayMs={over ? 6000 : defaultEnterDelayInMs}
label={t('dragToReorder', 'Drag to reorder')}
kind="ghost"
size="md"
onClick={(e) => e.stopPropagation()}
>
<Draggable />
</IconButton>
</div>
<p className={styles.questionLabel}>
{question.questionOptions.rendering === 'markdown' ? (
<MarkdownWrapper markdown={question.value} />
) : (
question.label
)}
</p>
</div>
<div className={styles.buttonsContainer}>
<IconButton
enterDelayMs={defaultEnterDelayInMs}
label={t('duplicateQuestion', 'Duplicate question')}
kind="ghost"
onClick={(e) => {
e.stopPropagation();
handleDuplicate();
}}
size="md"
>
<Copy />
</IconButton>
<IconButton
enterDelayMs={defaultEnterDelayInMs}
label={t('editQuestion', 'Edit question')}
kind="ghost"
onClick={(e) => {
e.stopPropagation();
launchEditQuestionModal();
}}
size="md"
>
<Edit />
</IconButton>
<IconButton
enterDelayMs={defaultEnterDelayInMs}
label={t('deleteQuestion', 'Delete question')}
kind="ghost"
onClick={(e) => {
e.stopPropagation();
launchDeleteQuestionModal();
}}
size="md"
>
<TrashCan />
</IconButton>
{question?.questions && question?.questions.length > 0 && (
<span className={styles.collapseIconWrapper} onClick={(e) => e.stopPropagation()}>
{isCollapsed ? (
<ChevronDownIcon className={styles.collapseIcon} aria-label="Expand" />
) : (
<ChevronUpIcon className={styles.collapseIcon} aria-label="Collapse" />
)}
</span>
)}
</div>
</div>
{question?.questions && (
<div
className={classNames(styles.obsQuestions, {
[styles.hiddenAccordion]: isCollapsed,
[styles.accordionContainer]: !isCollapsed,
})}
>
<SortableContext items={question.questions.map((qn) => qn.id)}>{children}</SortableContext>
</div>
)}
</div>
);
};
export default DraggableQuestion;