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

Add choiceColumn support for choice questions #381

Merged
merged 1 commit into from
Nov 25, 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
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ import _, { debounce } from 'lodash';
import { useCallback, useContext } from 'react';
import { QuestionItemProps } from 'sdc-qrf';

import { QuestionnaireItemAnswerOption, QuestionnaireResponseItemAnswer } from '@beda.software/aidbox-types';
import {
QuestionnaireItemAnswerOption,
QuestionnaireItemChoiceColumn,
QuestionnaireResponseItemAnswer,
} from '@beda.software/aidbox-types';

import { AsyncSelect, Select } from 'src/components/Select';
import { ValueSetExpandProvider } from 'src/contexts';
Expand All @@ -19,10 +23,11 @@ interface ChoiceQuestionSelectProps {
options: QuestionnaireItemAnswerOption[];
repeats?: boolean;
placeholder?: string;
choiceColumn?: QuestionnaireItemChoiceColumn[];
}

export function ChoiceQuestionSelect(props: ChoiceQuestionSelectProps) {
const { value, onChange, options, repeats = false, placeholder = t`Select...` } = props;
const { value, onChange, options, repeats = false, placeholder = t`Select...`, choiceColumn } = props;

return (
<>
Expand All @@ -35,7 +40,7 @@ export function ChoiceQuestionSelect(props: ChoiceQuestionSelectProps) {
!!value && value?.findIndex((v) => _.isEqual(v?.value, option.value)) !== -1
}
isMulti={repeats}
getOptionLabel={(o) => (getDisplay(o.value) as string) || ''}
getOptionLabel={(o) => (getDisplay(o.value, choiceColumn) as string) || ''}
classNamePrefix="react-select"
placeholder={placeholder}
/>
Expand All @@ -44,7 +49,7 @@ export function ChoiceQuestionSelect(props: ChoiceQuestionSelectProps) {
}

export function QuestionChoice({ parentPath, questionItem }: QuestionItemProps) {
const { linkId, answerOption, repeats, answerValueSet } = questionItem;
const { linkId, answerOption, repeats, answerValueSet, choiceColumn } = questionItem;
const fieldName = [...parentPath, linkId];

const { value, formItem, onChange, placeholder = t`Select...` } = useFieldController(fieldName, questionItem);
Expand All @@ -60,6 +65,7 @@ export function QuestionChoice({ parentPath, questionItem }: QuestionItemProps)
onChange={onSelect}
repeats={repeats}
placeholder={placeholder}
choiceColumn={choiceColumn}
/>
</Form.Item>
);
Expand All @@ -73,6 +79,7 @@ export function QuestionChoice({ parentPath, questionItem }: QuestionItemProps)
onChange={onSelect}
repeats={repeats}
placeholder={placeholder}
choiceColumn={choiceColumn}
/>
</Form.Item>
);
Expand All @@ -84,10 +91,11 @@ interface ChoiceQuestionValueSetProps {
onChange: (option: any) => void;
repeats?: boolean;
placeholder?: string;
choiceColumn?: QuestionnaireItemChoiceColumn[];
}

export function ChoiceQuestionValueSet(props: ChoiceQuestionValueSetProps) {
const { answerValueSet, value, onChange, repeats = false, placeholder } = props;
const { answerValueSet, value, onChange, repeats = false, placeholder, choiceColumn } = props;
const expand = useContext(ValueSetExpandProvider);

const loadOptions = useCallback(
Expand All @@ -109,7 +117,7 @@ export function ChoiceQuestionValueSet(props: ChoiceQuestionValueSetProps) {
onChange={(v) => onChange(v)}
isOptionSelected={(option) => !!value && value?.findIndex((v) => _.isEqual(v?.value, option.value)) !== -1}
isMulti={repeats}
getOptionLabel={(o) => (getDisplay(o.value) as string) || ''}
getOptionLabel={(o) => (getDisplay(o.value, choiceColumn) as string) || ''}
placeholder={placeholder}
/>
);
Expand Down
14 changes: 13 additions & 1 deletion src/utils/questionnaire.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,31 @@ import * as yup from 'yup';
import {
Questionnaire,
QuestionnaireItemAnswerOption,
QuestionnaireItemChoiceColumn,
QuestionnaireResponseItemAnswer,
QuestionnaireResponseItemAnswerValue,
} from '@beda.software/aidbox-types';
import { parseFHIRTime } from '@beda.software/fhir-react';

import { formatHumanDate, formatHumanDateTime } from './date';
import { evaluate } from './fhirpath';

export function getDisplay(value?: QuestionnaireResponseItemAnswerValue): string | number | null {
export function getDisplay(
value?: QuestionnaireResponseItemAnswerValue,
choiceColumn?: QuestionnaireItemChoiceColumn[],
): string | number | null {
if (!value) {
return null;
}

if (value.Coding) {
if (choiceColumn && choiceColumn.length) {
const expression = choiceColumn[0]!.path;
if (expression) {
const calculatedValue = evaluate(value.Coding, expression)[0];
return calculatedValue ?? '';
}
}
return value.Coding.display ?? '';
}

Expand Down
Loading