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

Fix: Loses focus on User Search in users tab #9500

Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
d97b99c
Fix: Loses focus on user search
AdityaJ2305 Dec 19, 2024
ba602c0
Fix: Loses focus on user search and updated searchinput comp
AdityaJ2305 Dec 19, 2024
84181ec
Fix: cmd k shortcut trigger for userlistview
AdityaJ2305 Dec 19, 2024
7e073ed
Fix: linting issue
AdityaJ2305 Dec 19, 2024
ff1694d
Merge branch 'develop' into issue/9398/search_bar_loses_focus
AdityaJ2305 Dec 20, 2024
9ed8f11
Merge branch 'develop' into issue/9398/search_bar_loses_focus
AdityaJ2305 Dec 20, 2024
add62f6
Merge branch 'develop' into issue/9398/search_bar_loses_focus
AdityaJ2305 Dec 21, 2024
bcc8553
Undo changes
AdityaJ2305 Dec 21, 2024
6cf326a
Merge branch 'issue/9398/search_bar_loses_focus' of https://github.co…
AdityaJ2305 Dec 21, 2024
ee9293f
Fix: Issue on losing foucs
AdityaJ2305 Dec 21, 2024
77e4170
Fix: take out searchbar and tab change in parent comp
AdityaJ2305 Dec 21, 2024
885677f
Removed unnecessary code and fix import
AdityaJ2305 Dec 21, 2024
ed5bc60
Update to descriptive name
AdityaJ2305 Dec 21, 2024
728bf2c
Merge branch 'develop' into issue/9398/search_bar_loses_focus
AdityaJ2305 Dec 22, 2024
6072d41
Update to new seachinput
AdityaJ2305 Dec 22, 2024
2ffc677
Merge branch 'issue/9398/search_bar_loses_focus' of https://github.co…
AdityaJ2305 Dec 22, 2024
25f9b0a
Merge branch 'develop' into issue/9398/search_bar_loses_focus
AdityaJ2305 Dec 22, 2024
288cd78
just import fix
AdityaJ2305 Dec 22, 2024
e3abd7d
Merge branch 'issue/9398/search_bar_loses_focus' of https://github.co…
AdityaJ2305 Dec 22, 2024
95fc767
just import fix
AdityaJ2305 Dec 22, 2024
ab82beb
Merge branch 'develop' into issue/9398/search_bar_loses_focus
AdityaJ2305 Dec 23, 2024
60a0843
Fix: linting issue
AdityaJ2305 Dec 23, 2024
2c8733f
Update to shadcn tab
AdityaJ2305 Dec 24, 2024
443c192
Merge branch 'develop' into issue/9398/search_bar_loses_focus
AdityaJ2305 Dec 24, 2024
8711143
add lock file
AdityaJ2305 Dec 24, 2024
2112dd1
Merge branch 'issue/9398/search_bar_loses_focus' of https://github.co…
AdityaJ2305 Dec 24, 2024
57a57ae
add lock file
AdityaJ2305 Dec 24, 2024
0b16663
Delete package-lock.json
AdityaJ2305 Dec 24, 2024
da5368c
add lock file
AdityaJ2305 Dec 24, 2024
4ae9bee
add lock file
AdityaJ2305 Dec 24, 2024
55196d3
undo last commit
AdityaJ2305 Dec 24, 2024
218a930
reset lock file
AdityaJ2305 Dec 24, 2024
371827b
Merge branch 'develop' into issue/9398/search_bar_loses_focus
AdityaJ2305 Dec 24, 2024
8cb506d
fix cypress test
AdityaJ2305 Dec 24, 2024
05048de
fix cypress test
AdityaJ2305 Dec 24, 2024
f1e8ebf
Merge branch 'develop' into issue/9398/search_bar_loses_focus
AdityaJ2305 Dec 25, 2024
f1d6fb8
Merge branch 'develop' into issue/9398/search_bar_loses_focus
AdityaJ2305 Dec 25, 2024
a132a46
Merge branch 'develop' into issue/9398/search_bar_loses_focus
AdityaJ2305 Dec 26, 2024
4328dbb
Empty-Commit
AdityaJ2305 Dec 26, 2024
99e010e
Empty-Commit
AdityaJ2305 Dec 27, 2024
e0864a8
add strong type
AdityaJ2305 Dec 27, 2024
e3e57b4
Merge branch 'develop' into issue/9398/search_bar_loses_focus
AdityaJ2305 Dec 27, 2024
2d46fb1
Merge branch 'develop' into issue/9398/search_bar_loses_focus
AdityaJ2305 Dec 28, 2024
b98488b
Merge branch 'develop' into issue/9398/search_bar_loses_focus
AdityaJ2305 Dec 29, 2024
5ca5275
Merge branch 'develop' into issue/9398/search_bar_loses_focus
AdityaJ2305 Dec 30, 2024
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
178 changes: 89 additions & 89 deletions src/components/Form/SearchInput.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createRef, useEffect, useState } from "react";
import React, { forwardRef, useEffect, useRef, useState } from "react";
import useKeyboardShortcut from "use-keyboard-shortcut";

import CareIcon from "@/CAREUI/icons/CareIcon";
Expand All @@ -23,94 +23,94 @@
}
);

const SearchInput = ({
debouncePeriod = 500,
className = "w-full md:max-w-sm",
onChange,
name = "search",
...props
}: SearchInputProps) => {
// Debounce related
const [value, setValue] = useState(() => props.value);
useEffect(() => setValue(props.value), [props.value]);
useEffect(() => {
if (value !== props.value) {
const timeoutId = setTimeout(
() => onChange && onChange({ name, value: value || "" }),
debouncePeriod,
const SearchInput = forwardRef<HTMLInputElement, SearchInputProps>(

Check failure on line 26 in src/components/Form/SearchInput.tsx

View workflow job for this annotation

GitHub Actions / lint

Delete `··`
(

Check failure on line 27 in src/components/Form/SearchInput.tsx

View workflow job for this annotation

GitHub Actions / lint

Delete `··`
{

Check failure on line 28 in src/components/Form/SearchInput.tsx

View workflow job for this annotation

GitHub Actions / lint

Delete `··`
debouncePeriod = 500,

Check failure on line 29 in src/components/Form/SearchInput.tsx

View workflow job for this annotation

GitHub Actions / lint

Delete `··`
className = "w-full md:max-w-sm",

Check failure on line 30 in src/components/Form/SearchInput.tsx

View workflow job for this annotation

GitHub Actions / lint

Delete `··`
onChange,

Check failure on line 31 in src/components/Form/SearchInput.tsx

View workflow job for this annotation

GitHub Actions / lint

Delete `··`
name = "search",

Check failure on line 32 in src/components/Form/SearchInput.tsx

View workflow job for this annotation

GitHub Actions / lint

Delete `··`
...props

Check failure on line 33 in src/components/Form/SearchInput.tsx

View workflow job for this annotation

GitHub Actions / lint

Delete `··`
}: SearchInputProps,

Check failure on line 34 in src/components/Form/SearchInput.tsx

View workflow job for this annotation

GitHub Actions / lint

Delete `··`
ref,

Check failure on line 35 in src/components/Form/SearchInput.tsx

View workflow job for this annotation

GitHub Actions / lint

Delete `··`
) => {
// Debounce related
const [value, setValue] = useState(() => props.value);
const internalRef = useRef<HTMLInputElement>(null);
const inputRef = (ref || internalRef) as React.RefObject<HTMLInputElement>;
useEffect(() => setValue(props.value), [props.value]);

useEffect(() => {
if (value !== props.value) {
const timeoutId = setTimeout(() => {
onChange && onChange({ name, value: value || "" });
}, debouncePeriod);

return () => clearTimeout(timeoutId);
}
}, [value, debouncePeriod, name, onChange, props.value]);

// Focus shortcut logic
useKeyboardShortcut(
props.hotkey || [isAppleDevice ? "Meta" : "Control", "K"],
() => {
if (!props.secondary) {
inputRef.current?.focus();
}
},
{ overrideSystem: !props.secondary },
);

// Clear input and blur on `Escape` key
useKeyboardShortcut(
["Escape"],
() => {
if (value) {
setValue("");
}
inputRef.current?.blur();
},
{
ignoreInputFields: false,
},
);

return (
<TextFormField
labelClassName="font-medium"
{...props}
name={name}
errorClassName="hidden"
type="search"
ref={inputRef}
className={className}
leading={
props.leading || (
<CareIcon icon="l-search-alt" className="text-secondary-600 z-10" />
)
}
trailing={
props.trailing ||
(!props.secondary && (
<div className="absolute inset-y-0 right-0 hidden py-1.5 pr-1.5 md:flex">
<kbd className="inline-flex items-center rounded border border-secondary-200 bg-white px-2 font-sans text-sm font-medium text-secondary-500 focus:opacity-0">
{props.hotkeyIcon || (isAppleDevice ? "⌘K" : "Ctrl+K")}
</kbd>
</div>
))
}
trailingFocused={
<div className="absolute inset-y-0 right-0 hidden gap-1 py-1.5 pr-1.5 md:flex">
<kbd className="inline-flex items-center rounded border border-secondary-200 bg-white px-2 font-sans text-sm font-medium text-secondary-500">
Esc
</kbd>
</div>
}
value={value || ""}
onChange={({ value }) => setValue(value)}
/>
);
return () => clearTimeout(timeoutId);
}
}, [value, debouncePeriod, name, onChange, props.value]);

// Focus hotkey related
const ref = createRef<HTMLInputElement>();
useKeyboardShortcut(
props.hotkey || [isAppleDevice ? "Meta" : "Control", "K"],
() => !props.secondary && ref.current?.focus(),
{ overrideSystem: !props.secondary },
);

const shortcutKeyIcon =
props.hotkeyIcon ||
(isAppleDevice ? (
"⌘K"
) : (
<div className="flex gap-1 font-medium">
<span className="text-secondary-400">Ctrl</span>
<span className="text-secondary-500">K</span>
</div>
));

// Escape hotkey to clear related
useKeyboardShortcut(
["Escape"],
() => {
if (value) {
setValue("");
}
ref.current?.blur();
},
{
ignoreInputFields: false,
},
);

return (
<TextFormField
labelClassName="font-medium"
{...props}
name={name}
errorClassName="hidden"
type="search"
ref={ref}
className={className}
leading={
props.leading || (
<CareIcon icon="l-search-alt" className="text-secondary-600 z-10" />
)
}
trailing={
props.trailing ||
(!props.secondary && (
<div className="absolute inset-y-0 right-0 hidden py-1.5 pr-1.5 md:flex">
<kbd className="inline-flex items-center rounded border border-secondary-200 bg-white px-2 font-sans text-sm font-medium text-secondary-500 focus:opacity-0">
{shortcutKeyIcon}
</kbd>
</div>
))
}
trailingFocused={
<div className="absolute inset-y-0 right-0 hidden gap-1 py-1.5 pr-1.5 md:flex">
<kbd className="inline-flex items-center rounded border border-secondary-200 bg-white px-2 font-sans text-sm font-medium text-secondary-500">
Esc
</kbd>
</div>
}
value={value || ""}
onChange={({ value }) => setValue(value)}
/>
);
};

export default SearchInput;

export default SearchInput;
9 changes: 9 additions & 0 deletions src/components/Users/UserListAndCard.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { TFunction } from "i18next";
import { navigate } from "raviger";
import { useEffect, useRef } from "react";
import { useTranslation } from "react-i18next";

import Card from "@/CAREUI/display/Card";
Expand Down Expand Up @@ -358,6 +359,13 @@ export default function UserListView({
onTabChange,
}: UserListViewProps) {
const { t } = useTranslation();
const searchInputRef = useRef<HTMLInputElement>(null);

useEffect(() => {
if (searchValue) {
searchInputRef.current?.focus();
}
}, [searchValue]);
AdityaJ2305 marked this conversation as resolved.
Show resolved Hide resolved

return (
<>
Expand All @@ -366,6 +374,7 @@ export default function UserListView({
<SearchInput
id="search-by-username"
name="username"
ref={searchInputRef}
onChange={(e) => onSearch(e.value)}
value={searchValue}
placeholder={t("search_by_username")}
Expand Down
Loading