@openmrs/esm-form-builder-app 3.0.2-pre.1505 → 3.0.2-pre.1532

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,5 +1,5 @@
1
- import React, { useCallback, useMemo } from 'react';
2
- import { FormLabel, InlineNotification, FormGroup, Stack } from '@carbon/react';
1
+ import React, { useCallback, useEffect, useMemo, useState } from 'react';
2
+ import { FormLabel, InlineNotification, FormGroup, Stack, Checkbox } from '@carbon/react';
3
3
  import { useTranslation } from 'react-i18next';
4
4
  import ConceptSearch from '../../../common/concept-search/concept-search.component';
5
5
  import { useFormField } from '../../../../form-field-context';
@@ -9,6 +9,13 @@ import styles from './obs-type-question.scss';
9
9
  const ObsTypeQuestion: React.FC = () => {
10
10
  const { t } = useTranslation();
11
11
  const { formField, setFormField, concept, setConcept } = useFormField();
12
+ const [selectedMapping, setSelectedMapping] = useState<string | null>(null);
13
+
14
+ useEffect(() => {
15
+ if (formField.questionOptions?.concept) {
16
+ setSelectedMapping(formField.questionOptions.concept);
17
+ }
18
+ }, [formField.questionOptions?.concept]);
12
19
 
13
20
  const getDatePickerType = useCallback((selectedConcept: Concept): DatePickerType | null => {
14
21
  switch (selectedConcept.datatype.name) {
@@ -43,6 +50,7 @@ const ObsTypeQuestion: React.FC = () => {
43
50
 
44
51
  const clearSelectedConcept = useCallback(() => {
45
52
  setConcept(null);
53
+ setSelectedMapping(null);
46
54
  setFormField((prevFormField) => {
47
55
  const updatedFormField = { ...prevFormField };
48
56
  if (updatedFormField.questionOptions) {
@@ -56,6 +64,34 @@ const ObsTypeQuestion: React.FC = () => {
56
64
  });
57
65
  }, [setFormField, setConcept]);
58
66
 
67
+ const handleCheckboxChange = useCallback(
68
+ (mapping: ConceptMapping) => {
69
+ const newConceptValue = `${mapping.type}:${mapping.value}`;
70
+ setSelectedMapping(newConceptValue);
71
+ setFormField((prevField) => ({
72
+ ...prevField,
73
+ questionOptions: {
74
+ ...prevField.questionOptions,
75
+ concept: newConceptValue,
76
+ },
77
+ }));
78
+ },
79
+ [setFormField],
80
+ );
81
+
82
+ const handleUncheckAll = useCallback(() => {
83
+ setSelectedMapping(null);
84
+ if (concept) {
85
+ setFormField((prevField) => ({
86
+ ...prevField,
87
+ questionOptions: {
88
+ ...prevField.questionOptions,
89
+ concept: concept.uuid,
90
+ },
91
+ }));
92
+ }
93
+ }, [concept, setFormField]);
94
+
59
95
  const conceptMappings: ConceptMapping[] = useMemo(() => {
60
96
  if (concept && concept.mappings) {
61
97
  return concept.mappings.map((conceptMapping) => {
@@ -70,6 +106,8 @@ const ObsTypeQuestion: React.FC = () => {
70
106
  return [];
71
107
  }, [concept]);
72
108
 
109
+ const hasSameAsMappings = conceptMappings.some((mapping) => mapping.relationship === 'SAME-AS');
110
+
73
111
  return (
74
112
  <Stack gap={5}>
75
113
  <ConceptSearch
@@ -96,6 +134,7 @@ const ObsTypeQuestion: React.FC = () => {
96
134
  <th>{t('relationship', 'Relationship')}</th>
97
135
  <th>{t('source', 'Source')}</th>
98
136
  <th>{t('code', 'Code')}</th>
137
+ {hasSameAsMappings && <th>{t('select', 'Select')}</th>}
99
138
  </tr>
100
139
  </thead>
101
140
  <tbody>
@@ -104,6 +143,23 @@ const ObsTypeQuestion: React.FC = () => {
104
143
  <td>{mapping.relationship}</td>
105
144
  <td>{mapping.type}</td>
106
145
  <td>{mapping.value}</td>
146
+ {hasSameAsMappings && (
147
+ <td>
148
+ {mapping.relationship === 'SAME-AS' && (
149
+ <Checkbox
150
+ id={`checkbox-${index}`}
151
+ checked={selectedMapping === `${mapping.type}:${mapping.value}`}
152
+ onChange={() => {
153
+ if (selectedMapping === `${mapping.type}:${mapping.value}`) {
154
+ handleUncheckAll();
155
+ } else {
156
+ handleCheckboxChange(mapping);
157
+ }
158
+ }}
159
+ />
160
+ )}
161
+ </td>
162
+ )}
107
163
  </tr>
108
164
  ))}
109
165
  </tbody>
@@ -113,5 +169,4 @@ const ObsTypeQuestion: React.FC = () => {
113
169
  </Stack>
114
170
  );
115
171
  };
116
-
117
172
  export default ObsTypeQuestion;