diff --git a/package.json b/package.json index 83a1a2819..da661c6c1 100644 --- a/package.json +++ b/package.json @@ -29,7 +29,7 @@ "access": "public" }, "dependencies": { - "@carbon/react": ">1.47.0 <1.50.0", + "@carbon/react": "1.40.0", "classnames": "^2.5.1", "lodash-es": "^4.17.21", "react-error-boundary": "^4.0.13", diff --git a/src/adapters/program-state-adapter.ts b/src/adapters/program-state-adapter.ts index 971e55685..f3ec6bc16 100644 --- a/src/adapters/program-state-adapter.ts +++ b/src/adapters/program-state-adapter.ts @@ -29,7 +29,7 @@ export const ProgramStateAdapter: FormFieldValueAdapter = { .filter((state) => !state.endDate) .find((state) => state.state.programWorkflow?.uuid === field.questionOptions.workflowUuid)?.state; field.meta = { ...(field.meta || {}), previousValue: currentState }; - return currentState.uuid; + return currentState?.uuid; } return null; }, diff --git a/src/components/renderer/field/form-field-renderer.component.tsx b/src/components/renderer/field/form-field-renderer.component.tsx index efeb1744d..b32a1ef68 100644 --- a/src/components/renderer/field/form-field-renderer.component.tsx +++ b/src/components/renderer/field/form-field-renderer.component.tsx @@ -1,4 +1,4 @@ -import React, { useEffect, useState } from 'react'; +import React, { useEffect, useRef, useState } from 'react'; import { type FormField, type RenderType, @@ -39,6 +39,16 @@ export const FormFieldRenderer = ({ field, valueAdapter, repeatOptions }: FormFi const [warnings, setWarnings] = useState([]); const [historicalValue, setHistoricalValue] = useState(null); const context = useFormProviderContext(); + const [initialValue, setInitialValue] = useState(null); // State to store initial value + const hasMounted = useRef(false); // Set initial value when the component mounts + + useEffect(() => { + if (field.type === 'programState') { + const initialVal = valueAdapter.getInitialValue(field, null, context); + setInitialValue(initialVal); + hasMounted.current = true; + } + }, []); const { methods: { control, getValues, getFieldState }, @@ -67,7 +77,7 @@ export const FormFieldRenderer = ({ field, valueAdapter, repeatOptions }: FormFi } }); } - if (sessionMode === 'enter' && (field.historicalExpression || context.previousDomainObjectValue)) { + if (field.historicalExpression || context.previousDomainObjectValue) { try { context.processor.getHistoricalValue(field, context).then((value) => { setHistoricalValue(value); @@ -76,16 +86,6 @@ export const FormFieldRenderer = ({ field, valueAdapter, repeatOptions }: FormFi console.error(error); } } - - if (field.type === 'programState' && field.questionOptions.enablePreviousValue) { - try { - context.processor.getHistoricalValue(field, context).then((value) => { - setHistoricalValue({value: value.value, display: value.display}); - }); - } catch (error) { - console.error(error); - } - } }, []); useEffect(() => { @@ -158,51 +158,56 @@ export const FormFieldRenderer = ({ field, valueAdapter, repeatOptions }: FormFi /> ); } + return ( ( -
- { - onChange(val); - onAfterChange(val); - onBlur(); - }} - /> - {isUnspecifiedSupported(field) && ( -
- {field.unspecified && ( - { + const valueToUse = hasMounted.current ? initialValue : value; + return ( +
+ { + onChange(val); + onAfterChange(val); + onBlur(); + hasMounted.current = false; + }} + /> + {isUnspecifiedSupported(field) && ( +
+ {field.unspecified && ( + + )} +
+ )} + {historicalValue?.value && ( +
+ - )} -
- )} - {historicalValue?.value && ( -
- -
- )} -
- )} +
+ )} +
+ ); + }} />
);