Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Medication Request Enhancements #9962

Merged
merged 7 commits into from
Jan 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions public/locale/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -942,6 +942,7 @@
"encounter_suggestion_edit_disallowed": "Not allowed to switch to this option in edit consultation",
"encounters": "Encounters",
"end_datetime": "End Date/Time",
"end_dose": "End Dose",
"end_time": "End Time",
"enter_aadhaar_number": "Enter a 12-digit Aadhaar ID",
"enter_aadhaar_otp": "Enter OTP sent to the registered mobile with Aadhaar",
Expand Down Expand Up @@ -1932,6 +1933,7 @@
"start_date": "Start Date",
"start_datetime": "Start Date/Time",
"start_dosage": "Start Dosage",
"start_dose": "Start Dose",
"start_new_clinical_encounter": "Start a new clinical encounter",
"start_review": "Start Review",
"start_time": "Start Time",
Expand Down Expand Up @@ -1964,6 +1966,7 @@
"tachycardia": "Tachycardia",
"tag_name": "Tag Name",
"tag_slug": "Tag Slug",
"taper_titrate_dosage": "Taper & Titrate Dosage",
"target_dosage": "Target Dosage",
"template_deleted": "Template has been deleted",
"test_type": "Type of test done",
Expand Down Expand Up @@ -2030,6 +2033,8 @@
"unit_mo": "Months",
"unit_ms": "Milliseconds",
"unit_s": "Seconds",
"unit_taper": "Taper",
"unit_titrate": "Titrate",
"unit_tsp": "Tsp",
"unit_unit(s)": "Unit(s)",
"unit_wk": "Weeks",
Expand Down
170 changes: 170 additions & 0 deletions src/components/Common/ComboboxQuantityInput.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
"use client";

import { Check } from "lucide-react";
import * as React from "react";

import { cn } from "@/lib/utils";

import {
Command,
CommandEmpty,
CommandGroup,
CommandItem,
CommandList,
} from "@/components/ui/command";
import { Input } from "@/components/ui/input";
import {
Popover,
PopoverContent,
PopoverTrigger,
} from "@/components/ui/popover";

import {
DOSAGE_UNITS_CODES,
DosageQuantity,
} from "@/types/emr/medicationRequest";

interface Props {
quantity?: DosageQuantity;
onChange: (quantity: DosageQuantity) => void;
disabled?: boolean;
placeholder?: string;
autoFocus?: boolean;
}

export function ComboboxQuantityInput({
quantity,
onChange,
disabled,
placeholder = "Enter a number...",
autoFocus,
}: Props) {
const [open, setOpen] = React.useState(false);
const [inputValue, setInputValue] = React.useState(
quantity?.value.toString() || "",
);
const [selectedUnit, setSelectedUnit] = React.useState(quantity?.unit);
const inputRef = React.useRef<HTMLInputElement>(null);
const [activeIndex, setActiveIndex] = React.useState<number>(-1);

const showDropdown = /^\d+$/.test(inputValue);

const handleInputChange = (e: React.ChangeEvent<HTMLInputElement>) => {
const value = e.target.value;
if (value === "" || /^\d+$/.test(value)) {
setInputValue(value);
setOpen(true);
setActiveIndex(0);
if (value && selectedUnit) {
onChange({
value: parseInt(value, 10),
unit: selectedUnit,
});
}
}
};

const handleKeyDown = (e: React.KeyboardEvent<HTMLInputElement>) => {
if (!showDropdown) return;

if (e.key === "ArrowDown") {
e.preventDefault();
setOpen(true);
setActiveIndex((prev) =>
prev === -1
? 0
: prev < DOSAGE_UNITS_CODES.length - 1
? prev + 1
: prev,
);
} else if (e.key === "ArrowUp") {
e.preventDefault();
setActiveIndex((prev) => (prev > 0 ? prev - 1 : prev));
} else if (e.key === "Enter") {
e.preventDefault();
if (activeIndex >= 0 && activeIndex < DOSAGE_UNITS_CODES.length) {
const unit = DOSAGE_UNITS_CODES[activeIndex];
setSelectedUnit(unit);
setOpen(false);
setActiveIndex(-1);
onChange({ value: parseInt(inputValue, 10), unit });
}
}
};

return (
<div className="relative flex w-full lg:max-w-[200px] flex-col gap-1">
<Popover open={open && showDropdown} onOpenChange={setOpen}>
<PopoverTrigger asChild>
<div className="relative">
<Input
ref={inputRef}
type="text"
inputMode="numeric"
pattern="\d*"
value={inputValue}
Comment on lines +104 to +105
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Update pattern attribute to allow decimal input

If decimal quantities are permitted, the pattern attribute should be updated to reflect the new input format.

Apply this diff to update the pattern attribute:

-  pattern="\d*"
+  pattern="\d*\.?\d*"

Note that when using the pattern attribute, special characters like backslashes may need to be escaped differently in JSX.

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
pattern="\d*"
value={inputValue}
pattern="\d*\.?\d*"
value={inputValue}

onChange={handleInputChange}
onKeyDown={handleKeyDown}
placeholder={placeholder}
className={cn("w-full text-sm", selectedUnit && "pr-16")}
disabled={disabled}
autoFocus={autoFocus}
/>
{selectedUnit && (
<div className="absolute right-1.5 top-1/2 -translate-y-1/2 text-sm text-muted-foreground">
{selectedUnit.display}
</div>
)}
</div>
</PopoverTrigger>
<PopoverContent
className="w-auto p-0"
align="start"
onOpenAutoFocus={(e) => {
e.preventDefault();
inputRef.current?.focus();
}}
>
<Command>
<CommandList>
<CommandEmpty>No results found.</CommandEmpty>
<CommandGroup>
{DOSAGE_UNITS_CODES.map((unit, index) => (
<CommandItem
key={unit.code}
value={unit.code}
onSelect={() => {
setSelectedUnit(unit);
setOpen(false);
setActiveIndex(-1);
inputRef.current?.focus();
onChange({ value: parseInt(inputValue, 10), unit });
}}
className={cn(
"flex items-center gap-2",
activeIndex === index && "bg-gray-100",
)}
>
<div>
{inputValue} {unit.display}
</div>
<Check
className={cn(
"ml-auto h-4 w-4",
selectedUnit?.code === unit.code
? "opacity-100"
: "opacity-0",
)}
/>
</CommandItem>
))}
</CommandGroup>
</CommandList>
</Command>
</PopoverContent>
</Popover>
</div>
);
}

export default ComboboxQuantityInput;
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,8 @@ const PrescriptionEntry = ({
<div className="mt-1.5 flex flex-wrap items-baseline gap-x-3 gap-y-1 text-xs">
{instruction.dose_and_rate && (
<span className="font-medium">
{instruction.dose_and_rate.type === "calculated" ? (
{/* TODO: Rebuild Medicine Administration Sheet */}
{/* {instruction.dose_and_rate.type === "calculated" ? (
<span>
{instruction.dose_and_rate.dose_range?.low.value}{" "}
{instruction.dose_and_rate.dose_range?.low.unit} →{" "}
Expand All @@ -296,7 +297,7 @@ const PrescriptionEntry = ({
{instruction.dose_and_rate.dose_quantity?.value}{" "}
{instruction.dose_and_rate.dose_quantity?.unit}
</span>
)}
)} */}
</span>
)}
{instruction.route && (
Expand Down
Loading
Loading