|
| 1 | +import React, {useMemo, useState} from 'react'; |
| 2 | + |
| 3 | +import i18n from './i18n'; |
| 4 | +import {block} from '../../utils'; |
| 5 | +import {BlockType, ConstructorItem, FilterBlockProps, FilterItem} from '../../models'; |
| 6 | +import {Row, Col} from '../../grid'; |
| 7 | +import {BlockHeader, AnimateBlock} from '../../components'; |
| 8 | +import ButtonTabs, {ButtonTabsItemProps} from '../../components/ButtonTabs/ButtonTabs'; |
| 9 | +import {ConstructorBlocks} from '../../containers/PageConstructor/components/ConstructorBlocks'; |
| 10 | + |
| 11 | +import './FilterBlock.scss'; |
| 12 | + |
| 13 | +const b = block('filter-block'); |
| 14 | + |
| 15 | +const FilterBlock: React.FC<FilterBlockProps> = ({ |
| 16 | + title, |
| 17 | + description, |
| 18 | + tags, |
| 19 | + tagButtonSize, |
| 20 | + allTag, |
| 21 | + items, |
| 22 | + colSizes, |
| 23 | + centered, |
| 24 | + animated, |
| 25 | +}) => { |
| 26 | + const tabButtons = useMemo(() => { |
| 27 | + const allButton: ButtonTabsItemProps | undefined = allTag |
| 28 | + ? {id: null, title: typeof allTag === 'boolean' ? i18n('label-all-tag') : allTag} |
| 29 | + : undefined; |
| 30 | + const otherButtons: ButtonTabsItemProps[] | undefined = |
| 31 | + tags && tags.map((tag) => ({id: tag.id, title: tag.label})); |
| 32 | + return [...(allButton ? [allButton] : []), ...(otherButtons ? otherButtons : [])]; |
| 33 | + }, [allTag, tags]); |
| 34 | + |
| 35 | + const [selectedTag, setSelectedTag] = useState(tabButtons.length ? tabButtons[0].id : null); |
| 36 | + |
| 37 | + const actualTag: string | null = useMemo(() => { |
| 38 | + return tabButtons.length && !tabButtons.find((tab) => tab.id === selectedTag) |
| 39 | + ? tabButtons[0].id |
| 40 | + : selectedTag; |
| 41 | + }, [tabButtons, selectedTag]); |
| 42 | + |
| 43 | + const container: ConstructorItem[] = useMemo(() => { |
| 44 | + const itemsToShow: FilterItem[] = actualTag |
| 45 | + ? items.filter((item) => item.tags.includes(actualTag)) |
| 46 | + : items; |
| 47 | + return [ |
| 48 | + { |
| 49 | + type: BlockType.CardLayoutBlock, |
| 50 | + title: '', |
| 51 | + colSizes: colSizes, |
| 52 | + children: itemsToShow.map((item) => item.card), |
| 53 | + }, |
| 54 | + ]; |
| 55 | + }, [actualTag, items, colSizes]); |
| 56 | + |
| 57 | + return ( |
| 58 | + <AnimateBlock className={b()} animate={animated}> |
| 59 | + {title && ( |
| 60 | + <BlockHeader |
| 61 | + className={b('title', {centered: centered})} |
| 62 | + title={title} |
| 63 | + description={description} |
| 64 | + /> |
| 65 | + )} |
| 66 | + {tabButtons.length && ( |
| 67 | + <Row> |
| 68 | + <Col> |
| 69 | + <ButtonTabs |
| 70 | + className={b('tabs', {centered: centered})} |
| 71 | + items={tabButtons} |
| 72 | + activeTab={selectedTag} |
| 73 | + onSelectTab={setSelectedTag} |
| 74 | + tabSize={tagButtonSize} |
| 75 | + /> |
| 76 | + </Col> |
| 77 | + </Row> |
| 78 | + )} |
| 79 | + <Row className={b('block-container')}> |
| 80 | + <ConstructorBlocks items={container} /> |
| 81 | + </Row> |
| 82 | + </AnimateBlock> |
| 83 | + ); |
| 84 | +}; |
| 85 | +export default FilterBlock; |
0 commit comments