Skip to content

Commit

Permalink
Move isOnlyResult out of component state
Browse files Browse the repository at this point in the history
  • Loading branch information
ericgio committed Jan 29, 2019
1 parent 34d226b commit c208e82
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 20 deletions.
24 changes: 4 additions & 20 deletions src/containers/contextContainer.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,13 @@
import {head, pick} from 'lodash';
import {pick} from 'lodash';
import React from 'react';
import {polyfill} from 'react-lifecycles-compat';

import TypeaheadContext from '../TypeaheadContext';
import {getHintText} from '../utils';
import {getHintText, getIsOnlyResult} from '../utils';
import {RETURN} from '../constants';

function contextContainer(Typeahead) {
class WrappedTypeahead extends React.Component {
state = {
isOnlyResult: false,
};

static getDerivedStateFromProps(props, state) {
const {allowNew, highlightOnlyResult, results} = props;

if (!highlightOnlyResult || allowNew) {
return null;
}

return {
isOnlyResult: results.length === 1 && !head(results).disabled,
};
}

componentDidUpdate(prevProps, prevState) {
const {allowNew, onInitialItemChange, results} = this.props;

Expand All @@ -49,7 +33,7 @@ function contextContainer(Typeahead) {
value={{
...contextValues,
hintText: getHintText(this.props),
isOnlyResult: this.state.isOnlyResult,
isOnlyResult: getIsOnlyResult(this.props),
}}>
<Typeahead
{...this.props}
Expand All @@ -64,7 +48,7 @@ function contextContainer(Typeahead) {

switch (e.keyCode) {
case RETURN:
if (this.state.isOnlyResult) {
if (getIsOnlyResult(this.props)) {
onAdd(initialItem);
}
break;
Expand Down
11 changes: 11 additions & 0 deletions src/utils/getIsOnlyResult.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import {head} from 'lodash';

function getIsOnlyResult({allowNew, highlightOnlyResult, results}) {
if (!highlightOnlyResult || allowNew) {
return false;
}

return results.length === 1 && !head(results).disabled;
}

export default getIsOnlyResult;
1 change: 1 addition & 0 deletions src/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export getAccessibilityStatus from './getAccessibilityStatus';
export getDisplayName from './getDisplayName';
export getHintText from './getHintText';
export getInputText from './getInputText';
export getIsOnlyResult from './getIsOnlyResult';
export getMatchBounds from './getMatchBounds';
export getMenuItemId from './getMenuItemId';
export getOptionLabel from './getOptionLabel';
Expand Down
42 changes: 42 additions & 0 deletions test/utils/getIsOnlyResultSpec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import {expect} from 'chai';

import getIsOnlyResult from '../../src/utils/getIsOnlyResult';

describe('getIsOnlyResult', () => {
let props;

beforeEach(() => {
props = {
allowNew: false,
highlightOnlyResult: true,
results: ['The only result!'],
};
});

it('returns true', () => {
expect(getIsOnlyResult(props)).to.equal(true);
});

it('returns false when `highlightOnlyResult` is disabled', () => {
props.highlightOnlyResult = false;
expect(getIsOnlyResult(props)).to.equal(false);
});

it('returns false when custom options are allowed', () => {
props.allowNew = true;
expect(getIsOnlyResult(props)).to.equal(false);
});

it('returns false when there are more or less than one result', () => {
props.results = ['One', 'Two'];
expect(getIsOnlyResult(props)).to.equal(false);

props.results = [];
expect(getIsOnlyResult(props)).to.equal(false);
});

it('returns false when the only result is disabled', () => {
props.results = [{disabled: true}];
expect(getIsOnlyResult(props)).to.equal(false);
});
});

0 comments on commit c208e82

Please sign in to comment.