1
0
Fork 0
mirror of https://github.com/jellyfin/jellyfin-web synced 2025-03-30 19:56:21 +00:00
jellyfin-web/src/components/search/SearchFields.tsx

96 lines
3 KiB
TypeScript
Raw Normal View History

import debounce from 'lodash-es/debounce';
import React, { FunctionComponent, useEffect, useMemo, useRef } from 'react';
2021-05-28 15:58:41 -04:00
import AlphaPicker from '../alphaPicker/AlphaPickerComponent';
import globalize from '../../scripts/globalize';
import 'material-design-icons-iconfont';
import '../../elements/emby-input/emby-input';
import '../../assets/css/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: `<input
is="emby-input"
class="searchfields-txtSearch"
type="text"
data-keyboard="true"
placeholder="${globalize.translate('Search')}"
autocomplete="off"
maxlength="40"
autofocus
/>`
});
const normalizeInput = (value = '') => value.trim();
2021-05-28 15:58:41 -04:00
type SearchFieldsProps = {
onSearch?: () => void
};
// eslint-disable-next-line @typescript-eslint/no-empty-function
2021-06-15 12:05:33 +02:00
const SearchFields: FunctionComponent<SearchFieldsProps> = ({ onSearch = () => {} }: SearchFieldsProps) => {
2022-02-15 23:47:59 +03:00
const element = useRef<HTMLDivElement>(null);
2022-02-16 22:01:13 +03:00
const getSearchInput = () => element?.current?.querySelector<HTMLInputElement>('.searchfields-txtSearch');
const debouncedOnSearch = useMemo(() => debounce(onSearch, 400), [onSearch]);
2021-05-28 15:58:41 -04:00
useEffect(() => {
getSearchInput()?.addEventListener('input', e => {
debouncedOnSearch(normalizeInput(e.target?.value));
});
getSearchInput()?.focus();
2021-05-28 15:58:41 -04:00
return () => {
debouncedOnSearch.cancel();
2021-05-28 15:58:41 -04:00
};
}, [debouncedOnSearch]);
2021-05-28 15:58:41 -04:00
2022-02-18 14:27:39 +03:00
const onAlphaPicked = (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;
2021-05-28 15:58:41 -04:00
}
searchInput.dispatchEvent(new CustomEvent('input', { bubbles: true }));
};
2021-05-28 15:58:41 -04:00
return (
<div
className='padded-left padded-right searchFields'
ref={element}
>
<div className='searchFieldsInner flex align-items-center justify-content-center'>
<span className='searchfields-icon material-icons search' />
<div
className='inputContainer flex-grow'
style={{ marginBottom: 0 }}
dangerouslySetInnerHTML={createInputElement()}
/>
</div>
{layoutManager.tv && !browser.tv &&
<AlphaPicker onAlphaPicked={onAlphaPicked} />
}
</div>
2021-05-28 15:58:41 -04:00
);
};
2021-06-01 15:19:02 -04:00
export default SearchFields;