/* * 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, { useContext, useMemo } from 'react'; import { RepeatGroupContainerStack } from '../RepeatItem/RepeatItem.styles'; import Box from '@mui/material/Box'; import GroupItem from '../GroupItem/GroupItem'; import { RepeatGroupInstanceContext } from '../../../contexts/RepeatGroupInstanceContext'; import { appendRepeatInstanceIndex } from '../../../utils/validateErrorKey'; import type { PropsWithParentIsReadOnlyAttribute, PropsWithQrItemChangeHandler } from '../../../interfaces/renderProps.interface'; import type { QuestionnaireItem, QuestionnaireResponseItem } from 'fhir/r4'; import RemoveItemButton from './RemoveItemButton'; import useReadOnly from '../../../hooks/useReadOnly'; interface RepeatGroupItemProps extends PropsWithQrItemChangeHandler, PropsWithParentIsReadOnlyAttribute { qItem: QuestionnaireItem; repeatGroupIndex: number; /** This instance's index within the QuestionnaireResponse, or null if it isn't in the QR yet */ qrInstanceIndex: number | null; answeredQrItem: QuestionnaireResponseItem; nullableQrItem: QuestionnaireResponseItem | null; numOfRepeatGroups: number; groupCardElevation: number; onRemoveItem: () => void; } function RepeatGroupItem(props: RepeatGroupItemProps) { const { qItem, repeatGroupIndex, qrInstanceIndex, answeredQrItem, nullableQrItem, numOfRepeatGroups, groupCardElevation, parentIsReadOnly, onRemoveItem, onQrItemChange } = props; const readOnly = useReadOnly(qItem, parentIsReadOnly, repeatGroupIndex); // Append this instance's index to the enclosing repeat instance path so that descendant items look // up their own instance-scoped validation errors (see useValidationFeedback). Uses the // QuestionnaireResponse index, not repeatGroupIndex, because that is what validation walks. // Memoised to keep the provided context value referentially stable, so that memoising a descendant // item in future actually lets it bail out of a re-render. const parentRepeatInstancePath = useContext(RepeatGroupInstanceContext); const repeatInstancePath = useMemo( () => appendRepeatInstanceIndex(parentRepeatInstancePath, qrInstanceIndex), [parentRepeatInstancePath, qrInstanceIndex] ); return ( ); } export default RepeatGroupItem;