From 7afbdc0cca99b791198bb75f9117ba33ed890360 Mon Sep 17 00:00:00 2001 From: yiminghe Date: Wed, 20 Apr 2016 18:49:20 +0800 Subject: [PATCH] select on blur --- package.json | 2 +- src/Select.jsx | 17 ++++++++++++++++- src/util.js | 15 +++++++++++++++ 3 files changed, 32 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 6ad4aae74..70929a5e2 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "rc-select", - "version": "6.2.1", + "version": "6.2.2", "description": "React Select", "keywords": [ "react", diff --git a/src/Select.jsx b/src/Select.jsx index be5f205c9..697060e7f 100644 --- a/src/Select.jsx +++ b/src/Select.jsx @@ -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'; @@ -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) { @@ -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; } diff --git a/src/util.js b/src/util.js index 8a6f47049..f34bfd223 100644 --- a/src/util.js +++ b/src/util.js @@ -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; +}