diff --git a/src/Nav/Nav.css b/src/Nav/Nav.css index cfc6bdec..6f1dc9e7 100644 --- a/src/Nav/Nav.css +++ b/src/Nav/Nav.css @@ -6,6 +6,16 @@ height: auto; } +.horizontal { + display: flex; + flex-direction: row; +} + .nav a { text-decoration: none; +} + +.horizontal.nav .nav li:not(:first-child) { + position: absolute; + z-index: 9998; } \ No newline at end of file diff --git a/src/Nav/Nav.js b/src/Nav/Nav.js index af29de9f..5af3949e 100644 --- a/src/Nav/Nav.js +++ b/src/Nav/Nav.js @@ -10,8 +10,11 @@ export class Nav extends React.Component { super(props); this.state = { "activeIndex": props.activeIndex, // The index of selected menu item. - "collapsed": props.collapsed // Whether or not sub-nav menu collapsed. + "collapsed": props.collapsed, // Whether or not sub-nav menu collapsed. + "subNavItemHeight": 0, // the NavItem's height in subNav + "subNavItemMaxWidth": 0 }; + this.subNavItemRef = React.createRef(); } UNSAFE_componentWillReceiveProps(nextProps) { @@ -34,50 +37,130 @@ export class Nav extends React.Component { } }; - _buildNavByData = () => { - const { className, data, ignoreActive, onClick, style } = this.props; + _setDimension = (height, width) => { + console.log("height", height); + console.log("width", width); + if (width > 0 && width > this.state.subNavItemMaxWidth) { + this.setState({ + "subNavItemHeight": height, + "subNavItemMaxWidth": width + }); + } else { + this.setState({ + "subNavItemHeight": height + }); + } + }; + + // use this ternary helper to avoid nested ternary in JSX + // not working for _buidNavByData + widthStyleTernaryHelper = (cond1, cond2, child, index, data) => { + let tempIndex = index; + if (cond1 && cond2) { + if (data) { + tempIndex += 2; + } + return { + "transform": `translateY(${(tempIndex - 1) * + this.state.subNavItemHeight}px)`, + "width": `${this.state.subNavItemMaxWidth}px`, + ...child.style + }; + } else if (cond1 && !cond2) { + if (data) { + tempIndex += 2; + } + return { + "transform": `translateY(${(tempIndex - 1) * + this.state.subNavItemHeight}px)`, + ...child.style + }; + } else { + return child.style; + } + }; + + _buildNavByData = data => { + const { + className, + ignoreActive, + onClick, + style, + subNav, + horizontal + } = this.props; return ( ); @@ -90,15 +173,21 @@ export class Nav extends React.Component { data, ignoreActive, style, - subNav + subNav, + horizontal } = this.props; let styleName; if (data) { - return this._buildNavByData(); + return this._buildNavByData(data); } // Avoid to append styleName to ul if rendering from json data. styleName = - className.indexOf("ra_Nav__nav") === -1 ? { "styleName": cx("nav") } : {}; + className.indexOf("ra_Nav__nav") === -1 + ? horizontal && typeof subNav === "undefined" + ? { "styleName": cx("nav", "horizontal") } + : { "styleName": cx("nav") } + : {}; + return ( @@ -151,21 +254,26 @@ Nav.propTypes = { "data": PropTypes.array, /** Set to true if you intend to set an active style externally. */ "ignoreActive": PropTypes.bool, - /** Function that will be executed when onClick event occurs. */ + /** Function that will be executed when onClick event occurs. + * @ignore + * */ "onClick": PropTypes.func, /** Pass inline styling here. */ "style": PropTypes.object, /** Set true If is a sub-Nav container, not a public prop. * @ignore */ - "subNav": PropTypes.bool + "subNav": PropTypes.bool, + /** Define whether the Nav is vertical or horizontal, Nav is vertical by default */ + "horizontal": PropTypes.bool }; Nav.defaultProps = { "className": "", "collapsed": false, "data": null, - "ignoreActive": false + "ignoreActive": false, + "horizontal": false }; export default CSSModules(Nav, styles, { "allowMultiple": true }); diff --git a/src/Nav/README.md b/src/Nav/README.md index 7538e7ca..21c00975 100644 --- a/src/Nav/README.md +++ b/src/Nav/README.md @@ -1,4 +1,4 @@ -Basic Nav component: +Basic Vertical Nav component: - + +Basic Horizontal Nav component: + + +Horizontal Nav with sub-NavItems: + + Nav with sub-NavItems: