Skip to content

Commit

Permalink
Merge pull request react-bootstrap#591 from AlexKVal/refactoring425
Browse files Browse the repository at this point in the history
Collapsable* => Collapsible* renaming of components
  • Loading branch information
mtscout6 committed Apr 28, 2015
2 parents 779365c + 0105127 commit 4e919dd
Show file tree
Hide file tree
Showing 20 changed files with 698 additions and 329 deletions.
4 changes: 2 additions & 2 deletions docs/examples/.eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
"Button",
"ButtonGroup",
"ButtonToolbar",
"CollapsableNav",
"CollapsableMixin",
"CollapsibleNav",
"CollapsibleMixin",
"Carousel",
"CarouselItem",
"Col",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const navbarInstance = (
<Navbar brand='React-Bootstrap' toggleNavKey={0}>
<CollapsableNav eventKey={0}> {/* This is the eventKey referenced */}
<CollapsibleNav eventKey={0}> {/* This is the eventKey referenced */}
<Nav navbar>
<NavItem eventKey={1} href='#'>Link</NavItem>
<NavItem eventKey={2} href='#'>Link</NavItem>
Expand All @@ -16,7 +16,7 @@ const navbarInstance = (
<NavItem eventKey={1} href='#'>Link Right</NavItem>
<NavItem eventKey={2} href='#'>Link Right</NavItem>
</Nav>
</CollapsableNav>
</CollapsibleNav>
</Navbar>
);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
const CollapsableParagraph = React.createClass({
mixins: [CollapsableMixin],
const CollapsibleParagraph = React.createClass({
mixins: [CollapsibleMixin],

getCollapsableDOMNode: function(){
getCollapsibleDOMNode: function(){
return this.refs.panel.getDOMNode();
},

getCollapsableDimensionValue: function(){
getCollapsibleDimensionValue: function(){
return this.refs.panel.getDOMNode().scrollHeight;
},

Expand All @@ -15,7 +15,7 @@ const CollapsableParagraph = React.createClass({
},

render: function(){
let styles = this.getCollapsableClassSet();
let styles = this.getCollapsibleClassSet();
let text = this.isExpanded() ? 'Hide' : 'Show';
return (
<div>
Expand All @@ -29,9 +29,9 @@ const CollapsableParagraph = React.createClass({
});

const panelInstance = (
<CollapsableParagraph>
<CollapsibleParagraph>
Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid. 3 wolf moon officia aute, non cupidatat skateboard dolor brunch. Food truck quinoa nesciunt laborum eiusmod. Brunch 3 wolf moon tempor, sunt aliqua put a bird on it squid single-origin coffee nulla assumenda shoreditch et. Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred nesciunt sapiente ea proident. Ad vegan excepteur butcher vice lomo. Leggings occaecat craft beer farm-to-table, raw denim aesthetic synth nesciunt you probably haven't heard of them accusamus labore sustainable VHS.
</CollapsableParagraph>
</CollapsibleParagraph>
);

React.render(panelInstance, mountNode);
File renamed without changes.
16 changes: 8 additions & 8 deletions docs/src/ComponentsPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -208,9 +208,9 @@ const ComponentsPage = React.createClass({
<p><code>&lt;Accordion /&gt;</code> aliases <code>&lt;PanelGroup accordion /&gt;</code>.</p>
<ReactPlayground codeText={Samples.PanelGroupAccordion} />

<h3 id='panels-collapsable'>Collapsable Mixin</h3>
<p><code>CollapsableMixin</code> can be used to create your own components with collapse functionality.</p>
<ReactPlayground codeText={Samples.CollapsableParagraph} />
<h3 id='panels-collapsible'>Collapsible Mixin</h3>
<p><code>CollapsibleMixin</code> can be used to create your own components with collapse functionality.</p>
<ReactPlayground codeText={Samples.CollapsibleParagraph} />
</div>

<div className='bs-docs-section'>
Expand Down Expand Up @@ -351,7 +351,7 @@ const ComponentsPage = React.createClass({
<p>By setting the property {React.DOM.code(null, 'defaultNavExpanded={true}')} the Navbar will start expanded by default.</p>
<div className='bs-callout bs-callout-info'>
<h4>Scrollbar overflow</h4>
<p>The height of the collapsable is slightly smaller than the real height. To hide the scroll bar, add the following css to your style files.</p>
<p>The height of the collapsible is slightly smaller than the real height. To hide the scroll bar, add the following css to your style files.</p>
<pre>
{React.DOM.code(null,
'.navbar-collapse {\n' +
Expand All @@ -360,17 +360,17 @@ const ComponentsPage = React.createClass({
)}
</pre>
</div>
<ReactPlayground codeText={Samples.NavbarCollapsable} />
<ReactPlayground codeText={Samples.NavbarCollapsible} />

<h3>Mobile Friendly (Multiple Nav Components)</h3>
<p>To have a mobile friendly Navbar that handles multiple <code>Nav</code> components use <code>CollapsableNav</code>. The <code>toggleNavKey</code> must still be set, however, the corresponding <code>eventKey</code> must now be on the <code>CollapsableNav</code> component.</p>
<p>To have a mobile friendly Navbar that handles multiple <code>Nav</code> components use <code>CollapsibleNav</code>. The <code>toggleNavKey</code> must still be set, however, the corresponding <code>eventKey</code> must now be on the <code>CollapsibleNav</code> component.</p>
<div className="bs-callout bs-callout-info">
<h4>Div collapse</h4>
<p>The <code>navbar-collapse</code> div gets created as the collapsable element which follows the <a href="http://getbootstrap.com/components/#navbar-default">bootstrap</a> collapsable navbar documentation.</p>
<p>The <code>navbar-collapse</code> div gets created as the collapsible element which follows the <a href="http://getbootstrap.com/components/#navbar-default">bootstrap</a> collapsible navbar documentation.</p>
<pre>&lt;div class="collapse navbar-collapse"&gt;&lt;/div&gt;</pre>
</div>

<ReactPlayground codeText={Samples.CollapsableNav} />
<ReactPlayground codeText={Samples.CollapsibleNav} />
</div>

{/* Tabbed Areas */}
Expand Down
8 changes: 4 additions & 4 deletions docs/src/ReactPlayground.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import * as modBadge from '../../src/Badge';
import * as modmodButton from '../../src/Button';
import * as modButtonGroup from '../../src/ButtonGroup';
import * as modmodButtonToolbar from '../../src/ButtonToolbar';
import * as modCollapsableNav from '../../src/CollapsableNav';
import * as modCollapsableMixin from '../../src/CollapsableMixin';
import * as modCollapsibleNav from '../../src/CollapsibleNav';
import * as modCollapsibleMixin from '../../src/CollapsibleMixin';
import * as modCarousel from '../../src/Carousel';
import * as modCarouselItem from '../../src/CarouselItem';
import * as modCol from '../../src/Col';
Expand Down Expand Up @@ -53,8 +53,8 @@ const Badge = modBadge.default;
const Button = modmodButton.default;
const ButtonGroup = modButtonGroup.default;
const ButtonToolbar = modmodButtonToolbar.default;
const CollapsableNav = modCollapsableNav.default;
const CollapsableMixin = modCollapsableMixin.default;
const CollapsibleNav = modCollapsibleNav.default;
const CollapsibleMixin = modCollapsibleMixin.default;
const Carousel = modCarousel.default;
const CarouselItem = modCarouselItem.default;
const Col = modCol.default;
Expand Down
6 changes: 3 additions & 3 deletions docs/src/Samples.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export default {
PanelGroupControlled: require('fs').readFileSync(__dirname + '/../examples/PanelGroupControlled.js', 'utf8'),
PanelGroupUncontrolled: require('fs').readFileSync(__dirname + '/../examples/PanelGroupUncontrolled.js', 'utf8'),
PanelGroupAccordion: require('fs').readFileSync(__dirname + '/../examples/PanelGroupAccordion.js', 'utf8'),
CollapsableParagraph: require('fs').readFileSync(__dirname + '/../examples/CollapsableParagraph.js', 'utf8'),
CollapsibleParagraph: require('fs').readFileSync(__dirname + '/../examples/CollapsibleParagraph.js', 'utf8'),
ModalStatic: require('fs').readFileSync(__dirname + '/../examples/ModalStatic.js', 'utf8'),
ModalTrigger: require('fs').readFileSync(__dirname + '/../examples/ModalTrigger.js', 'utf8'),
ModalOverlayMixin: require('fs').readFileSync(__dirname + '/../examples/ModalOverlayMixin.js', 'utf8'),
Expand All @@ -52,8 +52,8 @@ export default {
NavJustified: require('fs').readFileSync(__dirname + '/../examples/NavJustified.js', 'utf8'),
NavbarBasic: require('fs').readFileSync(__dirname + '/../examples/NavbarBasic.js', 'utf8'),
NavbarBrand: require('fs').readFileSync(__dirname + '/../examples/NavbarBrand.js', 'utf8'),
NavbarCollapsable: require('fs').readFileSync(__dirname + '/../examples/NavbarCollapsable.js', 'utf8'),
CollapsableNav: require('fs').readFileSync(__dirname + '/../examples/CollapsableNav.js', 'utf8'),
NavbarCollapsible: require('fs').readFileSync(__dirname + '/../examples/NavbarCollapsible.js', 'utf8'),
CollapsibleNav: require('fs').readFileSync(__dirname + '/../examples/CollapsibleNav.js', 'utf8'),
TabbedAreaUncontrolled: require('fs').readFileSync(__dirname + '/../examples/TabbedAreaUncontrolled.js', 'utf8'),
TabbedAreaControlled: require('fs').readFileSync(__dirname + '/../examples/TabbedAreaControlled.js', 'utf8'),
TabbedAreaNoAnimation: require('fs').readFileSync(__dirname + '/../examples/TabbedAreaNoAnimation.js', 'utf8'),
Expand Down
194 changes: 32 additions & 162 deletions src/CollapsableMixin.js
Original file line number Diff line number Diff line change
@@ -1,168 +1,38 @@
import React from 'react';
import TransitionEvents from 'react/lib/ReactTransitionEvents';
import assign from './utils/Object.assign';
import deprecationWarning from './utils/deprecationWarning';
import CollapsibleMixin from './CollapsibleMixin';

const CollapsableMixin = {

propTypes: {
defaultExpanded: React.PropTypes.bool,
expanded: React.PropTypes.bool
},

getInitialState(){
let defaultExpanded = this.props.defaultExpanded != null ?
this.props.defaultExpanded :
this.props.expanded != null ?
this.props.expanded :
false;

return {
expanded: defaultExpanded,
collapsing: false
};
},

componentWillUpdate(nextProps, nextState){
let willExpanded = nextProps.expanded != null ? nextProps.expanded : nextState.expanded;
if (willExpanded === this.isExpanded()) {
return;
}

// if the expanded state is being toggled, ensure node has a dimension value
// this is needed for the animation to work and needs to be set before
// the collapsing class is applied (after collapsing is applied the in class
// is removed and the node's dimension will be wrong)

let node = this.getCollapsableDOMNode();
let dimension = this.dimension();
let value = '0';

if(!willExpanded){
value = this.getCollapsableDimensionValue();
}

node.style[dimension] = value + 'px';

this._afterWillUpdate();
},

componentDidUpdate(prevProps, prevState){
// check if expanded is being toggled; if so, set collapsing
this._checkToggleCollapsing(prevProps, prevState);

// check if collapsing was turned on; if so, start animation
this._checkStartAnimation();
},

// helps enable test stubs
_afterWillUpdate(){
},

_checkStartAnimation(){
if(!this.state.collapsing) {
return;
}

let node = this.getCollapsableDOMNode();
let dimension = this.dimension();
let value = this.getCollapsableDimensionValue();

// setting the dimension here starts the transition animation
let result;
if(this.isExpanded()) {
result = value + 'px';
} else {
result = '0px';
}
node.style[dimension] = result;
},

_checkToggleCollapsing(prevProps, prevState){
let wasExpanded = prevProps.expanded != null ? prevProps.expanded : prevState.expanded;
let isExpanded = this.isExpanded();
if(wasExpanded !== isExpanded){
if(wasExpanded) {
this._handleCollapse();
} else {
this._handleExpand();
}
}
},

_handleExpand(){
let node = this.getCollapsableDOMNode();
let dimension = this.dimension();

let complete = () => {
this._removeEndEventListener(node, complete);
// remove dimension value - this ensures the collapsable item can grow
// in dimension after initial display (such as an image loading)
node.style[dimension] = '';
this.setState({
collapsing:false
});
};

this._addEndEventListener(node, complete);

this.setState({
collapsing: true
});
},

_handleCollapse(){
let node = this.getCollapsableDOMNode();

let complete = () => {
this._removeEndEventListener(node, complete);
this.setState({
collapsing: false
});
};

this._addEndEventListener(node, complete);

this.setState({
collapsing: true
});
},

// helps enable test stubs
_addEndEventListener(node, complete){
TransitionEvents.addEndEventListener(node, complete);
},

// helps enable test stubs
_removeEndEventListener(node, complete){
TransitionEvents.removeEndEventListener(node, complete);
},

dimension(){
return (typeof this.getCollapsableDimension === 'function') ?
this.getCollapsableDimension() :
'height';
},

isExpanded(){
return this.props.expanded != null ? this.props.expanded : this.state.expanded;
},
let link = 'https://github.com/react-bootstrap/react-bootstrap/issues/425#issuecomment-97110963';

const CollapsableMixin = assign({}, CollapsibleMixin, {
getCollapsableClassSet(className) {
let classes = {};

if (typeof className === 'string') {
className.split(' ').forEach(subClasses => {
if (subClasses) {
classes[subClasses] = true;
}
});
}

classes.collapsing = this.state.collapsing;
classes.collapse = !this.state.collapsing;
classes.in = this.isExpanded() && !this.state.collapsing;

return classes;
deprecationWarning(
'CollapsableMixin.getCollapsableClassSet()',
'CollapsibleMixin.getCollapsibleClassSet()',
link
);
return CollapsibleMixin.getCollapsibleClassSet.call(this, className);
},

getCollapsibleDOMNode() {
deprecationWarning(
'CollapsableMixin.getCollapsableDOMNode()',
'CollapsibleMixin.getCollapsibleDOMNode()',
link
);
return this.getCollapsableDOMNode();
},

getCollapsibleDimensionValue() {
deprecationWarning(
'CollapsableMixin.getCollapsableDimensionValue()',
'CollapsibleMixin.getCollapsibleDimensionValue()',
link
);
return this.getCollapsableDimensionValue();
}
};
});

deprecationWarning('CollapsableMixin', 'CollapsibleMixin', link);

export default CollapsableMixin;
Loading

0 comments on commit 4e919dd

Please sign in to comment.