pizza-online
87 строк · 2.4 Кб
1'use client'
2
3import React from 'react'
4import {Button, Input} from '../ui'
5import {FilterCheckbox, FilterCheckboxProps} from './filter-checkbox'
6import {Title} from './title'
7
8type Props = {
9title: string
10items: FilterCheckboxProps[]
11defaultItems?: FilterCheckboxProps[]
12limit?: number
13searchInputPlaceholder?: string
14className?: string
15onChange?: (values: string[]) => void
16defaultValue?: string[]
17}
18
19export const FilterIngredients: React.FC<Props> = ({
20title,
21items,
22defaultItems,
23limit = 4,
24searchInputPlaceholder = 'Поиск...',
25className,
26onChange,
27defaultValue,
28}) => {
29const [showAllIngredients, setShowAllIngredients] = React.useState<number>(limit)
30const [searchIngredients, setSearchIngredients] = React.useState<string>('')
31
32const handleSearchIngredients = (e: React.ChangeEvent<HTMLInputElement>) => {
33setSearchIngredients(e.target.value)
34}
35
36const filtered = items.filter(item =>
37item.text.toLowerCase().includes(searchIngredients.toLowerCase())
38)
39
40return (
41<div className={className}>
42<div className="flex justify-between items-center mb-3">
43<Title className="font-bold" size="xs" text={title} />
44{items.length > showAllIngredients ? (
45<Button
46variant="ghost"
47className="text-primary cursor-pointer"
48onClick={() => setShowAllIngredients(items.length)}
49>
50показать все +{items.slice(4).length}
51</Button>
52) : (
53<Button
54variant="ghost"
55className="text-primary cursor-pointer"
56onClick={() => setShowAllIngredients(limit)}
57>
58скрыть
59</Button>
60)}
61</div>
62
63{items.length === showAllIngredients && (
64<div className="mb-5">
65<Input
66className="bg-gray-50 border-none"
67onChange={handleSearchIngredients}
68placeholder={searchInputPlaceholder}
69/>
70</div>
71)}
72
73<ul className="flex flex-col gap-4 max-h-96 pr-2 overflow-auto scrollbar">
74{filtered.slice(0, showAllIngredients).map((item, i) => (
75<li key={`${i}${item.text}`}>
76<FilterCheckbox
77text={item.text}
78value={item.value}
79onCheckedChange={e => console.log(e)}
80checked={false}
81/>
82</li>
83))}
84</ul>
85</div>
86)
87}
88