Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Regression fix for single select with onSelectResetsInput=false #2226

Merged
merged 9 commits into from
Dec 19, 2017
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ function onInputKeyDown(event) {
| `onInputKeyDown` | function | undefined | input keyDown handler; call `event.preventDefault()` to override default `Select` behaviour: `function(event) {}` |
| `onMenuScrollToBottom` | function | undefined | called when the menu is scrolled to the bottom |
| `onOpen` | function | undefined | handler for when the menu opens: `function () {}` |
| `onSelectResetsInput` | boolean | true | whether the input value should be reset when options are selected, for `multi`
| `onSelectResetsInput` | boolean | true | whether the input value should be reset when options are selected. Also input value will be set to empty if 'onSelectResetsInput=true' and Select will get new value that not equal previous value. |
| `onValueClick` | function | undefined | onClick handler for value labels: `function (value, event) {}` |
| `openOnClick` | boolean | true | open the options menu when the control is clicked (requires searchable = true) |
| `openOnFocus` | boolean | false | open the options menu when the control gets focus |
Expand Down
38 changes: 33 additions & 5 deletions src/Select.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,26 @@ const stringOrNumber = PropTypes.oneOfType([

let instanceId = 1;

const shouldShowValue = (state, props) => {
const { inputValue, isPseudoFocused, isFocused } = state;
const { onSelectResetsInput } = props;

if (!inputValue) return true;

if (!onSelectResetsInput){
return !(!isFocused && isPseudoFocused || isFocused && !isPseudoFocused);
}

return false;
};

const shouldShowPlaceholder = (state, props, isOpen) => {
const { inputValue, isPseudoFocused, isFocused } = state;
const { onSelectResetsInput } = props;

return !inputValue || !onSelectResetsInput && !isOpen && !isPseudoFocused && !isFocused;
};

class Select extends React.Component {
constructor (props) {
super(props);
Expand Down Expand Up @@ -102,7 +122,7 @@ class Select extends React.Component {
this.setState({ required: false });
}

if (this.state.inputValue && this.props.value !== nextProps.value && this.props.onSelectResetsInput) {
if (this.state.inputValue && this.props.value !== nextProps.value && nextProps.onSelectResetsInput) {
this.setState({ inputValue: this.handleInputValueChange('') });
}
}
Expand Down Expand Up @@ -347,7 +367,7 @@ class Select extends React.Component {

let toOpen = this.state.isOpen || this._openAfterFocus || this.props.openOnFocus;
toOpen = this._focusAfterClear ? false : toOpen; //if focus happens after clear values, don't open dropdown yet.

if (this.props.onFocus) {
this.props.onFocus(event);
}
Expand Down Expand Up @@ -771,7 +791,8 @@ class Select extends React.Component {
let renderLabel = this.props.valueRenderer || this.getOptionLabel;
let ValueComponent = this.props.valueComponent;
if (!valueArray.length) {
return !this.state.inputValue ? <div className="Select-placeholder">{this.props.placeholder}</div> : null;
const showPlaceholder = shouldShowPlaceholder(this.state, this.props, isOpen);
return showPlaceholder ? <div className="Select-placeholder">{this.props.placeholder}</div> : null;
}
let onClick = this.props.onValueClick ? this.handleValueClick : null;
if (this.props.multi) {
Expand All @@ -791,7 +812,7 @@ class Select extends React.Component {
</ValueComponent>
);
});
} else if (!this.state.inputValue) {
} else if (shouldShowValue(this.state, this.props)) {
if (isOpen) onClick = null;
return (
<ValueComponent
Expand All @@ -818,6 +839,13 @@ class Select extends React.Component {
&& this.state.isFocused
&& !this.state.inputValue
});

let value = this.state.inputValue;
if (value && !this.props.onSelectResetsInput && !this.state.isFocused){
// it hides input value when it is not focused and was not reset on select
value= '';
}

const inputProps = {
...this.props.inputProps,
role: 'combobox',
Expand All @@ -835,7 +863,7 @@ class Select extends React.Component {
onFocus: this.handleInputFocus,
ref: ref => this.input = ref,
required: this.state.required,
value: this.state.inputValue,
value,
};

if (this.props.inputRenderer) {
Expand Down
Loading