Skip to content

Commit

Permalink
Move CollapsableNav => CollapsibleNav
Browse files Browse the repository at this point in the history
And refactor CollapsableNav to use CollapsibleMixin
  • Loading branch information
AlexKVal committed Apr 28, 2015
1 parent 4f66df8 commit 626fefa
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 107 deletions.
111 changes: 4 additions & 107 deletions src/CollapsableNav.js
Original file line number Diff line number Diff line change
@@ -1,111 +1,8 @@
import React, { cloneElement } from 'react';
import BootstrapMixin from './BootstrapMixin';
import CollapsableMixin from './CollapsableMixin';
import classNames from 'classnames';
import domUtils from './utils/domUtils';
import deprecationWarning from './utils/deprecationWarning';
import CollapsibleNav from './CollapsibleNav';

import ValidComponentChildren from './utils/ValidComponentChildren';
import createChainedFunction from './utils/createChainedFunction';
let CollapsableNav = CollapsibleNav;

const CollapsableNav = React.createClass({
mixins: [BootstrapMixin, CollapsableMixin],

propTypes: {
onSelect: React.PropTypes.func,
activeHref: React.PropTypes.string,
activeKey: React.PropTypes.any,
collapsable: React.PropTypes.bool,
expanded: React.PropTypes.bool,
eventKey: React.PropTypes.any
},

getCollapsableDOMNode() {
return this.getDOMNode();
},

getCollapsableDimensionValue() {
let height = 0;
let nodes = this.refs;
for (let key in nodes) {
if (nodes.hasOwnProperty(key)) {

let n = nodes[key].getDOMNode()
, h = n.offsetHeight
, computedStyles = domUtils.getComputedStyles(n);

height += (h + parseInt(computedStyles.marginTop, 10) + parseInt(computedStyles.marginBottom, 10));
}
}
return height;
},

render() {
/*
* this.props.collapsable is set in NavBar when a eventKey is supplied.
*/
let classes = this.props.collapsable ? this.getCollapsableClassSet() : {};
/*
* prevent duplicating navbar-collapse call if passed as prop. kind of overkill... good cadidate to have check implemented as a util that can
* also be used elsewhere.
*/
if (this.props.className === undefined || this.props.className.split(' ').indexOf('navbar-collapse') === -2) {
classes['navbar-collapse'] = this.props.collapsable;
}

return (
<div eventKey={this.props.eventKey} className={classNames(this.props.className, classes)} >
{ValidComponentChildren.map(this.props.children, this.props.collapsable ? this.renderCollapsableNavChildren : this.renderChildren )}
</div>
);
},

getChildActiveProp(child) {
if (child.props.active) {
return true;
}
if (this.props.activeKey != null) {
if (child.props.eventKey === this.props.activeKey) {
return true;
}
}
if (this.props.activeHref != null) {
if (child.props.href === this.props.activeHref) {
return true;
}
}

return child.props.active;
},

renderChildren(child, index) {
let key = child.key ? child.key : index;
return cloneElement(
child,
{
activeKey: this.props.activeKey,
activeHref: this.props.activeHref,
ref: 'nocollapse_' + key,
key: key,
navItem: true
}
);
},

renderCollapsableNavChildren(child, index) {
let key = child.key ? child.key : index;
return cloneElement(
child,
{
active: this.getChildActiveProp(child),
activeKey: this.props.activeKey,
activeHref: this.props.activeHref,
onSelect: createChainedFunction(child.props.onSelect, this.props.onSelect),
ref: 'collapsable_' + key,
key: key,
navItem: true
}
);
}
});
deprecationWarning('CollapsableNav', 'CollapsibleNav');

export default CollapsableNav;
121 changes: 121 additions & 0 deletions src/CollapsibleNav.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
import React, { cloneElement } from 'react';
import BootstrapMixin from './BootstrapMixin';
import CollapsibleMixin from './CollapsibleMixin';
import classNames from 'classnames';
import domUtils from './utils/domUtils';

import ValidComponentChildren from './utils/ValidComponentChildren';
import createChainedFunction from './utils/createChainedFunction';

const CollapsibleNav = React.createClass({
mixins: [BootstrapMixin, CollapsibleMixin],

propTypes: {
onSelect: React.PropTypes.func,
activeHref: React.PropTypes.string,
activeKey: React.PropTypes.any,
collapsable: React.PropTypes.bool,
expanded: React.PropTypes.bool,
eventKey: React.PropTypes.any
},

getCollapsibleDOMNode() {
return this.getDOMNode();
},

getCollapsibleDimensionValue() {
let height = 0;
let nodes = this.refs;
for (let key in nodes) {
if (nodes.hasOwnProperty(key)) {

let n = nodes[key].getDOMNode()
, h = n.offsetHeight
, computedStyles = domUtils.getComputedStyles(n);

height += (h +
parseInt(computedStyles.marginTop, 10) +
parseInt(computedStyles.marginBottom, 10)
);
}
}
return height;
},

render() {
/*
* this.props.collapsable is set in NavBar when a eventKey is supplied.
*/
let classes = this.props.collapsable ? this.getCollapsibleClassSet() : {};
/*
* prevent duplicating navbar-collapse call if passed as prop.
* kind of overkill...
* good cadidate to have check implemented as an util that can
* also be used elsewhere.
*/
if (this.props.className === undefined ||
this.props.className.split(' ').indexOf('navbar-collapse') === -2) {
classes['navbar-collapse'] = this.props.collapsable;
}

return (
<div eventKey={this.props.eventKey} className={classNames(this.props.className, classes)} >
{ValidComponentChildren.map(this.props.children,
this.props.collapsable ?
this.renderCollapsibleNavChildren :
this.renderChildren
)}
</div>
);
},

getChildActiveProp(child) {
if (child.props.active) {
return true;
}
if (this.props.activeKey != null) {
if (child.props.eventKey === this.props.activeKey) {
return true;
}
}
if (this.props.activeHref != null) {
if (child.props.href === this.props.activeHref) {
return true;
}
}

return child.props.active;
},

renderChildren(child, index) {
let key = child.key ? child.key : index;
return cloneElement(
child,
{
activeKey: this.props.activeKey,
activeHref: this.props.activeHref,
ref: 'nocollapse_' + key,
key: key,
navItem: true
}
);
},

renderCollapsibleNavChildren(child, index) {
let key = child.key ? child.key : index;
return cloneElement(
child,
{
active: this.getChildActiveProp(child),
activeKey: this.props.activeKey,
activeHref: this.props.activeHref,
onSelect: createChainedFunction(child.props.onSelect, this.props.onSelect),
ref: 'collapsable_' + key,
key: key,
navItem: true
}
);
}
});

export default CollapsibleNav;

0 comments on commit 626fefa

Please sign in to comment.