// todo link to source
This documentation covers files located at: Questionnaire Item Choice
Questionnaire Item Choice Component
The Questionnaire Item Choice Component is a little more complex than the other questionnaire item components. It needs to handle the answerOptions and therefore it must retrieve them from the QuestionnaireItem.answerOption
property.
Choice Item Handling & Option Matching
Concept
A critical requirement for Choice items is that the QuestionnaireResponseItem.answer property must be matched against available options before the patching process occurs. This ensures that only valid values, consistent with the current Questionnaire definition, are set.
Initialization Workflow
To achieve this, option retrieval must be completed before the answer FormArray is initialized. Consequently, the setup must occur prior to the QuestionnaireItemBaseComponent.ngOnInit call:
- We override
ngOnInit. - We set up and filter for our options.
- Finally, we call
super.ngOnInit()to proceed with the standard lifecycle.
Special Case: itemValueKey
In standard items, the itemValueKey is mapped directly to a specific FHIR key (e.g., valueString or valueInteger), as detailed in the Questionnaire Item Base Component.
However, Choice elements present a unique challenge because they support polymorphic value types within their options (e.g., valueString, valueInteger, or valueCoding). To handle this flexibility without hardcoding a single FHIR key, we use a specialized approach:
- Dedicated Wrapper: We use a custom key called
CHOICE_ANSWER_WRAPPER. - Object-Level Handling: Instead of managing primitive values, the Choice element handles the entire Response object.
- Custom Comparison: We implemented a custom comparison function to ensure that objects are compared by their internal structure/identity rather than just primitive equality.
Dynamic Response Building
When the final response is constructed, the QuestionnaireLogicService unpacks the value stored under the CHOICE_ANSWER_WRAPPER. This architecture allows us to:
- Keep
FormGroupsconsistent and simple. - Remain dynamic across different value types.
- Theoretically support a mix of different "value" types within the same set of options.
Supported Item Features
| Feature | Config Key | Logic |
|---|---|---|
| Label Text | item.text | This component |
| Answer Options | item.answerOption | This component |
| Enable When | item.enableWhen | Item Renderer |
| Enable When Behaviour | item.enableWhenBehaviour | Item Renderer |
| Required | item.required | Item Renderer |
| Repeats | item.repeats | Item Renderer |
Extension Features
| Feature | Extension URL | Key / Value Type |
|---|---|---|
| Translation | http://hl7.org/fhir/StructureDefinition/translation | Complex Type |
| Min Occurrences | http://hl7.org/fhir/StructureDefinition/questionnaire-minOccurs | valueInteger |
| Max Occurrences | http://hl7.org/fhir/StructureDefinition/questionnaire-maxOccurs | valueInteger |
| SDC Enable When | http://hl7.org/fhir/uv/sdc/StructureDefinition/sdc-questionnaire-enableWhenExpression | valueExpression |
| Item Control | http://hl7.org/fhir/StructureDefinition/questionnaire-itemControl | valueCodeableConcept: radio-button |
| Choice Orientation | http://hl7.org/fhir/StructureDefinition/questionnaire-choiceOrientation | valueCode: horizontal | vertical |
Technical Implementation Details
For detailed information regarding technical implementation, please refer to the following:
- Questionnaire Base Component
- Questionnaire Item Renderer
- Questionnaire-Enable-When Service