Skip to content

Commit

Permalink
Merge branch 'TheSharpieOne-fix/remove-shared-cache'
Browse files Browse the repository at this point in the history
  • Loading branch information
Brian Vaughn committed Oct 21, 2016
2 parents b4a087e + 4ba7bb2 commit a8f4118
Show file tree
Hide file tree
Showing 8 changed files with 1,061 additions and 678 deletions.
29 changes: 14 additions & 15 deletions examples/dist/app.js

Large diffs are not rendered by default.

127 changes: 102 additions & 25 deletions examples/dist/bundle.js

Large diffs are not rendered by default.

1,437 changes: 825 additions & 612 deletions examples/dist/common.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions examples/dist/example.css
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@
margin: -1px;
clip: rect(0, 0, 0, 0);
overflow: hidden;
float: left;
}
@-webkit-keyframes Select-animation-fadeIn {
from {
Expand Down
124 changes: 100 additions & 24 deletions examples/dist/standalone.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,26 @@ var propTypes = {
children: _react2['default'].PropTypes.func.isRequired, // Child function responsible for creating the inner Select component; (props: Object): PropTypes.element
ignoreAccents: _react2['default'].PropTypes.bool, // strip diacritics when filtering; defaults to true
ignoreCase: _react2['default'].PropTypes.bool, // perform case-insensitive filtering; defaults to true
loadingPlaceholder: _react.PropTypes.string.isRequired, // replaces the placeholder while options are loading
loadingPlaceholder: _react2['default'].PropTypes.oneOfType([// replaces the placeholder while options are loading
_react2['default'].PropTypes.string, _react2['default'].PropTypes.node]),
loadOptions: _react2['default'].PropTypes.func.isRequired, // callback to load options asynchronously; (inputValue: string, callback: Function): ?Promise
options: _react.PropTypes.array.isRequired, // array of options
placeholder: _react2['default'].PropTypes.oneOfType([// field placeholder, displayed when there's no value (shared with Select)
_react2['default'].PropTypes.string, _react2['default'].PropTypes.node]),
noResultsText: _react2['default'].PropTypes.oneOfType([// field noResultsText, displayed when no options come back from the server
_react2['default'].PropTypes.string, _react2['default'].PropTypes.node]),
onChange: _react2['default'].PropTypes.func, // onChange handler: function (newValue) {}
searchPromptText: _react2['default'].PropTypes.oneOfType([// label to prompt for search input
_react2['default'].PropTypes.string, _react2['default'].PropTypes.node])
};
_react2['default'].PropTypes.string, _react2['default'].PropTypes.node]),
onInputChange: _react2['default'].PropTypes.func, // optional for keeping track of what is being typed
value: _react2['default'].PropTypes.any };

// initial field value
var defaultCache = {};

var defaultProps = {
autoload: true,
cache: {},
cache: defaultCache,
children: defaultChildren,
ignoreAccents: true,
ignoreCase: true,
Expand All @@ -66,6 +74,8 @@ var Async = (function (_Component) {

_get(Object.getPrototypeOf(Async.prototype), 'constructor', this).call(this, props, context);

this._cache = props.cache === defaultCache ? {} : props.cache;

this.state = {
isLoading: false,
options: props.options
Expand Down Expand Up @@ -95,14 +105,19 @@ var Async = (function (_Component) {
}
});
}
}, {
key: 'clearOptions',
value: function clearOptions() {
this.setState({ options: [] });
}
}, {
key: 'loadOptions',
value: function loadOptions(inputValue) {
var _this2 = this;

var _props = this.props;
var cache = _props.cache;
var loadOptions = _props.loadOptions;
var loadOptions = this.props.loadOptions;

var cache = this._cache;

if (cache && cache.hasOwnProperty(inputValue)) {
this.setState({
Expand Down Expand Up @@ -152,9 +167,10 @@ var Async = (function (_Component) {
}, {
key: '_onInputChange',
value: function _onInputChange(inputValue) {
var _props2 = this.props;
var ignoreAccents = _props2.ignoreAccents;
var ignoreCase = _props2.ignoreCase;
var _props = this.props;
var ignoreAccents = _props.ignoreAccents;
var ignoreCase = _props.ignoreCase;
var onInputChange = _props.onInputChange;

if (ignoreAccents) {
inputValue = (0, _utilsStripDiacritics2['default'])(inputValue);
Expand All @@ -164,24 +180,65 @@ var Async = (function (_Component) {
inputValue = inputValue.toLowerCase();
}

if (onInputChange) {
onInputChange(inputValue);
}

return this.loadOptions(inputValue);
}
}, {
key: 'inputValue',
value: function inputValue() {
if (this.select) {
return this.select.state.inputValue;
}
return '';
}
}, {
key: 'noResultsText',
value: function noResultsText() {
var _props2 = this.props;
var loadingPlaceholder = _props2.loadingPlaceholder;
var noResultsText = _props2.noResultsText;
var searchPromptText = _props2.searchPromptText;
var isLoading = this.state.isLoading;

var inputValue = this.inputValue();

if (isLoading) {
return loadingPlaceholder;
}
if (inputValue && noResultsText) {
return noResultsText;
}
return searchPromptText;
}
}, {
key: 'render',
value: function render() {
var _this3 = this;

var _props3 = this.props;
var children = _props3.children;
var loadingPlaceholder = _props3.loadingPlaceholder;
var placeholder = _props3.placeholder;
var searchPromptText = _props3.searchPromptText;
var _state = this.state;
var isLoading = _state.isLoading;
var options = _state.options;

var props = {
noResultsText: isLoading ? loadingPlaceholder : searchPromptText,
noResultsText: this.noResultsText(),
placeholder: isLoading ? loadingPlaceholder : placeholder,
options: isLoading ? [] : options
options: isLoading && loadingPlaceholder ? [] : options,
ref: function ref(_ref) {
return _this3.select = _ref;
},
onChange: function onChange(newValues) {
if (_this3.props.value && newValues.length > _this3.props.value.length) {
_this3.clearOptions();
}
_this3.props.onChange(newValues);
}
};

return children(_extends({}, this.props, props, {
Expand Down Expand Up @@ -305,6 +362,9 @@ var Creatable = _react2['default'].createClass({
// ({ label: string, labelKey: string, valueKey: string }): Object
newOptionCreator: _react2['default'].PropTypes.func,

// input keyDown handler: function (event) {}
onInputKeyDown: _react2['default'].PropTypes.func,

// See Select.propTypes.options
options: _react2['default'].PropTypes.array,

Expand Down Expand Up @@ -433,7 +493,9 @@ var Creatable = _react2['default'].createClass({
},

onInputKeyDown: function onInputKeyDown(event) {
var shouldKeyDownEventCreateNewOption = this.props.shouldKeyDownEventCreateNewOption;
var _props3 = this.props;
var shouldKeyDownEventCreateNewOption = _props3.shouldKeyDownEventCreateNewOption;
var onInputKeyDown = _props3.onInputKeyDown;

var focusedOption = this.select.getFocusedOption();

Expand All @@ -442,6 +504,8 @@ var Creatable = _react2['default'].createClass({

// Prevent decorated Select from doing anything additional with this keyDown event
event.preventDefault();
} else if (onInputKeyDown) {
onInputKeyDown(event);
}
},

Expand All @@ -456,13 +520,13 @@ var Creatable = _react2['default'].createClass({
render: function render() {
var _this = this;

var _props3 = this.props;
var _props3$children = _props3.children;
var children = _props3$children === undefined ? defaultChildren : _props3$children;
var newOptionCreator = _props3.newOptionCreator;
var shouldKeyDownEventCreateNewOption = _props3.shouldKeyDownEventCreateNewOption;
var _props4 = this.props;
var _props4$children = _props4.children;
var children = _props4$children === undefined ? defaultChildren : _props4$children;
var newOptionCreator = _props4.newOptionCreator;
var shouldKeyDownEventCreateNewOption = _props4.shouldKeyDownEventCreateNewOption;

var restProps = _objectWithoutProperties(_props3, ['children', 'newOptionCreator', 'shouldKeyDownEventCreateNewOption']);
var restProps = _objectWithoutProperties(_props4, ['children', 'newOptionCreator', 'shouldKeyDownEventCreateNewOption']);

var props = _extends({}, restProps, {
allowCreate: true,
Expand Down Expand Up @@ -938,14 +1002,26 @@ var Select = _react2['default'].createClass({
},

componentWillUnmount: function componentWillUnmount() {
document.removeEventListener('touchstart', this.handleTouchOutside);
if (!document.removeEventListener && document.detachEvent) {
document.detachEvent('ontouchstart', this.handleTouchOutside);
} else {
document.removeEventListener('touchstart', this.handleTouchOutside);
}
},

toggleTouchOutsideEvent: function toggleTouchOutsideEvent(enabled) {
if (enabled) {
document.addEventListener('touchstart', this.handleTouchOutside);
if (!document.addEventListener && document.attachEvent) {
document.attachEvent('ontouchstart', this.handleTouchOutside);
} else {
document.addEventListener('touchstart', this.handleTouchOutside);
}
} else {
document.removeEventListener('touchstart', this.handleTouchOutside);
if (!document.removeEventListener && document.detachEvent) {
document.detachEvent('ontouchstart', this.handleTouchOutside);
} else {
document.removeEventListener('touchstart', this.handleTouchOutside);
}
}
},

Expand Down Expand Up @@ -1601,7 +1677,7 @@ var Select = _react2['default'].createClass({
}

if (this.props.autosize) {
return _react2['default'].createElement(_reactInputAutosize2['default'], _extends({}, inputProps, { minWidth: '5px' }));
return _react2['default'].createElement(_reactInputAutosize2['default'], _extends({}, inputProps, { minWidth: '5' }));
}
return _react2['default'].createElement(
'div',
Expand Down
10 changes: 8 additions & 2 deletions src/Async.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const propTypes = {
React.PropTypes.string,
React.PropTypes.node
]),
onChange: React.PropTypes.func, // onChange handler: function (newValue) {}
searchPromptText: React.PropTypes.oneOfType([ // label to prompt for search input
React.PropTypes.string,
React.PropTypes.node
Expand All @@ -30,9 +31,11 @@ const propTypes = {
value: React.PropTypes.any, // initial field value
};

const defaultCache = {};

const defaultProps = {
autoload: true,
cache: {},
cache: defaultCache,
children: defaultChildren,
ignoreAccents: true,
ignoreCase: true,
Expand All @@ -45,6 +48,8 @@ export default class Async extends Component {
constructor (props, context) {
super(props, context);

this._cache = props.cache === defaultCache ? {} : props.cache;

this.state = {
isLoading: false,
options: props.options,
Expand Down Expand Up @@ -77,7 +82,8 @@ export default class Async extends Component {
}

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

if (
cache &&
Expand Down
3 changes: 3 additions & 0 deletions src/Creatable.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ const Creatable = React.createClass({
// ({ label: string, labelKey: string, valueKey: string }): Object
newOptionCreator: React.PropTypes.func,

// input keyDown handler: function (event) {}
onInputKeyDown: React.PropTypes.func,

// See Select.propTypes.options
options: React.PropTypes.array,

Expand Down
8 changes: 8 additions & 0 deletions test/Async-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,14 @@ describe('Async', () => {
typeSearchText('a');
return expect(loadOptions, 'was called times', 1);
});

it('should not use the same cache for every instance by default', () => {
createControl();
const instance1 = asyncInstance;
createControl();
const instance2 = asyncInstance;
expect(instance1._cache !== instance2._cache, 'to equal', true);
});
});

describe('loadOptions', () => {
Expand Down

0 comments on commit a8f4118

Please sign in to comment.