/* * 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 React from 'react'; import type { AutocompleteChangeReason } from '@mui/material/Autocomplete'; import Autocomplete from '@mui/material/Autocomplete'; import FormControl from '@mui/material/FormControl'; import FormHelperText from '@mui/material/FormHelperText'; import { StandardTextField } from '../Textfield.styles'; import Typography from '@mui/material/Typography'; import type { PropsWithIsTabledAttribute, PropsWithParentIsReadOnlyAttribute, PropsWithRenderingExtensionsAttribute } from '../../../interfaces/renderProps.interface'; import type { Coding, QuestionnaireItem } from 'fhir/r4'; import type { TerminologyError } from '../../../hooks/useValueSetCodings'; import { useRendererConfigStore } from '../../../stores'; import { interpolate } from '../../../i18n'; import DisplayUnitText from '../ItemParts/DisplayUnitText'; import ExpressionUpdateFadingIcon from '../ItemParts/ExpressionUpdateFadingIcon'; import AccessibleFeedback from '../ItemParts/AccessibleFeedback'; interface OpenChoiceSelectAnswerValueSetFieldProps extends PropsWithIsTabledAttribute, PropsWithParentIsReadOnlyAttribute, PropsWithRenderingExtensionsAttribute { qItem: QuestionnaireItem; options: Coding[]; valueSelect: Coding | string | null; terminologyError: TerminologyError; feedback: string; feedbackSeverity?: 'error' | 'warning'; readOnly: boolean; calcExpUpdated: boolean; instructionsId?: string; onValueChange: ( newValue: Coding | string | null, reason: AutocompleteChangeReason | string ) => void; } function OpenChoiceSelectAnswerValueSetField(props: OpenChoiceSelectAnswerValueSetFieldProps) { const { qItem, options, valueSelect, terminologyError, feedback, feedbackSeverity, readOnly, calcExpUpdated, instructionsId, isTabled, renderingExtensions, onValueChange } = props; const readOnlyVisualStyle = useRendererConfigStore.use.readOnlyVisualStyle(); const textFieldWidth = useRendererConfigStore.use.textFieldWidth(); const rendererStrings = useRendererConfigStore.use.rendererStrings(); const { displayUnit, displayPrompt, entryFormat } = renderingExtensions; return ( typeof option === 'string' ? option : (option.display ?? `${option.code}`) } onChange={(_, newValue, reason) => onValueChange(newValue, reason)} onInputChange={(_, newValue, reason) => onValueChange(newValue, reason)} fullWidth freeSolo autoHighlight disabled={readOnly && readOnlyVisualStyle === 'disabled'} readOnly={readOnly && readOnlyVisualStyle === 'readonly'} size="small" renderInput={(params) => ( {params.InputProps.endAdornment} {displayUnit} ) }, htmlInput: { ...params.inputProps, ...(isTabled ? { 'aria-label': qItem.text ?? interpolate(rendererStrings.unnamedItem, { type: qItem.type }) } : { 'aria-labelledby': `label-${qItem.linkId}` }), ...(instructionsId && { 'aria-describedby': instructionsId }) } }} /> )} /> {terminologyError.error ? ( {interpolate(rendererStrings.terminologyServerFetchError, { valueSet: `${terminologyError.answerValueSet}` })} ) : null} {feedback ? ( {feedback} ) : null} ); } export default OpenChoiceSelectAnswerValueSetField;