Skip to content

Commit

Permalink
Merge pull request #381 from beda-software/378-choice-column-2
Browse files Browse the repository at this point in the history
Add choiceColumn support for choice questions
  • Loading branch information
ir4y authored Nov 25, 2024
2 parents 07c744b + 59a3517 commit fd3e597
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 7 deletions.
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

0 comments on commit fd3e597

Please sign in to comment.