Skip to content

Commit

Permalink
display units and min/max from number component instead of field labe…
Browse files Browse the repository at this point in the history
…l, add test concept with units and range
  • Loading branch information
D-matz committed Jan 9, 2025
1 parent 035c318 commit a8f625a
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 9 deletions.
26 changes: 25 additions & 1 deletion src/components/inputs/number/number.component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,30 @@ import { useFormProviderContext } from '../../../provider/form-provider';
import FieldLabel from '../../field-label/field-label.component';
import { isEmpty } from '../../../validators/form-validator';

const extractFieldUnitsAndRange = (concept) => {
if (!concept) {
return '';
}

let unitsDisplay = '';
if (concept.units) {
unitsDisplay = ` (${concept.units})`;
}

let rangeDisplay = '';
if (concept.lowAbsolute != null && concept.hiAbsolute != null) {
rangeDisplay = ` (${concept.lowAbsolute}-${concept.hiAbsolute})`;
}
else if (concept.lowAbsolute != null) {
rangeDisplay = ` (Min ${concept.lowAbsolute})`;
}
else if (concept.hiAbsolute != null) {
rangeDisplay = ` (Max ${concept.hiAbsolute})`;
}

return unitsDisplay + rangeDisplay;
};

const NumberField: React.FC<FormFieldInputProps> = ({ field, value, errors, warnings, setFieldValue }) => {
const { t } = useTranslation();
const [lastBlurredValue, setLastBlurredValue] = useState(value);
Expand Down Expand Up @@ -61,7 +85,7 @@ const NumberField: React.FC<FormFieldInputProps> = ({ field, value, errors, warn
id={field.id}
invalid={errors.length > 0}
invalidText={errors[0]?.message}
label={<FieldLabel field={field} />}
label={<FieldLabel field={field} customLabel={t(field.label) + extractFieldUnitsAndRange(field.meta?.concept)} />}
max={Number(field.questionOptions.max) || undefined}
min={Number(field.questionOptions.min) || undefined}
name={field.id}
Expand Down
30 changes: 30 additions & 0 deletions src/components/inputs/number/number.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,25 @@ const numberFieldMock = {
readonly: false,
};

const numberFieldMockWithUnitsAndRange = {
label: 'Weight',
type: 'obs',
id: 'weight',
questionOptions: {
rendering: 'number',
},
meta: {
concept: {
units: 'kg',
lowAbsolute: 0,
hiAbsolute: 200,
}
},
isHidden: false,
isDisabled: false,
readonly: false,
};

const renderNumberField = async (props) => {
await act(() => render(<NumberField {...props} />));
};
Expand Down Expand Up @@ -104,4 +123,15 @@ describe('NumberField Component', () => {
const inputElement = screen.getByLabelText('Weight(kg):') as HTMLInputElement;
expect(inputElement).toBeDisabled();
});

it('renders units and range', async () => {
await renderNumberField({
field: numberFieldMockWithUnitsAndRange,
value: '',
errors: [],
warnings: [],
setFieldValue: jest.fn(),
});
expect(screen.getByLabelText('Weight (kg) (0-200)')).toBeInTheDocument();
});
});
16 changes: 8 additions & 8 deletions src/hooks/useFormFieldsMeta.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@ export function useFormFieldsMeta(rawFormFields: FormField[], concepts: OpenmrsR
const matchingConcept = findConceptByReference(field.questionOptions.concept, concepts);
field.questionOptions.concept = matchingConcept ? matchingConcept.uuid : field.questionOptions.concept;
field.label = field.label ? field.label : matchingConcept?.display;
if(matchingConcept)
{
if(matchingConcept.units)
{
field.label = field.label + " (" + matchingConcept.units + ")";

if (matchingConcept) {
if (matchingConcept.lowAbsolute != undefined && matchingConcept.hiAbsolute != undefined) {
field.questionOptions.min = matchingConcept.lowAbsolute;
field.questionOptions.max = matchingConcept.hiAbsolute;
}
if(matchingConcept.lowAbsolute && matchingConcept.hiAbsolute)
{
field.label = field.label + " (" + matchingConcept.lowAbsolute + "-" + matchingConcept.hiAbsolute + ")";
else if (matchingConcept.lowAbsolute != undefined) {
field.questionOptions.min = matchingConcept.lowAbsolute;
}
else if (matchingConcept.hiAbsolute != undefined) {
field.questionOptions.max = matchingConcept.hiAbsolute;
}
}
Expand Down

0 comments on commit a8f625a

Please sign in to comment.