Skip to content

Commit

Permalink
[changed] Assert ProgressBar children can be ProgressBar only.
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexKVal committed Jun 6, 2015
1 parent 54dd9fa commit 2f8c454
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 8 deletions.
35 changes: 27 additions & 8 deletions src/ProgressBar.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { cloneElement } from 'react';
import React, { cloneElement, PropTypes } from 'react';
import Interpolate from './Interpolate';
import BootstrapMixin from './BootstrapMixin';
import classNames from 'classnames';
Expand All @@ -7,13 +7,14 @@ import ValidComponentChildren from './utils/ValidComponentChildren';

const ProgressBar = React.createClass({
propTypes: {
min: React.PropTypes.number,
now: React.PropTypes.number,
max: React.PropTypes.number,
label: React.PropTypes.node,
srOnly: React.PropTypes.bool,
striped: React.PropTypes.bool,
active: React.PropTypes.bool
min: PropTypes.number,
now: PropTypes.number,
max: PropTypes.number,
label: PropTypes.node,
srOnly: PropTypes.bool,
striped: PropTypes.bool,
active: PropTypes.bool,
children: onlyProgressBar
},

mixins: [BootstrapMixin],
Expand Down Expand Up @@ -127,4 +128,22 @@ const ProgressBar = React.createClass({
}
});

/**
* Custom propTypes checker
*/
function onlyProgressBar(props, propName, componentName) {
if (props[propName]) {
let error, childIdentifier;

React.Children.forEach(props[propName], (child) => {
if (child.type !== ProgressBar) {
childIdentifier = (child.type.displayName ? child.type.displayName : child.type);
error = new Error(`Children of ${componentName} can contain only ProgressBar components. Found ${childIdentifier}`);
}
});

return error;
}
}

export default ProgressBar;
12 changes: 12 additions & 0 deletions test/ProgressBarSpec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React from 'react';
import ReactTestUtils from 'react/lib/ReactTestUtils';
import ProgressBar from '../src/ProgressBar';
import {shouldWarn} from './helpers';

const getProgressBarNode = function (wrapper) {
return React.findDOMNode(ReactTestUtils.findRenderedDOMComponentWithClass(wrapper, 'progress-bar'));
Expand Down Expand Up @@ -181,4 +182,15 @@ describe('ProgressBar', function () {
assert.equal(bar2.style.width, '30%');
});

it('allows only ProgressBar in children', function () {
ReactTestUtils.renderIntoDocument(
<ProgressBar>
<ProgressBar key={1} />
<div />
<ProgressBar key={2} />
</ProgressBar>
);

shouldWarn('Failed propType');
});
});

0 comments on commit 2f8c454

Please sign in to comment.