/* * 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 { useState } from 'react'; import useDateValidation from '../../../../hooks/useDateValidation'; import useReadOnly from '../../../../hooks/useReadOnly'; import useValidationFeedbackSeverity from '../../../../hooks/useValidationFeedbackSeverity'; import type { BaseItemProps } from '../../../../interfaces/renderProps.interface'; import { useQuestionnaireStore } from '../../../../stores'; import { createEmptyQrItem, getQRItemId } from '../../../../utils/qrItem'; import { FullWidthFormComponentBox } from '../../../Box.styles'; import ItemFieldGrid from '../../ItemParts/ItemFieldGrid'; import ItemLabel from '../../ItemParts/ItemLabel'; import { parseFhirDateToDisplayDate, parseInputDateToFhirDate, validateDateInput } from '../utils/parseDate'; import CustomDateField from './CustomDateField'; function CustomDateItem(props: BaseItemProps) { const { qItem, qrItem, isRepeated, isTabled, renderingExtensions, parentIsReadOnly, calcExpUpdated, onQrItemChange } = props; const onFocusLinkId = useQuestionnaireStore.use.onFocusLinkId(); const readOnly = useReadOnly(qItem, parentIsReadOnly); const { displayPrompt, entryFormat, displayInstructions } = renderingExtensions; // Init input value const answerKey = getQRItemId(qrItem?.answer?.[0]?.id); const qrDate = qrItem ?? createEmptyQrItem(qItem, answerKey); let valueDate: string = ''; if (qrDate.answer) { if (qrDate.answer[0].valueDate) { valueDate = qrDate.answer[0].valueDate; } else if (qrDate.answer[0].valueDateTime) { valueDate = qrDate.answer[0].valueDateTime; } } const { displayDate, dateParseFail } = parseFhirDateToDisplayDate(valueDate); const [input, setInput] = useState(displayDate); const [focused, setFocused] = useState(false); // Perform validation checks // Constraint and required feedback takes priority; fall back to date format feedback. const { feedback: validationFeedback, feedbackSeverity } = useValidationFeedbackSeverity( qItem, undefined ); const dateValidationFeedback = useDateValidation(input, dateParseFail); const feedback = validationFeedback || dateValidationFeedback; // Generate instruction ID if instructions exist and there's no feedback const instructionsId = displayInstructions && !feedback ? `instructions-${qItem.linkId}` : undefined; function handleSelectDate(selectedDate: string) { setInput(selectedDate); onQrItemChange({ ...createEmptyQrItem(qItem, answerKey), answer: [{ id: answerKey, valueDate: parseInputDateToFhirDate(selectedDate) }] }); } function handleInputChange(newInput: string) { setInput(newInput); if (newInput === '') { onQrItemChange(createEmptyQrItem(qItem, answerKey)); } if (!validateDateInput(newInput)) { return; } onQrItemChange({ ...createEmptyQrItem(qItem, answerKey), answer: [{ id: answerKey, valueDate: parseInputDateToFhirDate(newInput) }] }); } if (isRepeated) { return ( ); } return ( onFocusLinkId(qItem.linkId)}> } fieldChildren={ } feedback={feedback ?? undefined} /> ); } export default CustomDateItem;