-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b853c59
commit e070e47
Showing
2 changed files
with
67 additions
and
0 deletions.
There are no files selected for viewing
65 changes: 65 additions & 0 deletions
65
web/src/components/BaseQuestionnaireResponseForm/components/quantity.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import React from 'react'; | ||
import { QuestionItemProps, useQuestionnaireResponseFormContext } from 'sdc-qrf/src'; | ||
|
||
import { QuestionField } from './field'; | ||
import { QuestionLabel } from './label'; | ||
|
||
export function QuestionQuantity({ parentPath, questionItem }: QuestionItemProps) { | ||
const qrfContext = useQuestionnaireResponseFormContext(); | ||
const { linkId, readOnly, hidden, unitOption } = questionItem; | ||
const fieldPath = [...parentPath, linkId, 0, 'value', 'decimal']; | ||
const fieldName = fieldPath.join('.'); | ||
|
||
const [selectedUnit, setSelectedUnit] = React.useState(unitOption?.[0]); | ||
|
||
return ( | ||
<QuestionField name={fieldName}> | ||
{({ input, meta }) => ( | ||
<> | ||
<QuestionLabel questionItem={questionItem} htmlFor={fieldName} /> | ||
<div style={{ display: 'flex', alignItems: 'center' }}> | ||
<input | ||
type="number" | ||
id={fieldName} | ||
readOnly={qrfContext.readOnly || readOnly || hidden} | ||
onChange={(e) => { | ||
input.onChange({ | ||
value: e.target.value, | ||
unit: selectedUnit?.display, | ||
system: selectedUnit?.system, | ||
code: selectedUnit?.code, | ||
}); | ||
}} | ||
/> | ||
<select | ||
style={{ marginLeft: '8px' }} | ||
value={selectedUnit?.display} | ||
disabled={qrfContext.readOnly || readOnly || hidden} | ||
onChange={(e) => { | ||
const newUnit = unitOption?.find( | ||
(unit) => unit.display === e.target.value, | ||
); | ||
if (newUnit) { | ||
setSelectedUnit(newUnit); | ||
input.onChange({ | ||
value: input.value?.value, | ||
unit: newUnit.display, | ||
system: newUnit.system, | ||
code: newUnit.code, | ||
}); | ||
} | ||
}} | ||
> | ||
{unitOption?.map((unit) => ( | ||
<option key={unit.code} value={unit.display}> | ||
{unit.display} | ||
</option> | ||
))} | ||
</select> | ||
</div> | ||
{meta.touched && meta.error && <span>{meta.error}</span>} | ||
</> | ||
)} | ||
</QuestionField> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters