|
| 1 | +import React, { PropTypes } from 'react'; |
| 2 | + |
| 3 | +// Convert string to spinal tap case |
| 4 | +// http://codereview.stackexchange.com/questions/109899/convert-string-to-spinal-case |
| 5 | +const toSpinalTapCase = str => |
| 6 | + str |
| 7 | + .replace(/(?!^)([A-Z])/g, ' $1') |
| 8 | + .replace(/[_\s]+(?=[a-zA-Z])/g, '-') |
| 9 | + .toLowerCase(); |
| 10 | + |
| 11 | +const renderMenu = items => |
| 12 | + <ul className="menu-list"> |
| 13 | + { items.map(item => |
| 14 | + <li className="menu-item"> |
| 15 | + <a className="menu-item-link" href={item.url || toSpinalTapCase(item.text)}> |
| 16 | + {item.iconCode && <span className={`menu-item-icon icon-budicon-${item.iconCode}`} />} |
| 17 | + <span className="text">{item.text}</span> |
| 18 | + </a> |
| 19 | + <ul className="menu-sublist"> |
| 20 | + { item.children && item.children.map(subitem => |
| 21 | + <li className="menu-subitem"> |
| 22 | + <a |
| 23 | + className="menu-subitem-link" |
| 24 | + href={subitem.url || toSpinalTapCase(subitem.text)} |
| 25 | + > |
| 26 | + {subitem.text} |
| 27 | + </a> |
| 28 | + </li> |
| 29 | + )} |
| 30 | + </ul> |
| 31 | + </li> |
| 32 | + )} |
| 33 | + </ul>; |
| 34 | + |
| 35 | +/** |
| 36 | + * Sidebar: Styleguide sidebar with drop drown sections. |
| 37 | + */ |
| 38 | +const Sidebar = ({ header, items }) => |
| 39 | + <div className="a0r-sidebar"> |
| 40 | + <header className="a0r-sidebar-header"> |
| 41 | + { header || |
| 42 | + <h1> |
| 43 | + <a href="/"> |
| 44 | + <img src="https://cdn.auth0.com/styleguide/1.0.0/img/badge.svg" alt="Auth0 badge" width="30" height="" /> |
| 45 | + </a> |
| 46 | + </h1> |
| 47 | + } |
| 48 | + </header> |
| 49 | + <nav className="a0r-sidebar-menu">{renderMenu(items)}</nav> |
| 50 | + <footer></footer> |
| 51 | + </div>; |
| 52 | + |
| 53 | +Sidebar.propTypes = { |
| 54 | + /** |
| 55 | + * DOM element to replace default header content. |
| 56 | + */ |
| 57 | + header: PropTypes.element, |
| 58 | + /** |
| 59 | + * List of objects representing the sidebar items |
| 60 | + */ |
| 61 | + items: PropTypes.arrayOf(PropTypes.shape({ |
| 62 | + text: PropTypes.string.isRequired, |
| 63 | + url: PropTypes.string, |
| 64 | + iconCode: PropTypes.number, |
| 65 | + children: PropTypes.arrayOf({ |
| 66 | + text: PropTypes.string.isRequired, |
| 67 | + url: PropTypes.string |
| 68 | + }).isRequired |
| 69 | + })).isRequired |
| 70 | +}; |
| 71 | + |
| 72 | +export default Sidebar; |
0 commit comments