/* * 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 type { QuestionnaireItem, QuestionnaireResponseItem } from 'fhir/r4'; import { RepeatGroupInstanceContext } from '../../../contexts/RepeatGroupInstanceContext'; import { appendRepeatInstanceIndex } from '../../../utils/validateErrorKey'; import type { PropsWithParentIsReadOnlyAttribute } from '../../../interfaces/renderProps.interface'; import type { TableRowProps } from '@mui/material/TableRow'; import TableRow from '@mui/material/TableRow'; import SelectRowButton from './SelectRowButton'; import GroupTableRowCells from './GroupTableRowCells'; import RemoveRowButton from './RemoveRowButton'; import type { GroupTableRowModel } from '../../../interfaces/groupTable.interface'; import DragIndicator from '@mui/icons-material/DragIndicator'; import TableCell from '@mui/material/TableCell'; import Box from '@mui/material/Box'; import { Draggable } from 'react-beautiful-dnd'; import { StyledGroupTableRow } from './Table.styles'; import { useRendererConfigStore } from '../../../stores'; interface GroupTableRowProps extends PropsWithParentIsReadOnlyAttribute, TableRowProps { rowId: string; index: number; tableQItem: QuestionnaireItem; answeredQrItem: QuestionnaireResponseItem; nullableQrItem: QuestionnaireResponseItem | null; readOnly: boolean; hoverDisabled: boolean; tableRows: GroupTableRowModel[]; itemIsSelected: boolean; /** This row's index within the QuestionnaireResponse, or null if it isn't in the QR */ qrRowIndex: number | null; qItemsIndexMap: Record; visibleItemLabels: string[]; calculatedColumnWidths: { width: string; isFixed: boolean }[]; showExtraGTableInteractions: boolean; onRowChange: (newQrRow: QuestionnaireResponseItem, index: number) => void; onRemoveRow: (index: number) => void; onSelectRow: (nanoId: string) => void; } function GroupTableRow(props: GroupTableRowProps) { const { rowId, index, tableQItem, answeredQrItem, nullableQrItem, readOnly, hoverDisabled, tableRows, itemIsSelected, qrRowIndex, qItemsIndexMap, visibleItemLabels, calculatedColumnWidths, showExtraGTableInteractions, onRowChange, onRemoveRow, onSelectRow } = props; const dragRowLabel = useRendererConfigStore.use.rendererStrings().dragRow; // Append this row's index to the enclosing repeat instance path so that the row's cells look up // their own instance-scoped validation errors (see useValidationFeedback). Uses the // QuestionnaireResponse index, not the rendered row index, 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, qrRowIndex), [parentRepeatInstancePath, qrRowIndex] ); if (showExtraGTableInteractions) { return ( {(draggableProvided, snapshot) => ( <> onSelectRow(rowId)} /> onRowChange(newQrGroup, index)} /> onRemoveRow(index)} /> )} ); } return ( onRowChange(newQrGroup, index)} /> ); } export default GroupTableRow;