/* * 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 Autocomplete from '@mui/material/Autocomplete'; import CircularProgress from '@mui/material/CircularProgress'; import Fade from '@mui/material/Fade'; import Tooltip from '@mui/material/Tooltip'; import type { Coding, QuestionnaireItem } from 'fhir/r4'; import { StandardTextField } from '../Textfield.styles'; import SearchIcon from '@mui/icons-material/Search'; import InfoIcon from '@mui/icons-material/Info'; // @ts-ignore: Module has no declaration file. Not sure why WarningAmber.d.ts is not present in MUI icons 7.0.2 import WarningAmberIcon from '@mui/icons-material/WarningAmber'; import DoneIcon from '@mui/icons-material/Done'; import ErrorIcon from '@mui/icons-material/Error'; import type { PropsWithIsTabledAttribute, PropsWithParentIsReadOnlyAttribute, PropsWithRenderingExtensionsAttribute } from '../../../interfaces/renderProps.interface'; import type { AlertColor } from '@mui/material/Alert'; import { useRendererConfigStore } from '../../../stores'; import { interpolate } from '../../../i18n'; import DisplayUnitText from '../ItemParts/DisplayUnitText'; import ExpressionUpdateFadingIcon from '../ItemParts/ExpressionUpdateFadingIcon'; interface ChoiceAutocompleteFieldsProps extends PropsWithIsTabledAttribute, PropsWithParentIsReadOnlyAttribute, PropsWithRenderingExtensionsAttribute { qItem: QuestionnaireItem; options: Coding[]; valueCoding: Coding | null; /** Debounced search term sent to the terminology server. Fewer than 2 characters means no query has run. */ searchTerm: string; loading: boolean; feedback: { message: string; color: AlertColor } | null; readOnly: boolean; calcExpUpdated: boolean; instructionsId?: string; onInputChange: (newInput: string) => void; onValueChange: (newValue: Coding | null) => void; } function ChoiceAutocompleteField(props: ChoiceAutocompleteFieldsProps) { const { qItem, options, valueCoding, searchTerm, loading, feedback, readOnly, calcExpUpdated, instructionsId, isTabled, renderingExtensions, onInputChange, onValueChange } = props; const readOnlyVisualStyle = useRendererConfigStore.use.readOnlyVisualStyle(); const textFieldWidth = useRendererConfigStore.use.textFieldWidth(); const rendererStrings = useRendererConfigStore.use.rendererStrings(); const { displayUnit, displayPrompt, entryFormat } = renderingExtensions; return ( option.display ?? `${option.code}`} isOptionEqualToValue={(option, value) => option.code === value.code} disabled={readOnly && readOnlyVisualStyle === 'disabled'} readOnly={readOnly && readOnlyVisualStyle === 'readonly'} loading={loading} loadingText={rendererStrings.fetchingResults} noOptionsText={ searchTerm.length < 2 ? rendererStrings.autocompleteTypeToSearch : rendererStrings.terminologyNoResults } clearOnEscape autoHighlight onChange={(_, newValue) => onValueChange(newValue)} sx={{ maxWidth: !isTabled ? textFieldWidth : 3000, minWidth: 160, flexGrow: 1 }} filterOptions={(x) => x} renderInput={(params) => ( onInputChange(e.target.value)} textFieldWidth={textFieldWidth} isTabled={isTabled} size="small" placeholder={valueCoding ? undefined : entryFormat || displayPrompt} slotProps={{ input: { ...params.InputProps, readOnly: readOnly && readOnlyVisualStyle === 'readonly', startAdornment: ( <> {!valueCoding ? : null} {params.InputProps.startAdornment} ), endAdornment: ( <> {loading ? ( ) : feedback ? ( { { info: , warning: , success: , error: }[feedback.color] } ) : null} {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 }) } }} /> )} /> ); } export default ChoiceAutocompleteField;