UNPKG

2.82 kBJavaScriptView Raw
1import classNames from 'classnames';
2import React from 'react';
3import PropTypes from 'prop-types';
4import createChainedFunction from 'tinper-bee-core/lib/createChainedFunction';
5
6const propTypes = {
7 onClick: PropTypes.func,
8 /**
9 * The toggle content, if left empty it will render the default toggle (seen above).
10 */
11 show: PropTypes.bool,
12 children: PropTypes.node,
13};
14
15const contextTypes = {
16 u_navbar: PropTypes.shape({
17 expanded: PropTypes.bool,
18 onToggle: PropTypes.func,
19 }),
20};
21
22const defaultProps = {
23 clsPrefix: 'u-navbar-toggle',
24 show:false
25}
26
27class MenuToggle extends React.Component {
28
29 constructor(props){
30 super(props);
31 this.state = {
32 toggleState:false
33 }
34 //this.handleRender = this.handleRender.bind(this);
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 //const navbarProps = this.context.u_navbar || { bsClass: 'navbar' };
47 //console.log(navbarProps.onToggle, navbarProps.expanded);
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 //!this.context.u_navbar.expanded && 'collapsed',
60 };
61
62 if (children) {
63 return (
64 <button {...buttonProps}>
65 {children}
66 </button>
67 );
68 }
69 //当show存在时,渲染左侧静态面包按钮
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
101MenuToggle.propTypes = propTypes;
102MenuToggle.defaultProps = defaultProps;
103MenuToggle.contextTypes = contextTypes;
104
105export default MenuToggle;