I'm trying to render grid with multiple toggling filters. I'm Using react hooks.
This is data array:
const items = [
{
name: 'hotel1',
type: 'hotel'
},
{
name: 'bar1',
type: 'bar'
},{
name: 'entertainment1',
type: 'entertainment'
},{
name: 'equipment1',
type: 'equipment'
},
{
name: 'shop1',
type: 'shop'
}
]
const initialFilters = [
{
id: 1,
active: false,
type: 'bar'
},
{
id: 2,
active: false,
type: 'shop'
},
{
id: 3,
active: false,
type: 'hotel'
},
{
id: 4,
active: false,
type: 'entertainment'
},
{
id: 5,
active: false,
type: 'equipment'
},
];
const [data, setData] = useState(items);
const [filterItems, setFilteredItems] = useState(initialFilters);
currently I'm filtering with single key that is passed
const mainFilter = (key) => {
setData(items.filter(x => x.type === key));
}
and filter buttons with grid item names are rendered:
return (
<div>
<ul>
{filterItems.map(x =>
<li key={x.type}><a
className={x.active == true ? 'active' : ''}
onClick={() => mainFilter(x.type)}>
{x.type}
</a></li>
)}
</ul>
<div>{data.map(item => <div>{item.name}</div>}</div>
</div>
)
I need to get the functionality where when pressed for example shop filter, it should only leave items with type shop. if you press bar filter, it should only leave items with bar an shop and it should work all the way to all 5 filters. if none are selected, it should show full array.
I've tried converting logic from this:
https://gist.github.com/jherax/f11d669ba286f21b7a2dcff69621eb72#file-filterplainarray-js
but no luck yet