Skip to content

Commit

Permalink
fix clear button
Browse files Browse the repository at this point in the history
  • Loading branch information
yiminghe committed Apr 28, 2016
1 parent b65e4f3 commit 1567bf6
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 23 deletions.
5 changes: 5 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# History
----

## 6.3.0 / 2016-04-28

- support onBlur

## 6.2.0 / 2016-04-20

- remove searchPlaceholder
Expand Down
6 changes: 6 additions & 0 deletions examples/single.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const Test = React.createClass({
} else {
value = e;
}
console.log('onChange', value);
this.setState({
value,
});
Expand All @@ -31,6 +32,10 @@ const Test = React.createClass({
});
},

onBlur(v) {
console.log('onBlur', v);
},

render() {
if (this.state.destroy) {
return null;
Expand All @@ -47,6 +52,7 @@ const Test = React.createClass({
placeholder="placeholder"
dropdownMenuStyle={{ maxHeight: 200, overflow: 'auto' }}
style={{ width: 500 }}
onBlur={this.onBlur}
allowClear
optionLabelProp="children"
optionFilterProp="text"
Expand Down
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.3",
"version": "6.3.0",
"description": "React Select",
"keywords": [
"react",
Expand Down
68 changes: 46 additions & 22 deletions src/Select.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ const Select = React.createClass({
animation: PropTypes.string,
choiceTransitionName: PropTypes.string,
onChange: PropTypes.func,
onBlur: PropTypes.func,
onSelect: PropTypes.func,
onSearch: PropTypes.func,
placeholder: PropTypes.any,
Expand All @@ -69,6 +70,7 @@ const Select = React.createClass({
placeholder: '',
defaultValue: [],
onChange: noop,
onBlur: noop,
onSelect: noop,
onSearch: noop,
onDeselect: noop,
Expand Down Expand Up @@ -136,6 +138,7 @@ const Select = React.createClass({
},

componentWillUnmount() {
this.clearBlurTime();
if (this.dropdownContainer) {
ReactDOM.unmountComponentAtNode(this.dropdownContainer);
document.body.removeChild(this.dropdownContainer);
Expand Down Expand Up @@ -282,6 +285,7 @@ const Select = React.createClass({
},

onOuterFocus() {
this.clearBlurTime();
this._focused = true;
this.updateFocusClassName();
},
Expand All @@ -292,22 +296,27 @@ 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),
}]);
this.blurTimer = setTimeout(() => {
this._focused = false;
this.updateFocusClassName();
const props = this.props;
let { value } = this.state;
if (isSingleMode(props) && props.showSearch &&
this.state.inputValue && props.defaultActiveFirstOption) {
const options = this._options || [];
if (options.length) {
const firstOption = findFirstMenuItem(options);
if (firstOption) {
value = [{
key: firstOption.key,
label: this.getLabelFromOption(firstOption),
}];
this.fireChange(value);
}
}
}
}
props.onBlur(this.getVLForOnChange(value));
}, 10);
},

onClearSelection(event) {
Expand All @@ -318,11 +327,15 @@ const Select = React.createClass({
}
event.stopPropagation();
if (state.inputValue || state.value.length) {
this.fireChange([]);
this.setOpenState(false);
this.setState({
inputValue: '',
});
if (this.state.value.length) {
this.fireChange([]);
}
this.setOpenState(false, true);
if (this.state.inputValue) {
this.setState({
inputValue: '',
});
}
}
},

Expand Down Expand Up @@ -457,7 +470,12 @@ const Select = React.createClass({
}
});
},

clearBlurTime() {
if (this.blurTimer) {
clearTimeout(this.blurTimer);
this.blurTimer = null;
}
},
updateFocusClassName() {
const { refs, props } = this;
// avoid setState and its side effect
Expand Down Expand Up @@ -677,11 +695,17 @@ const Select = React.createClass({
[`${prefixCls}-enabled`]: !disabled,
[`${prefixCls}-allow-clear`]: !!props.allowClear,
};

const clearStyle = {
...UNSELECTABLE_STYLE,
display: 'none',
};
if (this.state.inputValue || this.state.value.length) {
clearStyle.display = 'block';
}
const clear = (<span
key="clear"
onMouseDown={preventDefaultEvent}
style={UNSELECTABLE_STYLE}
style={clearStyle}
{...UNSELECTABLE_ATTRIBUTE}
className={`${prefixCls}-selection__clear`}
onClick={this.onClearSelection}
Expand Down

0 comments on commit 1567bf6

Please sign in to comment.