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

Feat: Update the design and shortcut key for SearchByMultipleOptions #9851

Open
wants to merge 38 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 31 commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
736b6fb
desing update
AdityaJ2305 Jan 8, 2025
2d583a2
Merge branch 'develop' into serachinput_design_update
AdityaJ2305 Jan 8, 2025
55f34e2
Merge branch 'develop' into serachinput_design_update
AdityaJ2305 Jan 9, 2025
b444c1e
Merge branch 'develop' into serachinput_design_update
AdityaJ2305 Jan 9, 2025
22cfa91
fix searchbyphonenumber in org patient
AdityaJ2305 Jan 10, 2025
f171fc4
fix encounter option change query search
AdityaJ2305 Jan 10, 2025
59ef699
Merge branch 'develop' into serachinput_design_update
AdityaJ2305 Jan 10, 2025
171ac9a
fix searchbyphonenumber in org patient
AdityaJ2305 Jan 10, 2025
95c06f9
fix lint issue
AdityaJ2305 Jan 10, 2025
8503430
final fix
AdityaJ2305 Jan 10, 2025
7f2735f
Merge branch 'develop' into serachinput_design_update
AdityaJ2305 Jan 10, 2025
5a920c1
Merge branch 'develop' into serachinput_design_update
AdityaJ2305 Jan 10, 2025
3cfb24a
remove alphabet shortcut
AdityaJ2305 Jan 11, 2025
17f58e9
Merge branch 'develop' into serachinput_design_update
AdityaJ2305 Jan 12, 2025
a90cb9f
remane to GovtOrganizationSelector
AdityaJ2305 Jan 12, 2025
bbbf9d7
put initialOptionIndex
AdityaJ2305 Jan 12, 2025
a8e3a38
fix focusIndexOption
AdityaJ2305 Jan 12, 2025
03518bd
Merge branch 'develop' into serachinput_design_update
AdityaJ2305 Jan 12, 2025
33698b1
reset to 0 focus on reopen
AdityaJ2305 Jan 12, 2025
2f3b779
phone in search
AdityaJ2305 Jan 12, 2025
8fa1670
fix clearsearch
AdityaJ2305 Jan 12, 2025
f41f32c
fix clearsearch
AdityaJ2305 Jan 12, 2025
ed62635
remove log
AdityaJ2305 Jan 12, 2025
45d3717
hide shortcut keys
AdityaJ2305 Jan 12, 2025
ed5ca3b
add locale
AdityaJ2305 Jan 12, 2025
e2a9fc2
reset to back commit
AdityaJ2305 Jan 12, 2025
575190b
Merge branch 'develop' into serachinput_design_update
AdityaJ2305 Jan 12, 2025
8a312c2
fix popover closing on esc
AdityaJ2305 Jan 13, 2025
a3d83b5
Merge branch 'serachinput_design_update' of https://github.com/Aditya…
AdityaJ2305 Jan 13, 2025
73b33ec
structure code
AdityaJ2305 Jan 13, 2025
5bbfb59
isPending was pending
AdityaJ2305 Jan 13, 2025
069e2a3
isPending was pending
AdityaJ2305 Jan 13, 2025
87bed7d
Merge branch 'develop' into serachinput_design_update
AdityaJ2305 Jan 13, 2025
4ef2e8a
Merge branch 'develop' into serachinput_design_update
AdityaJ2305 Jan 13, 2025
8f5d5d1
Merge branch 'develop' into serachinput_design_update
AdityaJ2305 Jan 13, 2025
cde4ae5
resolved conflicts
AdityaJ2305 Jan 13, 2025
7f98d95
resolved conflicts
AdityaJ2305 Jan 13, 2025
e2cbb84
Merge branch 'develop' into serachinput_design_update
AdityaJ2305 Jan 13, 2025
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
2 changes: 2 additions & 0 deletions public/locale/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,7 @@
"choose_file": "Upload From Device",
"choose_localbody": "Choose Local Body",
"choose_location": "Choose Location",
"choose_other_search_type": "Choose other search types",
"choose_state": "Choose State",
"claim__add_item": "Add Item",
"claim__create_claim": "Create Claim",
Expand Down Expand Up @@ -1734,6 +1735,7 @@
"scribe__reviewing_field": "Reviewing field {{currentField}} / {{totalFields}}",
"scribe_error": "Could not autofill fields",
"search": "Search",
"search_by": "Search by",
"search_by_emergency_contact_phone_number": "Search by Emergency Contact Phone Number",
"search_by_emergency_phone_number": "Search by Emergency Phone Number",
"search_by_patient_name": "Search by Patient Name",
Expand Down
291 changes: 205 additions & 86 deletions src/components/Common/SearchByMultipleFields.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,21 @@ import {
import { FieldError } from "@/components/Form/FieldValidators";
import PhoneNumberFormField from "@/components/Form/FormFields/PhoneNumberFormField";

import { isAppleDevice } from "@/Utils/utils";

AdityaJ2305 marked this conversation as resolved.
Show resolved Hide resolved
interface SearchOption {
key: string;
type: "text" | "phone";
placeholder: string;
value: string;
shortcutKey: string;
component?: React.ComponentType<HTMLDivElement>;
}

interface SearchByMultipleFieldsProps {
id: string;
options: SearchOption[];
onSearch: (key: string, value: string) => void;
initialOptionIndex?: number;
initialOptionIndex: number;
className?: string;
inputClassName?: string;
buttonClassName?: string;
Expand All @@ -66,21 +67,15 @@ const SearchByMultipleFields: React.FC<SearchByMultipleFieldsProps> = ({
}) => {
const { t } = useTranslation();
const [selectedOptionIndex, setSelectedOptionIndex] = useState(
initialOptionIndex || 0,
initialOptionIndex == -1 ? 0 : initialOptionIndex,
);
const selectedOption = options[selectedOptionIndex];
const [searchValue, setSearchValue] = useState(selectedOption.value || "");
const [open, setOpen] = useState(false);
const inputRef = useRef<HTMLInputElement>(null);
const [focusedIndex, setFocusedIndex] = useState(0);
const [error, setError] = useState<string | undefined | boolean>();

useEffect(() => {
if (!(selectedOption.type === "phone" && searchValue.length < 13)) {
setSearchValue(options[selectedOptionIndex].value);
}
}, [options]);

const isSingleOption = options.length == 1;
useEffect(() => {
if (clearSearch?.value) {
const clearinput = options
Expand All @@ -106,43 +101,42 @@ const SearchByMultipleFields: React.FC<SearchByMultipleFieldsProps> = ({
[onSearch],
);

const unselectedOptions = useMemo(
() => options.filter((option) => option.key !== selectedOption.key),
[options, selectedOption],
);

useEffect(() => {
if (open) {
setFocusedIndex(0);
}
}, [open]);

useEffect(() => {
const handleKeyDown = (e: KeyboardEvent) => {
if (
e.key === "/" &&
!(document.activeElement instanceof HTMLInputElement)
) {
if (e.key === "k" && (e.metaKey || e.ctrlKey)) {
e.preventDefault();
setOpen(true);
}
if (e.key === "Escape") {
inputRef.current?.focus();
setOpen(false);
}
if (open) {
if (e.key === "ArrowDown") {
setFocusedIndex((prevIndex) =>
prevIndex === options.length - 1 ? 0 : prevIndex + 1,
prevIndex === unselectedOptions.length - 1 ? 0 : prevIndex + 1,
);
} else if (e.key === "ArrowUp") {
setFocusedIndex((prevIndex) =>
prevIndex === 0 ? options.length - 1 : prevIndex - 1,
prevIndex === 0 ? unselectedOptions.length - 1 : prevIndex - 1,
);
} else if (e.key === "Enter") {
handleOptionChange(focusedIndex);
}

if (e.key === "Escape") {
inputRef.current?.focus();
setOpen(false);
const selectedOptionIndex = options.findIndex(
(option) => option.key === unselectedOptions[focusedIndex].key,
);
handleOptionChange(selectedOptionIndex);
AdityaJ2305 marked this conversation as resolved.
Show resolved Hide resolved
}

options.forEach((option, i) => {
if (
e.key.toLocaleLowerCase() ===
option.shortcutKey.toLocaleLowerCase() &&
open
) {
e.preventDefault();
handleOptionChange(i);
}
});
}
};

Expand Down Expand Up @@ -181,28 +175,89 @@ const SearchByMultipleFields: React.FC<SearchByMultipleFieldsProps> = ({
switch (selectedOption.type) {
case "phone":
return (
<PhoneNumberFormField
id={id}
name={selectedOption.key}
placeholder={selectedOption.placeholder}
types={["mobile", "landline"]}
{...commonProps}
errorClassName="hidden"
hideHelp={true}
onError={(error: FieldError) => setError(error)}
/>
<div className="relative">
<PhoneNumberFormField
id={id}
name={selectedOption.key}
placeholder={selectedOption.placeholder}
types={["mobile", "landline"]}
{...commonProps}
errorClassName="hidden"
hideHelp={true}
onError={(error: FieldError) => setError(error)}
/>
{!isSingleOption && (
<div className="absolute top-1/2 right-2 transform -translate-y-1/2 flex items-center space-x-2 text-xs text-gray-500">
{open ? (
<span className="border border-gray-300 rounded px-1 py-0.5 bg-white text-gray-500">
ESC
</span>
) : (
<span>
{isAppleDevice ? (
<span className="border border-gray-300 rounded px-1 py-0.5 bg-white text-gray-500">
⌘K
</span>
) : (
<div className="flex gap-1 font-medium">
<span className="border border-gray-300 rounded px-1 py-0.5 bg-white text-gray-500">
Ctrl
</span>
<span className="border border-gray-300 rounded px-1 py-0.5 bg-white text-gray-500">
K
</span>
</div>
)}
</span>
)}
</div>
)}
</div>
);
default:
return (
<Input
id={id}
type="text"
placeholder={selectedOption.placeholder}
{...commonProps}
/>
<div className="relative">
<Input
id={id}
type="text"
placeholder={selectedOption.placeholder}
{...commonProps}
/>
<div className="absolute top-1/2 right-2 transform -translate-y-1/2 flex items-center space-x-2 text-xs text-gray-500">
{open ? (
<span className="border border-gray-300 rounded px-1 py-0.5 bg-white text-gray-500">
ESC
</span>
) : (
<span>
{isAppleDevice ? (
<span className="border border-gray-300 rounded px-1 py-0.5 bg-white text-gray-500">
⌘K
</span>
) : (
<div className="flex gap-1 font-medium">
<span className="border border-gray-300 rounded px-1 py-0.5 bg-white text-gray-500">
Ctrl
</span>
<span className="border border-gray-300 rounded px-1 py-0.5 bg-white text-gray-500">
K
</span>
</div>
)}
</span>
)}
</div>
</div>
);
}
}, [selectedOption, searchValue, handleSearchChange, t, inputClassName]);
}, [
selectedOption,
searchValue,
handleSearchChange,
t,
inputClassName,
open,
]);

return (
<div
Expand All @@ -218,42 +273,93 @@ const SearchByMultipleFields: React.FC<SearchByMultipleFieldsProps> = ({
aria-haspopup="listbox"
className="flex items-center rounded-t-lg"
>
<Popover open={open} onOpenChange={setOpen}>
<PopoverTrigger asChild>
<Button
variant="ghost"
className="focus:ring-0 px-2 ml-1"
size="sm"
onClick={() => setOpen(true)}
>
<CareIcon icon="l-search" className="mr-2 text-base" />/
</Button>
</PopoverTrigger>
<PopoverContent className="w-[250px] p-0">
<Command>
<CommandList>
<CommandGroup>
{options.map((option, index) => (
<CommandItem
key={option.key}
onSelect={() => handleOptionChange(index)}
className={cn({
"bg-gray-100": focusedIndex === index,
"hover:bg-secondary-100": true,
})}
>
<CareIcon icon="l-search" className="mr-2 h-4 w-4" />
<span className="flex-1">{t(option.key)}</span>
<kbd className="ml-auto text-xs text-gray-400">
{option.shortcutKey}
</kbd>
</CommandItem>
))}
</CommandGroup>
</CommandList>
</Command>
</PopoverContent>
</Popover>
{!isSingleOption && (
<Popover open={open} onOpenChange={setOpen}>
<PopoverTrigger asChild>
<Button
variant="ghost"
className="focus:ring-0 px-2 ml-1"
size="sm"
onClick={() => setOpen(true)}
>
<CareIcon icon="l-search" className="mr-2 text-base" />
</Button>
</PopoverTrigger>
<PopoverContent className="absolute p-0">
<Command>
<CommandList>
<CommandGroup>
<div className="p-4">
<div className="mb-4">
<p className="text-sm font-medium text-gray-600">
{t("search_by")}
</p>
<div className="flex mt-2">
<Button
onClick={() => setOpen(false)}
variant="outline"
size="xs"
className="bg-primary-100 text-primary-700 hover:bg-primary-200 border-primary-400"
>
<CareIcon icon="l-check" className="mr-1" />
{t(options[selectedOptionIndex].key)}
</Button>
</div>
</div>
<hr className="border-gray-200 mb-3" />
<div>
<p className="text-xs font-semibold text-gray-500 mb-2">
{t("choose_other_search_type")}
</p>
<div className="space-y-2">
{unselectedOptions.map((option, index) => {
if (selectedOption.key === option.key) return null;

return (
<CommandItem
key={option.key}
onSelect={() =>
handleOptionChange(
options.findIndex(
(option) =>
option.key ===
unselectedOptions[index].key,
),
)
}
className={cn(
"flex items-center p-2 rounded-md cursor-pointer",
{
"bg-gray-100": focusedIndex === index,
"hover:bg-secondary-100": true,
},
)}
onMouseEnter={() => setFocusedIndex(index)}
onMouseLeave={() => setFocusedIndex(-1)}
>
<span className="flex-1 text-sm">
{t(option.key)}
</span>
{focusedIndex === index && (
<kbd
className="ml-2 border border-gray-300 rounded px-1 bg-white text-xs text-gray-500"
title="Press Enter to select"
>
⏎ Enter
</kbd>
)}
</CommandItem>
);
})}
</div>
</div>
</div>
</CommandGroup>
</CommandList>
</Command>
</PopoverContent>
</Popover>
)}
<div className="w-full">{renderSearchInput}</div>
</div>
{error && (
Expand Down Expand Up @@ -282,6 +388,19 @@ const SearchByMultipleFields: React.FC<SearchByMultipleFieldsProps> = ({
))}
</div>
)}
{searchValue.length !== 0 && (
<Button
variant="ghost"
size="sm"
className="w-full flex items-center justify-center text-muted-foreground"
onClick={() => {
setSearchValue("");
}}
>
<CareIcon icon="l-times" className="mr-2 h-4 w-4" />
{t("clear_search")}
</Button>
)}
</div>
);
};
Expand Down
Loading
Loading