1 | import classNames from 'classnames';
|
2 | import React from 'react';
|
3 | import PropTypes from 'prop-types';
|
4 | import createChainedFunction from 'tinper-bee-core/lib/createChainedFunction';
|
5 |
|
6 | const propTypes = {
|
7 | onClick: PropTypes.func,
|
8 | |
9 |
|
10 |
|
11 | show: PropTypes.bool,
|
12 | children: PropTypes.node,
|
13 | };
|
14 |
|
15 | const contextTypes = {
|
16 | u_navbar: PropTypes.shape({
|
17 | expanded: PropTypes.bool,
|
18 | onToggle: PropTypes.func,
|
19 | }),
|
20 | };
|
21 |
|
22 | const defaultProps = {
|
23 | clsPrefix: 'u-navbar-toggle',
|
24 | show:false
|
25 | }
|
26 |
|
27 | class MenuToggle extends React.Component {
|
28 |
|
29 | constructor(props){
|
30 | super(props);
|
31 | this.state = {
|
32 | toggleState:false
|
33 | }
|
34 |
|
35 | }
|
36 |
|
37 | handleClick() {
|
38 | const {expanded,onToggle } = this.context.u_navbar;
|
39 | this.setState({toggleState:!this.state.toggleState});
|
40 | if (onToggle) {
|
41 | onToggle(!expanded);
|
42 | }
|
43 | }
|
44 | render() {
|
45 | const { onClick, className, children,clsPrefix, show,...props } = this.props;
|
46 |
|
47 |
|
48 |
|
49 | const buttonProps = {
|
50 | type: 'button',
|
51 | ...props,
|
52 | onClick: createChainedFunction(onClick, this.handleClick.bind(this)),
|
53 | className: classNames(
|
54 | className,
|
55 | clsPrefix,
|
56 | show && 'show',
|
57 |
|
58 | )
|
59 |
|
60 | };
|
61 |
|
62 | if (children) {
|
63 | return (
|
64 | <button {...buttonProps}>
|
65 | {children}
|
66 | </button>
|
67 | );
|
68 | }
|
69 |
|
70 | return (
|
71 | <div>
|
72 |
|
73 | {show && this.state.toggleState && (
|
74 | <button {...buttonProps} >
|
75 | <span className="sr-only">Toggle navigation</span>
|
76 | <span className="icon-bar" />
|
77 | <span className="icon-bar" />
|
78 | <span className="icon-bar" />
|
79 | </button>
|
80 | )}
|
81 | {show && !this.state.toggleState && (
|
82 | <button {...buttonProps}>
|
83 | <span className="uf uf-arrow-left"></span>
|
84 | </button>
|
85 | )}
|
86 | {!show && !this.state.toggleState && (
|
87 | <button {...buttonProps} >
|
88 | <span className="sr-only">Toggle navigation</span>
|
89 | <span className="icon-bar" />
|
90 | <span className="icon-bar" />
|
91 | <span className="icon-bar" />
|
92 | </button>
|
93 | )}
|
94 | </div>
|
95 | )
|
96 |
|
97 |
|
98 | }
|
99 | }
|
100 |
|
101 | MenuToggle.propTypes = propTypes;
|
102 | MenuToggle.defaultProps = defaultProps;
|
103 | MenuToggle.contextTypes = contextTypes;
|
104 |
|
105 | export default MenuToggle;
|