Skip to content

Commit

Permalink
select on blur
Browse files Browse the repository at this point in the history
  • Loading branch information
yiminghe committed Apr 20, 2016
1 parent 1f816aa commit 7afbdc0
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "rc-select",
"version": "6.2.1",
"version": "6.2.2",
"description": "React Select",
"keywords": [
"react",
Expand Down
17 changes: 16 additions & 1 deletion src/Select.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
isMultipleOrTags, isMultipleOrTagsOrCombobox,
isSingleMode, toArray, findIndexInValueByKey,
UNSELECTABLE_ATTRIBUTE, UNSELECTABLE_STYLE,
preventDefaultEvent,
preventDefaultEvent, findFirstMenuItem,
} from './util';
import SelectTrigger from './SelectTrigger';
import FilterMixin from './FilterMixin';
Expand Down Expand Up @@ -292,6 +292,20 @@ const Select = React.createClass({
onOuterBlur() {
this._focused = false;
this.updateFocusClassName();
const props = this.props;
if (isSingleMode(props) && props.showSearch &&
this.state.inputValue && props.defaultActiveFirstOption) {
const options = this._options || [];
if (options.length) {
const firstOption = findFirstMenuItem(options);
if (firstOption) {
this.fireChange([{
key: firstOption.key,
label: this.getLabelFromOption(firstOption),
}]);
}
}
}
},

onClearSelection(event) {
Expand Down Expand Up @@ -641,6 +655,7 @@ const Select = React.createClass({
if (open) {
options = this.renderFilterOptions();
}
this._options = options;
if (open && (isMultipleOrTagsOrCombobox(props) || !props.showSearch) && !options.length) {
open = false;
}
Expand Down
15 changes: 15 additions & 0 deletions src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,18 @@ export const UNSELECTABLE_STYLE = {
export const UNSELECTABLE_ATTRIBUTE = {
unselectable: 'unselectable',
};

export function findFirstMenuItem(children) {
for (let i = 0; i < children.length; i++) {
const child = children[i];
if (child.type === MenuItemGroup) {
const found = findFirstMenuItem(child.props.children);
if (found) {
return found;
}
} else if (!child.props.disabled) {
return child;
}
}
return null;
}

0 comments on commit 7afbdc0

Please sign in to comment.