Skip to content

Commit

Permalink
Merge pull request #343 from appfolio/zdAddSelectAsyncClass
Browse files Browse the repository at this point in the history
Add class to Select component when in Async mode for testing
  • Loading branch information
gthomas-appfolio authored Dec 11, 2017
2 parents 909e2bd + 47d963d commit f5e6d77
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/components/Select.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import PropTypes from 'prop-types';
import React from 'react';
import ReactSelect from 'react-select-plus';
import classnames from 'classnames';
import Close from './Close';
import Icon from './Icon';
import noop from 'lodash.noop';
Expand All @@ -11,6 +12,7 @@ import './Select.scss';

class Select extends React.Component {
static propTypes = {
className: PropTypes.string,
defaultValue: PropTypes.any,
loadOptions: PropTypes.func,
onChange: PropTypes.func,
Expand Down Expand Up @@ -48,9 +50,10 @@ class Select extends React.Component {
}

render() {
const { value, ...props } = this.props;
const { className, value, ...props } = this.props;
delete props.onChange; // don't pass onChange prop to react-select
const SelectElement = this.props.loadOptions ? ReactSelect.Async : ReactSelect;
const classNames = classnames(className, {'select-async': this.props.loadOptions});

return (
<SelectElement
Expand All @@ -61,6 +64,7 @@ class Select extends React.Component {
onChange={this.onChange}
value={value || this.state.value}
ref={this.bindInput}
className={classNames}
{...props}
/>
);
Expand Down
20 changes: 20 additions & 0 deletions test/components/Select.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ describe('<Select />', () => {
it('should have a blank default', () => {
assert.equal(component.type(), ReactSelect);
assert.equal(component.prop('value'), null);
assert(!component.hasClass('select-async'));
});

it('should clear input', () => {
Expand Down Expand Up @@ -78,6 +79,25 @@ describe('<Select />', () => {

const component = shallow(<Select loadOptions={getOptions} />);
assert.equal(component.type(), ReactSelect.Async);
assert(component.hasClass('select-async'));
});

it('should append the async class to the className prop', () => {
const getOptions = (input, callback) => {
callback(null, {
options: [
{ value: 'oogah', label: 'Oogah' },
{ value: 'chaka', label: 'Chaka' }
],
complete: true
});
};

const component = shallow(<Select loadOptions={getOptions} className="foo bar" />);
assert.equal(component.type(), ReactSelect.Async);
assert(component.hasClass('foo'));
assert(component.hasClass('bar'));
assert(component.hasClass('select-async'));
});

it('should support focus', () => {
Expand Down

0 comments on commit f5e6d77

Please sign in to comment.