/* * Copyright 2025 Commonwealth Scientific and Industrial Research * Organisation (CSIRO) ABN 41 687 119 230. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import type { AutocompleteChangeReason } from '@mui/material'; import type { QuestionnaireItemAnswerOption } from 'fhir/r4'; import useReadOnly from '../../../hooks/useReadOnly'; import useRenderingExtensions from '../../../hooks/useRenderingExtensions'; import useValidationFeedbackSeverity from '../../../hooks/useValidationFeedbackSeverity'; import type { BaseItemProps } from '../../../interfaces/renderProps.interface'; import { useQuestionnaireStore } from '../../../stores'; import { compareAnswerOptionValue } from '../../../utils/choice'; import { withFallbackDisplay } from '../../../utils/openChoice'; import { createEmptyQrItem, getQRItemId } from '../../../utils/qrItem'; import { FullWidthFormComponentBox } from '../../Box.styles'; import ItemFieldGrid, { getInstructionsId } from '../ItemParts/ItemFieldGrid'; import ItemLabel from '../ItemParts/ItemLabel'; import { sanitizeInput } from '../../../utils/inputSanitization'; import OpenChoiceSelectAnswerOptionField from './OpenChoiceSelectAnswerOptionField'; function OpenChoiceSelectAnswerOptionItem(props: BaseItemProps) { const { qItem, qrItem, isRepeated, isTabled, renderingExtensions, parentIsReadOnly, feedbackFromParent, onQrItemChange, calcExpUpdated } = props; const onFocusLinkId = useQuestionnaireStore.use.onFocusLinkId(); const readOnly = useReadOnly(qItem, parentIsReadOnly); // Perform validation checks const { feedback, feedbackSeverity } = useValidationFeedbackSeverity(qItem, feedbackFromParent); const { displayInstructions } = useRenderingExtensions(qItem); const instructionsId = getInstructionsId(qItem, displayInstructions, !!feedback); // Init input value const answerKey = getQRItemId(qrItem?.answer?.[0]?.id); const answerOptions = qItem.answerOption; if (!answerOptions) return null; const qrOpenChoice = qrItem ?? createEmptyQrItem(qItem, answerKey); const qrAnswer = qrOpenChoice.answer?.[0] ?? null; // A coded answer prefers a freshly-resolved display over its own recorded one, falling back // to the recorded display only if live resolution can't supply one - same as Choice Select. // A free-text answer (no valueCoding) is passed through unchanged; it has no terminology // concept and matching it against the coded answerOption list would incorrectly blank it out. let valueSelect: QuestionnaireItemAnswerOption | string | null = null; if (qrAnswer) { if (qrAnswer.valueCoding) { const liveValue = answerOptions.find((option) => compareAnswerOptionValue(option, qrAnswer)) ?? null; valueSelect = liveValue ? withFallbackDisplay(liveValue, qrAnswer.valueCoding) : null; } else { valueSelect = qrAnswer; } } // Event handlers // Handler function which handles both input change and selection change function handleValueChange( newValue: QuestionnaireItemAnswerOption | string | null, reason: AutocompleteChangeReason | string ) { //if the reason is reset, then we don't change the value, otherwise you will end up with looped setState calls if (reason === 'reset') { return; } if (newValue) { //If the value is a string (i.e from freeSolo input) if (typeof newValue === 'string') { onQrItemChange({ ...qrOpenChoice, answer: [{ id: answerKey, valueString: sanitizeInput(newValue) }] }); return; } //If the value is not a string, then it is a coding. const option = newValue; if (option['valueCoding']) { onQrItemChange({ ...qrOpenChoice, answer: [{ id: answerKey, valueCoding: option.valueCoding }] }); } else if (option['valueString']) { onQrItemChange({ ...qrOpenChoice, answer: [{ id: answerKey, valueString: sanitizeInput(option.valueString) }] }); } else if (option['valueInteger']) { onQrItemChange({ ...qrOpenChoice, answer: [{ id: answerKey, valueInteger: option.valueInteger }] }); } return; } onQrItemChange(createEmptyQrItem(qItem, answerKey)); } if (isRepeated) { return ( ); } return ( onFocusLinkId(qItem.linkId)}> } fieldChildren={ } /> ); } export default OpenChoiceSelectAnswerOptionItem;