Skip to content

Commit

Permalink
Implemented and tested cache. Also fixed a spacing issue.
Browse files Browse the repository at this point in the history
  • Loading branch information
bvaughn committed Sep 18, 2016
1 parent ed8d087 commit b319024
Show file tree
Hide file tree
Showing 2 changed files with 246 additions and 190 deletions.
39 changes: 27 additions & 12 deletions src/Async.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,28 @@ import React, { Component, PropTypes } from 'react';
import Select from './Select';
import stripDiacritics from './utils/stripDiacritics';

// @TODO Implement cache

const propTypes = {
autoload: React.PropTypes.bool.isRequired,
children: React.PropTypes.func.isRequired, // Child function responsible for creating the inner Select component; (props: Object): PropTypes.element
ignoreAccents: React.PropTypes.bool, // whether to strip diacritics when filtering (shared with Select)
ignoreCase: React.PropTypes.bool, // whether to perform case-insensitive filtering (shared with Select)
loadingPlaceholder: PropTypes.string.isRequired,
loadOptions: React.PropTypes.func.isRequired,
options: PropTypes.array.isRequired,
placeholder: React.PropTypes.oneOfType([
autoload: React.PropTypes.bool.isRequired, // automatically call the `loadOptions` prop on-mount; defaults to true
cache: React.PropTypes.any, // object to use to cache results; set to null/false to disable caching
children: React.PropTypes.func.isRequired, // Child function responsible for creating the inner Select component; (props: Object): PropTypes.element
ignoreAccents: React.PropTypes.bool, // strip diacritics when filtering; defaults to true
ignoreCase: React.PropTypes.bool, // perform case-insensitive filtering; defaults to true
loadingPlaceholder: PropTypes.string.isRequired, // replaces the placeholder while options are loading
loadOptions: React.PropTypes.func.isRequired, // callback to load options asynchronously; (inputValue: string, callback: Function): ?Promise
options: PropTypes.array.isRequired, // array of options
placeholder: React.PropTypes.oneOfType([ // field placeholder, displayed when there's no value (shared with Select)
React.PropTypes.string,
React.PropTypes.node
]),
searchPromptText: React.PropTypes.oneOfType([
searchPromptText: React.PropTypes.oneOfType([ // label to prompt for search input
React.PropTypes.string,
React.PropTypes.node
]),
};

const defaultProps = {
autoload: true,
cache: {},
children: defaultChildren,
ignoreAccents: true,
ignoreCase: true,
Expand Down Expand Up @@ -64,14 +64,29 @@ export default class Async extends Component {
}

loadOptions (inputValue) {
const { loadOptions } = this.props;
const { cache, loadOptions } = this.props;

if (
cache &&
cache.hasOwnProperty(inputValue)
) {
this.setState({
options: cache[inputValue]
});

return;
}

const callback = (error, data) => {
if (callback === this._callback) {
this._callback = null;

const options = data && data.options || [];

if (cache) {
cache[inputValue] = options;

This comment has been minimized.

Copy link
@bigomega

bigomega Sep 28, 2016

The cache property(from docs) is a boolean. This fails with

- Uncaught TypeError: Cannot create property 'ni' on boolean 'true'

cache - bool - true - enables the options cache for asyncOptions (default: true)

https://github.com/JedWatson/react-select#further-options

@bvaughn

This comment has been minimized.

Copy link
@bvaughn

bvaughn Sep 28, 2016

Author Collaborator

The way it's intended for use is that cache is an empty Object by default and you can pass false to disable it.

I didn't write the docs but I guess they're a bit unclear on that fact. You don't need to pass cache={true}. Just omit the property if you want to enable the cache.

This comment has been minimized.

Copy link
@bigomega

bigomega Sep 29, 2016

I ended up passing cache={{}}, but now that you mention it, yes, omitting the property seems to work.

}

this.setState({
isLoading: false,
options
Expand Down
Loading

0 comments on commit b319024

Please sign in to comment.