1+ import { Coding } from 'fhir/r4b' ;
12import React from 'react' ;
3+ import { useField } from 'react-final-form' ;
24import { QuestionItemProps , useQuestionnaireResponseFormContext } from 'sdc-qrf' ;
35
46import { QuestionField } from './field' ;
@@ -10,59 +12,63 @@ export function QuestionQuantity({ parentPath, questionItem }: QuestionItemProps
1012 const fieldPath = [ ...parentPath , linkId , 0 , 'value' , 'Quantity' ] ;
1113 const fieldName = fieldPath . join ( '.' ) ;
1214
13- const [ selectedUnit , setSelectedUnit ] = React . useState ( unitOption ?. [ 0 ] ) ;
15+ const { input, meta } = useField ( fieldName ) ;
16+ const [ selectedUnit , setSelectedUnit ] = React . useState < Coding | undefined > ( {
17+ code : input . value ?. code ,
18+ system : input . value ?. system ,
19+ display : input . value ?. unit ,
20+ } ) ;
1421
15- return (
16- < QuestionField name = { fieldName } >
17- { ( { input, meta } ) => (
18- < >
19- < QuestionLabel questionItem = { questionItem } htmlFor = { fieldName } />
20- < div style = { { display : 'flex' , alignItems : 'center' } } >
21- < input
22- type = "number"
23- id = { fieldName }
24- readOnly = { qrfContext . readOnly || readOnly || hidden }
25- onChange = { ( e ) => {
26- const value = e . target . value
27- ? parseFloat ( e . target . value )
28- : undefined ;
22+ const InputWithUnit = ( ) => {
23+ return (
24+ < >
25+ < QuestionLabel questionItem = { questionItem } htmlFor = { fieldName } />
26+ < div style = { { display : 'flex' , alignItems : 'center' } } >
27+ < input
28+ type = "number"
29+ id = { fieldName }
30+ readOnly = { qrfContext . readOnly || readOnly || hidden }
31+ value = { input . value ?. value }
32+ onChange = { ( e ) => {
33+ console . log ( 'e.target.value' , e . target . value ) ;
34+ input . onChange ( {
35+ value : e . target . value ,
36+ unit : selectedUnit ?. display ,
37+ system : selectedUnit ?. system ,
38+ code : selectedUnit ?. code ,
39+ } ) ;
40+ } }
41+ />
42+ < select
43+ style = { { marginLeft : '8px' } }
44+ value = { selectedUnit ?. display }
45+ disabled = { qrfContext . readOnly || readOnly || hidden }
46+ onChange = { ( e ) => {
47+ const newUnit = ( unitOption ?? [ ] ) . find (
48+ ( unit ) => unit . display === e . target . value ,
49+ ) ;
50+ if ( newUnit ) {
51+ setSelectedUnit ( newUnit ) ;
2952 input . onChange ( {
30- value : value ,
31- unit : selectedUnit ? .display ,
32- system : selectedUnit ? .system ,
33- code : selectedUnit ? .code ,
53+ value : input . value ?. value ,
54+ unit : newUnit . display ,
55+ system : newUnit . system ,
56+ code : newUnit . code ,
3457 } ) ;
35- } }
36- />
37- < select
38- style = { { marginLeft : '8px' } }
39- value = { selectedUnit ?. display }
40- disabled = { qrfContext . readOnly || readOnly || hidden }
41- onChange = { ( e ) => {
42- const newUnit = unitOption ?. find (
43- ( unit ) => unit . display === e . target . value ,
44- ) ;
45- if ( newUnit ) {
46- setSelectedUnit ( newUnit ) ;
47- input . onChange ( {
48- value : input . value ?. value ,
49- unit : newUnit . display ,
50- system : newUnit . system ,
51- code : newUnit . code ,
52- } ) ;
53- }
54- } }
55- >
56- { unitOption ?. map ( ( unit ) => (
57- < option key = { unit . code } value = { unit . display } >
58- { unit . display }
59- </ option >
60- ) ) }
61- </ select >
62- </ div >
63- { meta . touched && meta . error && < span > { meta . error } </ span > }
64- </ >
65- ) }
66- </ QuestionField >
67- ) ;
58+ }
59+ } }
60+ >
61+ { ( unitOption ?? [ ] ) . map ( ( unit ) => (
62+ < option key = { unit . code } value = { unit . display } >
63+ { unit . display }
64+ </ option >
65+ ) ) }
66+ </ select >
67+ </ div >
68+ { meta . touched && meta . error && < span > { meta . error } </ span > }
69+ </ >
70+ ) ;
71+ } ;
72+
73+ return < QuestionField name = { fieldName } component = { InputWithUnit } /> ;
6874}
0 commit comments