-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented and tested cache. Also fixed a spacing issue.
- Loading branch information
Showing
2 changed files
with
246 additions
and
190 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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, | ||
|
@@ -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.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
bvaughn
Author
Collaborator
|
||
} | ||
|
||
this.setState({ | ||
isLoading: false, | ||
options | ||
|
Oops, something went wrong.
The cache property(from docs) is a boolean. This fails with
- Uncaught TypeError: Cannot create property 'ni' on boolean 'true'
https://github.com/JedWatson/react-select#further-options
@bvaughn