From 84181ec2305536080616b6f6596ff3e4001cf3be Mon Sep 17 00:00:00 2001 From: AdityaJ2305 Date: Fri, 20 Dec 2024 00:17:19 +0530 Subject: [PATCH] Fix: cmd k shortcut trigger for userlistview --- src/components/Form/SearchInput.tsx | 178 ++++++++++++++-------------- 1 file changed, 87 insertions(+), 91 deletions(-) diff --git a/src/components/Form/SearchInput.tsx b/src/components/Form/SearchInput.tsx index 194c543184d..77adb193730 100644 --- a/src/components/Form/SearchInput.tsx +++ b/src/components/Form/SearchInput.tsx @@ -23,98 +23,94 @@ type SearchInputProps = TextFormFieldProps & { } ); -const SearchInput = forwardRef( - ( - { - debouncePeriod = 500, - className = "w-full md:max-w-sm", - onChange, - name = "search", - ...props - }: SearchInputProps, - ref, - ) => { - // 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, - ); - return () => clearTimeout(timeoutId); - } - }, [value, debouncePeriod, name, onChange, props.value]); - // Focus hotkey related - const inputRef = useRef(null); - useKeyboardShortcut( - props.hotkey || [isAppleDevice ? "Meta" : "Control", "K"], - () => !props.secondary && inputRef.current?.focus(), - { overrideSystem: !props.secondary }, - ); - - const shortcutKeyIcon = - props.hotkeyIcon || - (isAppleDevice ? ( - "⌘K" - ) : ( -
- Ctrl - K -
- )); - - // Escape hotkey to clear related - useKeyboardShortcut( - ["Escape"], - () => { - if (value) { - setValue(""); - } - inputRef.current?.blur(); - }, + const SearchInput = forwardRef( + ( { - ignoreInputFields: false, - }, - ); - - return ( - - ) + debouncePeriod = 500, + className = "w-full md:max-w-sm", + onChange, + name = "search", + ...props + }: SearchInputProps, + ref, + ) => { + // Debounce related + const [value, setValue] = useState(() => props.value); + const internalRef = useRef(null); + const inputRef = (ref || internalRef) as React.RefObject; + useEffect(() => setValue(props.value), [props.value]); + + useEffect(() => { + if (value !== props.value) { + const timeoutId = setTimeout(() => { + onChange && onChange({ name, value: value || "" }); + }, debouncePeriod); + + return () => clearTimeout(timeoutId); } - trailing={ - props.trailing || - (!props.secondary && ( -
- - {shortcutKeyIcon} + }, [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 ( + + ) + } + trailing={ + props.trailing || + (!props.secondary && ( +
+ + {props.hotkeyIcon || (isAppleDevice ? "⌘K" : "Ctrl+K")} + +
+ )) + } + trailingFocused={ +
+ + Esc
- )) - } - trailingFocused={ -
- - Esc - -
- } - value={value || ""} - onChange={({ value }) => setValue(value)} - /> - ); - }, -); - -export default SearchInput; + } + value={value || ""} + onChange={({ value }) => setValue(value)} + /> + ); + }, + ); + + export default SearchInput;