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.js

91 lines
2.7 KiB
JavaScript
Raw Normal View History

import debounce from 'lodash-es/debounce';
2021-05-28 15:58:41 -04:00
import PropTypes from 'prop-types';
import React, { 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
2021-06-01 15:19:02 -04:00
const SearchFields = ({ onSearch = () => {} }) => {
const element = useRef(null);
const getSearchInput = () => element?.current?.querySelector('.searchfields-txtSearch');
const debouncedOnSearch = useMemo(() => debounce(onSearch, 400), []);
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
};
}, []);
const onAlphaPicked = e => {
const value = e.detail.value;
const searchInput = getSearchInput();
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
SearchFields.propTypes = {
2021-05-28 15:58:41 -04:00
onSearch: PropTypes.func
};
2021-06-01 15:19:02 -04:00
export default SearchFields;