import React, { FC, useEffect, useState } from 'react';
import Style from './Select.module.scss';
interface ISelectItem {
name: string;
categories: [];
}
interface ISelect {
placeholder?: string;
lists: any;
}
const SelectItem: FC<ISelectItem> = ({ name, categories }) => {
return (
<li className={Style.Item}>
<p className={Style.ItemName}>{name}</p>
<ul>
{categories.map((category: any) => (
<li key={category.id}>{category.name}</li>
))}
</ul>
</li>
);
};
const filterList = (searchText: string, list: any[]) => {
if (!searchText) return list;
return list.filter(({ name }) =>
name.toLowerCase().includes(searchText.toLowerCase())
);
};
const Select: FC<ISelect> = ({ placeholder, lists }) => {
const [searchInput, setSearchInput] = useState('');
const [filteredList, setFilteredList] = useState<any>();
useEffect(() => {
const Debounce = setTimeout(() => {
const sortedList = filterList(searchInput, lists);
setFilteredList(sortedList);
}, 300);
return () => clearTimeout(Debounce);
}, [searchInput]);
console.log(lists.map((list: any) => list.categories));
return (
<div className={Style.wrapper}>
<input
type="text"
placeholder={placeholder}
value={searchInput}
onChange={(e) => setSearchInput(e.target.value)}
/>
<ul>
{filteredList &&
filteredList.map((item: any) => (
<SelectItem
key={item.id}
name={item.name}
categories={item.categories}
/>
))}
</ul>
</div>
);
};
export default Select;
import React, { FC, useEffect, useState } from 'react';
import Style from './Select.module.scss';
interface ISelectItem {
name: string;
categories: [];
}
interface ISelect {
placeholder?: string;
lists: any;
}
const SelectItem: FC<ISelectItem> = ({ name, categories }) => {
return (
<li className={Style.Item}>
<p className={Style.ItemName}>{name}</p>
<ul>
{categories.map((category: any) => (
<li key={category.id}>{category.name}</li>
))}
</ul>
</li>
);
};
const filterList = (searchText: string, list: any[]) => {
if (!searchText) return list;
return list.filter(({ name }) =>
name.toLowerCase().includes(searchText.toLowerCase())
);
};
const Select: FC<ISelect> = ({ placeholder, lists }) => {
const [searchInput, setSearchInput] = useState('');
const [filteredList, setFilteredList] = useState<any>();
useEffect(() => {
const Debounce = setTimeout(() => {
const sortedList = filterList(searchInput, lists);
setFilteredList(sortedList);
}, 300);
return () => clearTimeout(Debounce);
}, [searchInput]);
console.log(lists.map((list: any) => list.categories));
return (
<div className={Style.wrapper}>
<input
type="text"
placeholder={placeholder}
value={searchInput}
onChange={(e) => setSearchInput(e.target.value)}
/>
<ul>
{filteredList &&
filteredList.map((item: any) => (
<SelectItem
key={item.id}
name={item.name}
categories={item.categories}
/>
))}
</ul>
</div>
);
};
export default Select;
Подскажи в какую сторону "копать", что бы сделать этот поиск, потому что как делать поиск только по заголовкам я понял быстро, это просто)
А как ещё и с вложенными элементами поиск делать не понятно