From a51d700eff9cc1230cba495d6405906b7c628f57 Mon Sep 17 00:00:00 2001 From: grafixeyehero Date: Sun, 26 May 2024 20:04:16 +0300 Subject: [PATCH 1/2] Fix onAlphaPicked callback, the query (search term) is not updated Properly --- src/components/search/SearchFields.tsx | 13 ++-- src/elements/emby-input/Input.tsx | 84 +++++++++++++------------- 2 files changed, 50 insertions(+), 47 deletions(-) diff --git a/src/components/search/SearchFields.tsx b/src/components/search/SearchFields.tsx index 16b71d3d1e..8de9f1680e 100644 --- a/src/components/search/SearchFields.tsx +++ b/src/components/search/SearchFields.tsx @@ -1,4 +1,4 @@ -import React, { type ChangeEvent, type FC, useCallback } from 'react'; +import React, { type ChangeEvent, type FC, useCallback, useRef } from 'react'; import AlphaPicker from '../alphaPicker/AlphaPickerComponent'; import Input from 'elements/emby-input/Input'; @@ -20,15 +20,19 @@ const SearchFields: FC = ({ onSearch = () => { /* no-op */ }, query }: SearchFieldsProps) => { + const inputRef = useRef(null); + const onAlphaPicked = useCallback((e: Event) => { const value = (e as CustomEvent).detail.value; + const inputValue = inputRef.current?.value || ''; if (value === 'backspace') { - onSearch(query.length ? query.substring(0, query.length - 1) : ''); + onSearch(inputValue.length ? inputValue.substring(0, inputValue.length - 1) : '' + ); } else { - onSearch(query + value); + onSearch(inputValue + value); } - }, [ onSearch, query ]); + }, [onSearch]); const onChange = useCallback((e: ChangeEvent) => { onSearch(e.target.value); @@ -43,6 +47,7 @@ const SearchFields: FC = ({ style={{ marginBottom: 0 }} > = ({ - id, - label, - className, - onBlur, - onFocus, - ...props -}) => { - const [ isFocused, setIsFocused ] = useState(false); +const Input = forwardRef( + ({ id, label, className, onBlur, onFocus, ...props }, ref) => { + const [isFocused, setIsFocused] = useState(false); - const onBlurInternal = useCallback(e => { - setIsFocused(false); - onBlur?.(e); - }, [ onBlur ]); + const onBlurInternal = useCallback( + (e) => { + setIsFocused(false); + onBlur?.(e); + }, + [onBlur] + ); - const onFocusInternal = useCallback(e => { - setIsFocused(true); - onFocus?.(e); - }, [ onFocus ]); + const onFocusInternal = useCallback( + (e) => { + setIsFocused(true); + onFocus?.(e); + }, + [onFocus] + ); - return ( - <> - + + + ); + } +); + +Input.displayName = 'Input'; export default Input; From c135be012cfcd6f63a6b6130692431cb5daa823b Mon Sep 17 00:00:00 2001 From: grafixeyehero Date: Thu, 30 May 2024 15:39:25 +0300 Subject: [PATCH 2/2] apply suggestion --- src/components/search/SearchFields.tsx | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/components/search/SearchFields.tsx b/src/components/search/SearchFields.tsx index 8de9f1680e..d2be6c26b3 100644 --- a/src/components/search/SearchFields.tsx +++ b/src/components/search/SearchFields.tsx @@ -27,8 +27,7 @@ const SearchFields: FC = ({ const inputValue = inputRef.current?.value || ''; if (value === 'backspace') { - onSearch(inputValue.length ? inputValue.substring(0, inputValue.length - 1) : '' - ); + onSearch(inputValue.length ? inputValue.substring(0, inputValue.length - 1) : ''); } else { onSearch(inputValue + value); }