You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
87 lines
2.4 KiB
87 lines
2.4 KiB
4 years ago
|
import React, { useState, useEffect, useRef } from 'react';
|
||
|
import PropTypes from 'prop-types';
|
||
|
import Fuse from 'fuse.js';
|
||
|
import InputAdornment from '@material-ui/core/InputAdornment';
|
||
|
import TextField from '../../../../components/ui/text-field';
|
||
|
import { usePrevious } from '../../../../hooks/usePrevious';
|
||
4 years ago
|
|
||
|
const renderAdornment = () => (
|
||
4 years ago
|
<InputAdornment position="start" style={{ marginRight: '12px' }}>
|
||
4 years ago
|
<img src="images/search.svg" width="17" height="17" alt="" />
|
||
4 years ago
|
</InputAdornment>
|
||
4 years ago
|
);
|
||
4 years ago
|
|
||
4 years ago
|
export default function ListItemSearch({
|
||
4 years ago
|
onSearch,
|
||
|
error,
|
||
|
listToSearch = [],
|
||
|
fuseSearchKeys,
|
||
|
searchPlaceholderText,
|
||
|
defaultToAll,
|
||
|
}) {
|
||
4 years ago
|
const fuseRef = useRef();
|
||
|
const [searchQuery, setSearchQuery] = useState('');
|
||
4 years ago
|
|
||
|
const handleSearch = (newSearchQuery) => {
|
||
4 years ago
|
setSearchQuery(newSearchQuery);
|
||
|
const fuseSearchResult = fuseRef.current.search(newSearchQuery);
|
||
4 years ago
|
onSearch({
|
||
|
searchQuery: newSearchQuery,
|
||
4 years ago
|
results:
|
||
|
defaultToAll && newSearchQuery === '' ? listToSearch : fuseSearchResult,
|
||
4 years ago
|
});
|
||
|
};
|
||
4 years ago
|
|
||
|
useEffect(() => {
|
||
|
if (!fuseRef.current) {
|
||
|
fuseRef.current = new Fuse(listToSearch, {
|
||
|
shouldSort: true,
|
||
|
threshold: 0.45,
|
||
|
location: 0,
|
||
|
distance: 100,
|
||
|
maxPatternLength: 32,
|
||
|
minMatchCharLength: 1,
|
||
|
keys: fuseSearchKeys,
|
||
4 years ago
|
});
|
||
4 years ago
|
}
|
||
4 years ago
|
}, [fuseSearchKeys, listToSearch]);
|
||
4 years ago
|
|
||
4 years ago
|
const previousListToSearch = usePrevious(listToSearch ?? []);
|
||
4 years ago
|
useEffect(() => {
|
||
4 years ago
|
if (
|
||
|
fuseRef.current &&
|
||
|
searchQuery &&
|
||
|
previousListToSearch !== listToSearch
|
||
|
) {
|
||
4 years ago
|
fuseRef.current.setCollection(listToSearch);
|
||
|
const fuseSearchResult = fuseRef.current.search(searchQuery);
|
||
|
onSearch({ searchQuery, results: fuseSearchResult });
|
||
4 years ago
|
}
|
||
4 years ago
|
}, [listToSearch, searchQuery, onSearch, previousListToSearch]);
|
||
4 years ago
|
|
||
|
return (
|
||
|
<TextField
|
||
|
data-testid="search-list-items"
|
||
|
className="searchable-item-list__search"
|
||
|
placeholder={searchPlaceholderText}
|
||
|
type="text"
|
||
|
value={searchQuery}
|
||
|
onChange={(e) => handleSearch(e.target.value)}
|
||
|
error={error}
|
||
|
fullWidth
|
||
|
startAdornment={renderAdornment()}
|
||
|
autoComplete="off"
|
||
|
autoFocus
|
||
|
/>
|
||
4 years ago
|
);
|
||
4 years ago
|
}
|
||
|
|
||
|
ListItemSearch.propTypes = {
|
||
|
onSearch: PropTypes.func,
|
||
|
error: PropTypes.string,
|
||
|
listToSearch: PropTypes.array.isRequired,
|
||
|
fuseSearchKeys: PropTypes.arrayOf(PropTypes.object).isRequired,
|
||
|
searchPlaceholderText: PropTypes.string,
|
||
|
defaultToAll: PropTypes.bool,
|
||
4 years ago
|
};
|