Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(feat) adding support for questionInfo #152

Merged
merged 5 commits into from
Dec 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions __mocks__/forms/ohri-forms/sample_fields.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"name": "Field tester",
"pages": [
{
"label": "One",
"sections": [
{
"label": "Sec One",
"isExpanded": "true",
"questions": [
{
"label": "Text question",
"type": "obs",
"required": false,
"id": "id_text",
"questionInfo":"sample tooltip info for text",
"questionOptions": {
"rendering": "text",
"concept": "928def39-33c1-4ddb-8430-165db28448c8",
"conceptMappings": [],
"answers": []
},
"validators": []
}
]
}
]
}
],
"processor": "EncounterFormProcessor",
"encounterType": "e22e39fd-7db2-45e7-80f1-60fa0d5a4378",
"referencedForms": [],
"uuid": "0859d9ad-2ed4-48db-96eb-7b0ce0c9903e",
"description": "A form containing most fields, to be used for testing purposes",
"version": "1.0"
}
1 change: 1 addition & 0 deletions src/api/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ export interface OHRIFormField {
inlineRendering?: 'single-line' | 'multiline' | 'automatic';
validators?: Array<Record<string, any>>;
behaviours?: Array<Record<string, any>>;
questionInfo?: string;
}

export interface OHRIFormFieldProps {
Expand Down
27 changes: 15 additions & 12 deletions src/components/group/ohri-obs-group.component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { OHRIUnspecified } from '../inputs/unspecified/ohri-unspecified.componen
import styles from './ohri-obs-group.scss';
import { useField } from 'formik';
import { getFieldControlWithFallback, isUnspecifiedSupported } from '../section/helpers';
import { OHRITooltip } from '../inputs/tooltip/ohri-tooltip';

export interface ObsGroupProps extends OHRIFormFieldProps {
deleteControl?: any;
Expand All @@ -17,16 +18,16 @@ export const OHRIObsGroup: React.FC<ObsGroupProps> = ({ question, onChange, dele
useEffect(() => {
if (question.questions) {
Promise.all(
question.questions.map(field => {
return getFieldControlWithFallback(field)?.then(result => ({ field, control: result }));
question.questions.map((field) => {
return getFieldControlWithFallback(field)?.then((result) => ({ field, control: result }));
}),
).then(results => {
).then((results) => {
setGroupMembersControlMap(results);
});
}
}, [question.questions]);
const groupContent = groupMembersControlMap
.filter(groupMemberMapItem => !!groupMemberMapItem && !groupMemberMapItem.field.isHidden)
.filter((groupMemberMapItem) => !!groupMemberMapItem && !groupMemberMapItem.field.isHidden)
.map((groupMemberMapItem, index) => {
const { control, field } = groupMemberMapItem;
if (control) {
Expand All @@ -37,16 +38,18 @@ export const OHRIObsGroup: React.FC<ObsGroupProps> = ({ question, onChange, dele
handler: formFieldHandlers[field.type],
useField,
});

return (
<div className={`${styles.flexColumn} ${styles.obsGroupColumn} `}>
{isUnspecifiedSupported(field) ? (
<>
{questionFragment}
<OHRIUnspecified question={field} onChange={onChange} handler={formFieldHandlers[field.type]} />
</>
) : (
questionFragment
)}
<div className={styles.parent}>
{questionFragment}
<div className={isUnspecifiedSupported(field) ? styles.tooltipWithUnspecified : styles.tooltip}>
{isUnspecifiedSupported(field) && (
<OHRIUnspecified question={field} onChange={onChange} handler={formFieldHandlers[field.type]} />
)}
{field.questionInfo && <OHRITooltip field={field} />}
</div>
</div>
</div>
);
}
Expand Down
21 changes: 17 additions & 4 deletions src/components/group/ohri-obs-group.scss
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,6 @@
max-width: 18rem;
}

.obsGroupColumn input[class*="cds--date-picker__input"] {
width: 18rem !important;
}

.obsGroupColumn > div, .obsGroupColumn div[class*="cds--number"] {
margin-top: 0 !important;
}
Expand All @@ -33,3 +29,20 @@
.obsGroupColumn > div > div > div > div {
max-width: 18rem !important;
}

.parent{
width: 360px;
}

.tooltipWithUnspecified{
display: flex;
justify-content: space-between;
align-items: center;
}

.tooltip{
display: flex;
justify-content: flex-end;
align-items: center;
margin-top: 0.5rem;
}
6 changes: 6 additions & 0 deletions src/components/inputs/tooltip/ohri-tooltip.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.tooltip{
border: none;
background-color: #f4f4f4;
z-index: 999;
margin-top: 0.25rem;
}
21 changes: 21 additions & 0 deletions src/components/inputs/tooltip/ohri-tooltip.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import React from 'react';
import { Tooltip } from '@carbon/react';
import { Information } from '@carbon/react/icons';
import styles from './ohri-tooltip.scss';
import { OHRIFormField } from '../../../api/types';

interface OHRITooltipProps {
field: OHRIFormField;
}

export const OHRITooltip: React.FC<OHRITooltipProps> = ({ field }) => {
return (
<span>
<Tooltip align="right" label={field.questionInfo} description={field.questionInfo}>
<button className={styles.tooltip} type="button" data-testid={field.id}>
<Information />
</button>
</Tooltip>
</span>
);
};
26 changes: 18 additions & 8 deletions src/components/section/ohri-form-section.component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import { OHRIUnspecified } from '../inputs/unspecified/ohri-unspecified.componen
import { OHRIFormField, OHRIFormFieldProps, SubmissionHandler } from '../../api/types';
import styles from './ohri-form-section.scss';
import { getFieldControlWithFallback, isUnspecifiedSupported } from './helpers';
import { OHRITooltip } from '../inputs/tooltip/ohri-tooltip';
import { subtle } from 'crypto';

interface FieldComponentMap {
fieldComponent: React.ComponentType<OHRIFormFieldProps>;
Expand All @@ -19,12 +21,12 @@ const OHRIFormSection = ({ fields, onFieldChange }) => {

useEffect(() => {
Promise.all(
fields.map(async fieldDescriptor => {
fields.map(async (fieldDescriptor) => {
const fieldComponent = await getFieldControlWithFallback(fieldDescriptor);
const handler = await getRegisteredFieldSubmissionHandler(fieldDescriptor.type);
return { fieldDescriptor, fieldComponent, handler };
}),
).then(results => {
).then((results) => {
setFieldComponentMapEntries(results);
});
}, [fields]);
Expand All @@ -33,7 +35,7 @@ const OHRIFormSection = ({ fields, onFieldChange }) => {
<ErrorBoundary FallbackComponent={ErrorFallback} onReset={() => {}}>
<div className={styles.sectionContainer}>
{fieldComponentMapEntries
.filter(entry => entry?.fieldComponent)
.filter((entry) => entry?.fieldComponent)
.map((entry, index) => {
const { fieldComponent: FieldComponent, fieldDescriptor, handler } = entry;
if (FieldComponent) {
Expand All @@ -46,13 +48,21 @@ const OHRIFormSection = ({ fields, onFieldChange }) => {
useField={useField}
/>
);
return isUnspecifiedSupported(fieldDescriptor) && fieldDescriptor.questionOptions.rendering != 'group' ? (
<div key={index}>

return (
<div key={index} className={styles.parent}>
{qnFragment}
<OHRIUnspecified question={fieldDescriptor} onChange={onFieldChange} handler={handler} />
<div
className={
isUnspecifiedSupported(fieldDescriptor) ? styles.tooltipWithUnspecified : styles.tooltip
}>
{isUnspecifiedSupported(fieldDescriptor) &&
fieldDescriptor.questionOptions.rendering != 'group' && (
<OHRIUnspecified question={fieldDescriptor} onChange={onFieldChange} handler={handler} />
)}
{fieldDescriptor.questionInfo && <OHRITooltip field={fieldDescriptor} />}
</div>
</div>
) : (
<div key={index}>{qnFragment}</div>
);
}
})}
Expand Down
15 changes: 15 additions & 0 deletions src/components/section/ohri-form-section.scss
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,21 @@
margin-bottom: 3.063rem;
}

.parent{
width: 360px;
}

.tooltipWithUnspecified{
display: flex;
justify-content: space-between;
}

.tooltip{
display: flex;
justify-content: flex-end;
align-items: center;
}

.sectionTitle {
max-width: 200px;
font-size: 1rem;
Expand Down
23 changes: 23 additions & 0 deletions src/ohri-form.component.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
findAllByRole,
getAllByRole,
queryAllByRole,
findByTestId,
} from '@testing-library/react';
import { when } from 'jest-when';
import * as api from '../src/api/api';
Expand All @@ -33,6 +34,7 @@ import { mockSessionDataResponse } from '../__mocks__/session.mock';
import demoHtsOpenmrsForm from '../__mocks__/forms/omrs-forms/demo_hts-form.json';
import demoHtsOhriForm from '../__mocks__/forms/ohri-forms/demo_hts-form.json';
import obsGroup_test_form from '../__mocks__/forms/ohri-forms/obs-group-test_form.json';
import sample_fields_form from '../__mocks__/forms/ohri-forms/sample_fields.json';

import {
assertFormHasAllFields,
Expand Down Expand Up @@ -148,6 +150,27 @@ describe('OHRI Forms:', () => {
}
});
// Form submission

describe('Question Info', () => {
fit('Should ascertain that each field with questionInfo passed will display a tooltip', async () => {
//render the test form
await act(async () => renderForm(null, sample_fields_form));

//check for text field
const textField = await findTextOrDateInput(screen, 'Text question');
expect(textField).toBeInTheDocument();

// check for tooltip icon on text field
const textFIeldTooltip = await screen.findByTestId('id_text');
expect(textFIeldTooltip).toBeInTheDocument();

//testing for the tooltip
fireEvent.mouseOver(textFIeldTooltip);
const textFieldTooltipMessage = await screen.findByText(/sample tooltip info for text/i);
expect(textFieldTooltipMessage).toBeInTheDocument();
});
});

describe('Form submission', () => {
it('Should validate form submission', async () => {
// Mock the form submission function to simulate success
Expand Down
Loading