Convert Menu, Item, Divider, and CloseArea components to use JSX (#7547)
parent
2b4c99e031
commit
f5c496ffcd
@ -1,61 +1,87 @@ |
|||||||
const inherits = require('util').inherits |
import PropTypes from 'prop-types' |
||||||
const Component = require('react').Component |
import React from 'react' |
||||||
const h = require('react-hyperscript') |
import classnames from 'classnames' |
||||||
|
|
||||||
inherits(Menu, Component) |
/** |
||||||
function Menu () { |
* Menu component |
||||||
Component.call(this) |
* @return {Component|null} |
||||||
|
*/ |
||||||
|
function Menu (props) { |
||||||
|
const { className, children, isShowing } = props |
||||||
|
return isShowing |
||||||
|
? <div className={classnames('menu', className)}>{children}</div> |
||||||
|
: null |
||||||
} |
} |
||||||
|
|
||||||
Menu.prototype.render = function () { |
Menu.defaultProps = { |
||||||
const { className = '', children, isShowing } = this.props |
className: '', |
||||||
return isShowing |
isShowing: false, |
||||||
? h('div', { className: `menu ${className}` }, children) |
children: null, |
||||||
: h('noscript') |
|
||||||
} |
} |
||||||
|
|
||||||
inherits(Item, Component) |
Menu.propTypes = { |
||||||
function Item () { |
className: PropTypes.string, |
||||||
Component.call(this) |
children: PropTypes.node, |
||||||
|
isShowing: PropTypes.bool, |
||||||
} |
} |
||||||
|
|
||||||
Item.prototype.render = function () { |
function Item (props) { |
||||||
const { |
const { |
||||||
icon, |
icon, |
||||||
children, |
children, |
||||||
text, |
text, |
||||||
subText, |
subText, |
||||||
className = '', |
className, |
||||||
onClick, |
onClick, |
||||||
} = this.props |
} = props |
||||||
const itemClassName = `menu__item ${className} ${onClick ? 'menu__item--clickable' : ''}` |
|
||||||
const iconComponent = icon ? h('div.menu__item__icon', [icon]) : null |
const itemClassName = classnames('menu__item', className, { |
||||||
const textComponent = text ? h('div.menu__item__text', text) : null |
'menu__item--clickable': Boolean(onClick), |
||||||
const subTextComponent = subText ? h('div.menu__item__subtext', subText) : null |
}) |
||||||
|
const iconComponent = icon ? <div className="menu__item__icon">{icon}</div> : null |
||||||
|
const textComponent = text ? <div className="menu__item__text">{text}</div> : null |
||||||
|
const subTextComponent = subText ? <div className="menu__item__subtext">{subText}</div> : null |
||||||
|
|
||||||
return children |
return children |
||||||
? h('div', { className: itemClassName, onClick }, children) |
? <div className={itemClassName} onClick={onClick}>{children}</div> |
||||||
: h('div.menu__item', { className: itemClassName, onClick }, [ iconComponent, textComponent, subTextComponent ] |
: ( |
||||||
.filter(d => Boolean(d)) |
<div |
||||||
|
className={itemClassName} |
||||||
|
onClick={onClick} |
||||||
|
> |
||||||
|
{[ iconComponent, textComponent, subTextComponent ].filter(d => Boolean(d))} |
||||||
|
</div> |
||||||
) |
) |
||||||
} |
} |
||||||
|
|
||||||
inherits(Divider, Component) |
Item.defaultProps = { |
||||||
function Divider () { |
children: null, |
||||||
Component.call(this) |
icon: null, |
||||||
|
text: null, |
||||||
|
subText: null, |
||||||
|
className: '', |
||||||
|
onClick: null, |
||||||
} |
} |
||||||
|
|
||||||
Divider.prototype.render = function () { |
Item.propTypes = { |
||||||
return h('div.menu__divider') |
icon: PropTypes.node, |
||||||
|
children: PropTypes.node, |
||||||
|
text: PropTypes.node, |
||||||
|
subText: PropTypes.node, |
||||||
|
className: PropTypes.string, |
||||||
|
onClick: PropTypes.func, |
||||||
|
} |
||||||
|
|
||||||
|
function Divider () { |
||||||
|
return <div className="menu__divider" /> |
||||||
} |
} |
||||||
|
|
||||||
inherits(CloseArea, Component) |
function CloseArea ({ onClick }) { |
||||||
function CloseArea () { |
return <div className="menu__close-area" onClick={onClick} /> |
||||||
Component.call(this) |
|
||||||
} |
} |
||||||
|
|
||||||
CloseArea.prototype.render = function () { |
CloseArea.propTypes = { |
||||||
return h('div.menu__close-area', { onClick: this.props.onClick }) |
onClick: PropTypes.func.isRequired, |
||||||
} |
} |
||||||
|
|
||||||
module.exports = { Menu, Item, Divider, CloseArea } |
module.exports = { Menu, Item, Divider, CloseArea } |
||||||
|
Loading…
Reference in new issue