Skip to content

Commit

Permalink
[removed] handleResize
Browse files Browse the repository at this point in the history
  • Loading branch information
thealjey committed Oct 16, 2015
1 parent 0e84ec9 commit 2013818
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 69 deletions.
41 changes: 18 additions & 23 deletions build/Combo.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ var _reactLibReactWithAddons2 = _interopRequireDefault(_reactLibReactWithAddons)

var _reactDom = require('react-dom');

var _reactDom2 = _interopRequireDefault(_reactDom);

var _reactBootstrap = require('react-bootstrap');

var _lodashCollectionMap = require('lodash/collection/map');
Expand All @@ -39,6 +37,10 @@ var _lodashFunctionDebounce = require('lodash/function/debounce');
var _lodashFunctionDebounce2 = _interopRequireDefault(_lodashFunctionDebounce);

var caret = _reactLibReactWithAddons2['default'].createElement('span', { className: 'caret' });
var _React$PropTypes = _reactLibReactWithAddons2['default'].PropTypes;
var func = _React$PropTypes.func;
var object = _React$PropTypes.object;
var string = _React$PropTypes.string;

var uid = 0;

Expand All @@ -49,8 +51,9 @@ var uid = 0;
* @param {Object} props - A props config
* @example
* import {Combo} from 'react-bootstrap-combobox';
* import {render} from 'react-dom';
*
* ReactDOM.render(<Combo items={{
* render(<Combo items={{
* // keys must be unique
* a: {label: 'first item', header: true}, // any combination of props supported by MenuItem
* b: 'second item', // same as {label: 'second item'}
Expand Down Expand Up @@ -86,16 +89,6 @@ var Combo = (function (_React$Component) {
*/
this.viewportHeight = 0;

/**
* Handles browser resize events, debounced by 150ms
*
* @memberof Combo
* @instance
* @private
* @method handleResize
*/
this.handleResize = (0, _lodashFunctionDebounce2['default'])(this.onResize.bind(this), 150);

/**
* Holds component state
*
Expand All @@ -105,6 +98,8 @@ var Combo = (function (_React$Component) {
* @type {Object}
*/
this.state = { maxHeight: null };

this.onResize = (0, _lodashFunctionDebounce2['default'])(this.onResize.bind(this), 150);
}

/**
Expand Down Expand Up @@ -136,8 +131,8 @@ var Combo = (function (_React$Component) {
}, {
key: 'componentDidMount',
value: function componentDidMount() {
this.handleResize();
window.addEventListener('resize', this.handleResize);
this.onResize();
window.addEventListener('resize', this.onResize);
}

/**
Expand All @@ -151,7 +146,7 @@ var Combo = (function (_React$Component) {
}, {
key: 'componentWillUnmount',
value: function componentWillUnmount() {
window.removeEventListener('resize', this.handleResize);
window.removeEventListener('resize', this.onResize);
}

/**
Expand All @@ -172,7 +167,7 @@ var Combo = (function (_React$Component) {
}
this.viewportHeight = height;

this.setState({ maxHeight: height - _reactDom2['default'].findDOMNode(this).getBoundingClientRect().bottom - 5 });
this.setState({ maxHeight: height - (0, _reactDom.findDOMNode)(this).getBoundingClientRect().bottom - 5 });
}

/**
Expand Down Expand Up @@ -204,7 +199,11 @@ var Combo = (function (_React$Component) {
}, {
key: 'getLabel',
value: function getLabel() {
return this.normalize(this.props.items[this.props.value]).label;
var _props = this.props;
var items = _props.items;
var value = _props.value;

return this.normalize(items[value]).label;
}

/**
Expand Down Expand Up @@ -305,10 +304,6 @@ var Combo = (function (_React$Component) {

exports['default'] = Combo;

Combo.propTypes = {
onChange: _reactLibReactWithAddons2['default'].PropTypes.func,
items: _reactLibReactWithAddons2['default'].PropTypes.object.isRequired,
value: _reactLibReactWithAddons2['default'].PropTypes.string.isRequired
};
Combo.propTypes = { onChange: func, items: object.isRequired, value: string.isRequired };
Combo.defaultProps = { onChange: Function.prototype };
module.exports = exports['default'];
2 changes: 1 addition & 1 deletion development/App.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* @flow */

import React from 'react';
import {Combo} from '../lib';
import Combo from '../lib/Combo';

export default class App extends React.Component {

Expand Down
4 changes: 2 additions & 2 deletions development/script.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* @flow */

import React from 'react';
import ReactDOM from 'react-dom';
import {render} from 'react-dom';
import App from './App';

ReactDOM.render(<App />, document.getElementById('app'));
render(<App />, document.getElementById('app'));
3 changes: 2 additions & 1 deletion docs/Combo.html
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,9 @@ <h5>Parameters:</h5>
<h5>Example</h5>

<pre class="prettyprint"><code>import {Combo} from 'react-bootstrap-combobox';
import {render} from 'react-dom';

ReactDOM.render(&lt;Combo items={{
render(&lt;Combo items={{
// keys must be unique
a: {label: 'first item', header: true}, // any combination of props supported by MenuItem
b: 'second item', // same as {label: 'second item'}
Expand Down
40 changes: 15 additions & 25 deletions lib/Combo.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
/* @flow */

import React from 'react/lib/ReactWithAddons';
import ReactDOM from 'react-dom';
import {findDOMNode} from 'react-dom';
import {Button, Dropdown, MenuItem} from 'react-bootstrap';
import map from 'lodash/collection/map';
import isString from 'lodash/lang/isString';
import debounce from 'lodash/function/debounce';

const caret = <span className="caret" />;
const caret = <span className="caret" />,
{func, object, string} = React.PropTypes;

let uid = 0;

Expand All @@ -18,8 +19,9 @@ let uid = 0;
* @param {Object} props - A props config
* @example
* import {Combo} from 'react-bootstrap-combobox';
* import {render} from 'react-dom';
*
* ReactDOM.render(<Combo items={{
* render(<Combo items={{
* // keys must be unique
* a: {label: 'first item', header: true}, // any combination of props supported by MenuItem
* b: 'second item', // same as {label: 'second item'}
Expand All @@ -36,8 +38,6 @@ export default class Combo extends React.Component {

viewportHeight: number;

handleResize: Function;

static propTypes: Object;

static defaultProps: Object;
Expand Down Expand Up @@ -65,16 +65,6 @@ export default class Combo extends React.Component {
*/
this.viewportHeight = 0;

/**
* Handles browser resize events, debounced by 150ms
*
* @memberof Combo
* @instance
* @private
* @method handleResize
*/
this.handleResize = debounce(this.onResize.bind(this), 150);

/**
* Holds component state
*
Expand All @@ -84,6 +74,8 @@ export default class Combo extends React.Component {
* @type {Object}
*/
this.state = {maxHeight: null};

this.onResize = debounce(this.onResize.bind(this), 150);
}

/**
Expand All @@ -110,8 +102,8 @@ export default class Combo extends React.Component {
* @method componentDidMount
*/
componentDidMount() {
this.handleResize();
window.addEventListener('resize', this.handleResize);
this.onResize();
window.addEventListener('resize', this.onResize);
}

/**
Expand All @@ -123,7 +115,7 @@ export default class Combo extends React.Component {
* @method componentWillUnmount
*/
componentWillUnmount() {
window.removeEventListener('resize', this.handleResize);
window.removeEventListener('resize', this.onResize);
}

/**
Expand All @@ -142,7 +134,7 @@ export default class Combo extends React.Component {
}
this.viewportHeight = height;

this.setState({maxHeight: height - ReactDOM.findDOMNode(this).getBoundingClientRect().bottom - 5});
this.setState({maxHeight: height - findDOMNode(this).getBoundingClientRect().bottom - 5});
}

/**
Expand Down Expand Up @@ -170,7 +162,9 @@ export default class Combo extends React.Component {
* @return {string} a label of the currently active menu item
*/
getLabel(): string {
return this.normalize(this.props.items[this.props.value]).label;
const {items, value} = this.props;

return this.normalize(items[value]).label;
}

/**
Expand Down Expand Up @@ -250,9 +244,5 @@ export default class Combo extends React.Component {

}

Combo.propTypes = {
onChange: React.PropTypes.func,
items: React.PropTypes.object.isRequired,
value: React.PropTypes.string.isRequired
};
Combo.propTypes = {onChange: func, items: object.isRequired, value: string.isRequired};
Combo.defaultProps = {onChange: Function.prototype};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,6 @@
"proxyquire": "^1.7.3",
"react-hot-loader": "^1.2.8",
"release-script": "^0.5.3",
"webcompiler": "^2.0.4"
"webcompiler": "^2.0.5"
}
}
20 changes: 4 additions & 16 deletions spec/ComboSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ describe('Combo', function () {
if (!Combo) {
return;
}
spyOn(Combo.prototype.onResize, 'bind').and.returnValue('bound handler');
spyOn(Combo.prototype.renderMenuItem, 'bind').and.returnValue('bound renderMenuItem');
cmp = new Combo({something: 'here'});
});
Expand Down Expand Up @@ -53,17 +52,6 @@ describe('Combo', function () {
expect(cmp.viewportHeight).toBe(0);
});

it('calls the bind method', function () {
if (!cmp) {
return;
}
expect(cmp.onResize.bind).toHaveBeenCalledWith(cmp);
});

it('debounces the bound handler', function () {
expect(debounce).toHaveBeenCalledWith('bound handler', 150);
});

it('initializes state', function () {
if (!cmp) {
return;
Expand Down Expand Up @@ -129,22 +117,22 @@ describe('Combo', function () {
if (!cmp) {
return;
}
spyOn(cmp, 'handleResize');
spyOn(cmp, 'onResize');
cmp.componentDidMount();
});

it('calls handleResize', function () {
if (!cmp) {
return;
}
expect(cmp.handleResize).toHaveBeenCalled();
expect(cmp.onResize).toHaveBeenCalled();
});

it('sets up a resize listener on the window object', function () {
if (!cmp) {
return;
}
expect(addEventListener).toHaveBeenCalledWith('resize', cmp.handleResize);
expect(addEventListener).toHaveBeenCalledWith('resize', cmp.onResize);
});

});
Expand All @@ -162,7 +150,7 @@ describe('Combo', function () {
if (!cmp) {
return;
}
expect(removeEventListener).toHaveBeenCalledWith('resize', cmp.handleResize);
expect(removeEventListener).toHaveBeenCalledWith('resize', cmp.onResize);
});

});
Expand Down

0 comments on commit 2013818

Please sign in to comment.