import debounce from 'lodash-es/debounce'; import React, { FunctionComponent, useCallback, useEffect, useMemo, useRef } from 'react'; import AlphaPicker from '../alphaPicker/AlphaPickerComponent'; import globalize from '../../scripts/globalize'; import 'material-design-icons-iconfont'; import '../../elements/emby-input/emby-input'; import '../../styles/flexstyles.scss'; import './searchfields.scss'; import layoutManager from '../layoutManager'; import browser from '../../scripts/browser'; // There seems to be some compatibility issues here between // React and our legacy web components, so we need to inject // them as an html string for now =/ const createInputElement = () => ({ __html: `` }); const normalizeInput = (value = '') => value.trim(); type SearchFieldsProps = { query: string, onSearch?: (query: string) => void }; // eslint-disable-next-line @typescript-eslint/no-empty-function const SearchFields: FunctionComponent = ({ onSearch = () => {}, query }: SearchFieldsProps) => { const element = useRef(null); const getSearchInput = () => element?.current?.querySelector('.searchfields-txtSearch'); const debouncedOnSearch = useMemo(() => debounce(onSearch, 400), [onSearch]); const initSearchInput = getSearchInput(); if (initSearchInput) { initSearchInput.value = query; } useEffect(() => { getSearchInput()?.addEventListener('input', e => { debouncedOnSearch(normalizeInput((e.target as HTMLInputElement).value)); }); getSearchInput()?.focus(); return () => { debouncedOnSearch.cancel(); }; }, [debouncedOnSearch]); const onAlphaPicked = useCallback((e: Event) => { const value = (e as CustomEvent).detail.value; const searchInput = getSearchInput(); if (!searchInput) { console.error('Unexpected null reference'); return; } if (value === 'backspace') { const currentValue = searchInput.value; searchInput.value = currentValue.length ? currentValue.substring(0, currentValue.length - 1) : ''; } else { searchInput.value += value; } searchInput.dispatchEvent(new CustomEvent('input', { bubbles: true })); }, []); return ( {layoutManager.tv && !browser.tv && } ); }; export default SearchFields;