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

Support multi select #5643

Merged
merged 7 commits into from
Dec 10, 2024
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
5 changes: 5 additions & 0 deletions packages/definitions/dist/fhir/r4/valuesets.json
Original file line number Diff line number Diff line change
Expand Up @@ -44990,6 44990,11 @@
"display" : "Drop down",
"definition" : "A control where an item (or multiple items) can be selected from a list that is only displayed when the user is editing the field."
},
{
"code" : "multi-select",
"display" : "Multi select",
"definition" : "A control where multiple items can be selected from a list that is only displayed when the user is editing the field."
},
{
"code" : "check-box",
"display" : "Check-box",
Expand Down
96 changes: 96 additions & 0 deletions packages/react/src/QuestionnaireForm/QuestionnaireForm.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1387,6 1387,102 @@ describe('QuestionnaireForm', () => {
expect(searchInput).toBeInstanceOf(HTMLInputElement);
});

test('Multi Select Code', async () => {
await setup({
questionnaire: {
resourceType: 'Questionnaire',
status: 'active',
id: 'default-values',
title: 'Default Values Example',
item: [
{
id: 'choice',
linkId: 'choice',
text: 'choice',
type: 'choice',
answerOption: [
{
valueString: 'Yes',
},
{
valueString: 'No',
},
],
extension: [
{
url: 'http://hl7.org/fhir/StructureDefinition/questionnaire-itemControl',
valueCodeableConcept: {
coding: [
{
system: 'http://hl7.org/fhir/questionnaire-item-control',
code: 'multi-select',
display: 'Multi Select',
},
],
text: 'Multi Select',
},
},
],
},
],
},
onSubmit: jest.fn(),
});

const dropDown = screen.getByText('choice');

await act(async () => {
fireEvent.click(dropDown);
});

await act(async () => {
fireEvent.change(dropDown, { target: 'Yes' });
});

await act(async () => {
fireEvent.change(dropDown, { target: 'No' });
});
});

test('Multi Select Code shows with no data', async () => {
await setup({
questionnaire: {
resourceType: 'Questionnaire',
status: 'active',
item: [
{
linkId: 'q1',
type: QuestionnaireItemType.choice,
extension: [
{
url: 'http://hl7.org/fhir/StructureDefinition/questionnaire-itemControl',
valueCodeableConcept: {
coding: [
{
system: 'http://hl7.org/fhir/questionnaire-item-control',
code: 'multi-select',
display: 'Multi Select',
},
],
text: 'Multi Select',
},
},
],
text: 'q1',
answerOption: [],
},
],
},
onSubmit: jest.fn(),
});

expect(screen.getByText('q1')).toBeInTheDocument();

const searchInput = screen.getByPlaceholderText('No Answers Defined');
expect(searchInput).toBeInTheDocument();
expect(searchInput).toBeInstanceOf(HTMLInputElement);
});

test('Nested repeat', async () => {
const onSubmit = jest.fn();
await setup({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 5,7 @@ import {
formatCodeableConcept,
formatCoding,
getElementDefinition,
HTTP_HL7_ORG,
stringify,
TypedValue,
} from '@medplum/core';
Expand Down Expand Up @@ -219,6 220,16 @@ export function QuestionnaireFormItem(props: QuestionnaireFormItemProps): JSX.El
onChangeAnswer={(e) => onChangeAnswer(e)}
/>
);
} else if (isMultiSelectChoice(item) && !item.answerValueSet) {
return (
<QuestionnaireMultiSelectInput
name={name}
item={item}
initial={initial}
response={response}
onChangeAnswer={(e) => onChangeAnswer(e)}
/>
);
} else {
return (
<QuestionnaireChoiceSetInput
Expand Down Expand Up @@ -302,6 313,31 @@ function QuestionnaireChoiceDropDownInput(props: QuestionnaireChoiceInputProps):
);
}

function QuestionnaireMultiSelectInput(props: QuestionnaireChoiceInputProps): JSX.Element {
const { item, initial, response } = props;

if (!item.answerOption?.length) {
return <NoAnswerDisplay />;
}

const initialValue = getItemInitialValue(initial);
const { propertyName, data } = formatSelectData(props.item);
const currentAnswer = getCurrentMultiSelectAnswer(response);

return (
<MultiSelect
data={data}
placeholder="Select items"
searchable
defaultValue={currentAnswer || [typedValueToString(initialValue)]}
onChange={(selected) => {
const values = getNewMultiSelectValues(selected, propertyName, item);
props.onChangeAnswer(values);
}}
/>
);
}

function QuestionnaireChoiceSetInput(props: QuestionnaireChoiceInputProps): JSX.Element {
const { name, item, initial, onChangeAnswer, response } = props;

Expand Down Expand Up @@ -436,6 472,14 @@ function isDropDownChoice(item: QuestionnaireItem): boolean {
);
}

function isMultiSelectChoice(item: QuestionnaireItem): boolean {
return !!item.extension?.some(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The getExtensionValue helper could be useful here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

was doing some testing but left as is to keep it consistent with the rest of the file

(e) =>
e.url === HTTP_HL7_ORG '/fhir/StructureDefinition/questionnaire-itemControl' &&
e.valueCodeableConcept?.coding?.[0]?.code === 'multi-select'
);
}

interface MultiSelect {
readonly value: any;
readonly label: any;
Expand Down
Loading