|
| 1 | +import React, {createRef, MouseEvent} from 'react'; |
| 2 | +import block from 'bem-cn-lite'; |
| 3 | +import {Col, Grid, Row} from '../../../../grid'; |
| 4 | +import OutsideClick from '../../../OutsideClick/OutsideClick'; |
| 5 | +import Control from '../../../Control/Control'; |
| 6 | + |
| 7 | +import Logo from '../Logo/Logo'; |
| 8 | + |
| 9 | +import {HeaderData, NavigationLogo} from '../../../../models/navigation'; |
| 10 | +import Navigation from '../Navigation/Navigation'; |
| 11 | +import MobileNavigation from '../MobileNavigation/MobileNavigation'; |
| 12 | +import NavigationItem from '../NavigationItem/NavigationItem'; |
| 13 | + |
| 14 | +import {NavigationClose, NavigationOpen} from '../../../../icons'; |
| 15 | + |
| 16 | +import './Header.scss'; |
| 17 | + |
| 18 | +const b = block('header'); |
| 19 | + |
| 20 | +export interface HeaderProps { |
| 21 | + logo: NavigationLogo; |
| 22 | + data: HeaderData; |
| 23 | +} |
| 24 | + |
| 25 | +interface HeaderState { |
| 26 | + isSidebarOpened: boolean; |
| 27 | + activeItemIndex: number; |
| 28 | +} |
| 29 | + |
| 30 | +class Header extends React.Component<HeaderProps, HeaderState> { |
| 31 | + ref = createRef(); |
| 32 | + state = { |
| 33 | + isSidebarOpened: false, |
| 34 | + activeItemIndex: -1, |
| 35 | + }; |
| 36 | + |
| 37 | + render() { |
| 38 | + return ( |
| 39 | + <Grid className={b()}> |
| 40 | + <Row> |
| 41 | + <Col> |
| 42 | + <header className={b('wrapper')}> |
| 43 | + {this.renderLogo()} |
| 44 | + {this.renderLeft()} |
| 45 | + {this.renderRight()} |
| 46 | + {this.renderMobileNavigation()} |
| 47 | + </header> |
| 48 | + </Col> |
| 49 | + </Row> |
| 50 | + </Grid> |
| 51 | + ); |
| 52 | + } |
| 53 | + |
| 54 | + private renderLeft() { |
| 55 | + const {activeItemIndex} = this.state; |
| 56 | + const {leftItems} = this.props.data; |
| 57 | + |
| 58 | + return ( |
| 59 | + leftItems && ( |
| 60 | + <div className={b('navigation-container')}> |
| 61 | + <Navigation |
| 62 | + className={b('navigation')} |
| 63 | + links={leftItems} |
| 64 | + activeItemIndex={activeItemIndex} |
| 65 | + onActiveItemChange={this.onActiveItemChange} |
| 66 | + /> |
| 67 | + </div> |
| 68 | + ) |
| 69 | + ); |
| 70 | + } |
| 71 | + |
| 72 | + private renderLogo() { |
| 73 | + const {logo} = this.props; |
| 74 | + |
| 75 | + if (!logo) { |
| 76 | + return null; |
| 77 | + } |
| 78 | + |
| 79 | + return ( |
| 80 | + <div className={b('left')}> |
| 81 | + <Logo {...logo} className={b('logo')} /> |
| 82 | + </div> |
| 83 | + ); |
| 84 | + } |
| 85 | + |
| 86 | + private renderRight() { |
| 87 | + return ( |
| 88 | + <div className={b('right')}> |
| 89 | + {this.renderMobileMenuButton()} |
| 90 | + {this.renderRightItems()} |
| 91 | + </div> |
| 92 | + ); |
| 93 | + } |
| 94 | + |
| 95 | + private renderRightItems() { |
| 96 | + const {rightItems} = this.props.data; |
| 97 | + |
| 98 | + return ( |
| 99 | + rightItems && ( |
| 100 | + <div className={b('buttons')}> |
| 101 | + {rightItems.map((button) => ( |
| 102 | + <NavigationItem key={button.text} data={button} className={b('button')} /> |
| 103 | + ))} |
| 104 | + </div> |
| 105 | + ) |
| 106 | + ); |
| 107 | + } |
| 108 | + |
| 109 | + private renderMobileMenuButton() { |
| 110 | + const {isSidebarOpened} = this.state; |
| 111 | + const iconProps = {icon: isSidebarOpened ? NavigationClose : NavigationOpen, iconSize: 36}; |
| 112 | + |
| 113 | + return ( |
| 114 | + <Control |
| 115 | + className={b('mobile-menu-button')} |
| 116 | + onClick={(e: MouseEvent) => { |
| 117 | + e.stopPropagation(); |
| 118 | + this.onSidebarOpenedChange(!isSidebarOpened); |
| 119 | + }} |
| 120 | + size="l" |
| 121 | + {...iconProps} |
| 122 | + /> |
| 123 | + ); |
| 124 | + } |
| 125 | + |
| 126 | + private renderMobileNavigation() { |
| 127 | + const {leftItems, rightItems} = this.props.data; |
| 128 | + const {isSidebarOpened, activeItemIndex} = this.state; |
| 129 | + |
| 130 | + return ( |
| 131 | + <OutsideClick onOutsideClick={() => this.onSidebarOpenedChange(false)}> |
| 132 | + <MobileNavigation |
| 133 | + topItems={leftItems} |
| 134 | + bottomItems={rightItems} |
| 135 | + isOpened={isSidebarOpened} |
| 136 | + activeItemIndex={activeItemIndex} |
| 137 | + onActiveItemChange={this.onActiveItemChange} |
| 138 | + onClose={this.hideSidebar} |
| 139 | + /> |
| 140 | + </OutsideClick> |
| 141 | + ); |
| 142 | + } |
| 143 | + |
| 144 | + private onActiveItemChange = (index: number) => { |
| 145 | + this.setState({activeItemIndex: index}); |
| 146 | + }; |
| 147 | + |
| 148 | + private onSidebarOpenedChange = (isSidebarOpened: boolean) => { |
| 149 | + this.setState({isSidebarOpened}); |
| 150 | + }; |
| 151 | + |
| 152 | + private hideSidebar = () => { |
| 153 | + this.setState({isSidebarOpened: false}); |
| 154 | + }; |
| 155 | +} |
| 156 | + |
| 157 | +export default Header; |
0 commit comments