-
Notifications
You must be signed in to change notification settings - Fork 518
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
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
891eacd
feat: add medication request enhancements with combobox quantity input
amjithtitus09 f13acbb
Merge remote-tracking branch 'origin/develop' into medication_req_enh…
amjithtitus09 4f03293
Fix dose type
amjithtitus09 3316ce7
Fix Scroll Issue
amjithtitus09 361953b
Merge branch 'develop' of https://github.com/ohcnetwork/care_fe into …
amjithtitus09 18efb6e
add medication request questionnaire to structured form data
bodhish 0230f5c
Clean up medication request type
bodhish File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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,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} | ||
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; |
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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Update
pattern
attribute to allow decimal inputIf decimal quantities are permitted, the
pattern
attribute should be updated to reflect the new input format.Apply this diff to update the
pattern
attribute:Note that when using the
pattern
attribute, special characters like backslashes may need to be escaped differently in JSX.📝 Committable suggestion