Skip to content

Commit 8b89795

Browse files
committed
Search albums by terms
1 parent 7b54795 commit 8b89795

File tree

1 file changed

+11
-10
lines changed

1 file changed

+11
-10
lines changed

src/AlbumList.tsx

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,26 +26,28 @@ interface AlbumListProps {
2626
const AlbumList: FC<AlbumListProps> = ({onClick}) => {
2727
const { collection, album } = useParams();
2828
const { data = [], isFetching } = useGetAlbumsQuery({collection}, {skip: collection === undefined});
29-
const [ searchTerm, setSearchTerm ] = useState<string>("");
29+
const [ searchTerm, setSearchTerm ] = useState<string[]>([]);
3030

3131
const albums = useMemo(() => searchTerm.length < 1 ? data :
32-
data.filter((album) => album.name.toLowerCase().includes(searchTerm)),
33-
[searchTerm, data]);
32+
data.filter(album => {
33+
const low = album.name.toLowerCase();
34+
return searchTerm.reduce((acc, term) => acc && low.includes(term), true);
35+
}), [searchTerm, data]);
3436

3537
const onSearch = (event: React.ChangeEvent<HTMLInputElement>) => {
36-
setSearchTerm(event.target.value.toLowerCase());
38+
setSearchTerm(event.target.value.toLowerCase().split(/\s+/));
3739
};
3840
const clearSearch = () => {
39-
setSearchTerm("");
41+
setSearchTerm([]);
4042
};
4143

4244
const renderRow = ({ index, style }: ListChildComponentProps<AlbumType>) => {
43-
const a = albums[index];
45+
const current = albums[index];
4446
return (
45-
<ListItem onClick={onClick} style={style} key={a.name} disablePadding>
46-
<ListItemButton component={Link} to={`/${collection}/${a.name}`} selected={a.name === album}>
47+
<ListItem onClick={onClick} style={style} key={current.name} disablePadding>
48+
<ListItemButton component={Link} to={`/${collection}/${current.name}`} selected={current.name === album}>
4749
<ListItemText>
48-
<Typography noWrap>{a.name}</Typography>
50+
<Typography noWrap>{current.name}</Typography>
4951
</ListItemText>
5052
</ListItemButton>
5153
</ListItem>
@@ -78,7 +80,6 @@ const AlbumList: FC<AlbumListProps> = ({onClick}) => {
7880
return <>
7981
<TextField
8082
label="Search albums"
81-
value={searchTerm}
8283
onChange={onSearch}
8384
fullWidth
8485
variant="filled"

0 commit comments

Comments
 (0)