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

Added onBlurResetsInput property to clear input onBlur or leave parti… #3242

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 11 additions & 4 deletions src/Select.js
Expand Up @@ -200,6 +200,8 @@ export type Props = {
noOptionsMessage: ({ inputValue: string }) => string | null,
/* Handle blur events on the control */
onBlur?: FocusEventHandler,
/* Whether to clear partial input when the control is blurred */
onBlurResetsInput: boolean,
/* Handle change events on the select */
onChange: (ValueType, ActionMeta) => void,
/* Handle focus events on the control */
Expand Down Expand Up @@ -274,6 +276,7 @@ export const defaultProps = {
noOptionsMessage: () => 'No options',
openMenuOnFocus: false,
openMenuOnClick: true,
onBlurResetsInput: true,
options: [],
pageSize: 5,
placeholder: 'Select...',
Expand Down Expand Up @@ -453,7 +456,9 @@ export default class Select extends Component<Props, State> {
event: 'input',
context: { isSearchable, isMulti },
});
this.onInputChange('', { action: 'menu-close' });
if (this.props.onBlurResetsInput) {
this.onInputChange('', { action: 'menu-close' });
}
this.props.onMenuClose();
}
onInputChange(newValue: string, actionMeta: InputActionMeta) {
Expand Down Expand Up @@ -1071,7 +1076,9 @@ export default class Select extends Component<Props, State> {
if (this.props.onBlur) {
this.props.onBlur(event);
}
this.onInputChange('', { action: 'input-blur' });
if (this.props.onBlurResetsInput) {
this.onInputChange('', {action: 'input-blur'});
}
this.onMenuClose();
this.setState({
focusedValue: null,
Expand Down Expand Up @@ -1427,8 +1434,8 @@ export default class Select extends Component<Props, State> {

if (!this.hasValue() || !controlShouldRenderValue) {
return inputValue ? null : (
<Placeholder
{...commonProps}
<Placeholder
{...commonProps}
key="placeholder"
isDisabled={isDisabled}
isFocused={isFocused}
Expand Down
20 changes: 18 additions & 2 deletions src/__tests__/Select.test.js
Expand Up @@ -1432,7 +1432,7 @@ test('should call onChange with `null` on hitting backspace when backspaceRemove
selectWrapper
.find(Control)
.simulate('keyDown', { keyCode: 8, key: 'Backspace' });
expect(onChangeSpy).toHaveBeenCalledWith(null, { action: 'clear' });
expect(onChangeSpy).toHaveBeenCalledWith(null, { action: 'clear', name: 'test-input-name' });
});

test('should call onChange with an array on hitting backspace when backspaceRemovesValue is true and isMulti is true', () => {
Expand All @@ -1449,7 +1449,7 @@ test('should call onChange with an array on hitting backspace when backspaceRemo
selectWrapper
.find(Control)
.simulate('keyDown', { keyCode: 8, key: 'Backspace' });
expect(onChangeSpy).toHaveBeenCalledWith([], { action: 'pop-value' });
expect(onChangeSpy).toHaveBeenCalledWith([], { action: 'pop-value', name: 'test-input-name', removedValue: undefined });
});

test('multi select > clicking on X next to option will call onChange with all options other that the clicked option', () => {
Expand Down Expand Up @@ -1735,6 +1735,22 @@ test('onInputChange() function prop to be called on blur', () => {
expect(onInputChangeSpy).toHaveBeenCalledTimes(2);
});

test('onInputChange() function prop to not be called on blur with onBlurResetsInput', () => {
let onInputChangeSpy = jest.fn();
let selectWrapper = mount(
<Select
{...BASIC_PROPS}
onBlurResetsInput={false}
onBlur={jest.fn()}
onInputChange={onInputChangeSpy}
onMenuClose={jest.fn()}
/>
);
selectWrapper.find('Control input').simulate('blur');
// onInputChangeSpy should not be called on blur with onBlurResetsInput={false}
expect(onInputChangeSpy).toHaveBeenCalledTimes(0);
});

test('onMenuClose() function prop to be called on blur', () => {
let onMenuCloseSpy = jest.fn();
let selectWrapper = mount(
Expand Down
8 changes: 8 additions & 0 deletions src/__tests__/__snapshots__/Async.test.js.snap
Expand Up @@ -50,6 +50,7 @@ exports[`defaults - snapshot 1`] = `
menuShouldScrollIntoView={true}
minMenuHeight={140}
noOptionsMessage={[Function]}
onBlurResetsInput={true}
onChange={[Function]}
onInputChange={[Function]}
onMenuClose={[Function]}
Expand Down Expand Up @@ -118,6 +119,7 @@ exports[`defaults - snapshot 1`] = `
"menuShouldScrollIntoView": true,
"minMenuHeight": 140,
"noOptionsMessage": [Function],
"onBlurResetsInput": true,
"onChange": [Function],
"onInputChange": [Function],
"onMenuClose": [Function],
Expand Down Expand Up @@ -223,6 +225,7 @@ exports[`defaults - snapshot 1`] = `
"menuShouldScrollIntoView": true,
"minMenuHeight": 140,
"noOptionsMessage": [Function],
"onBlurResetsInput": true,
"onChange": [Function],
"onInputChange": [Function],
"onMenuClose": [Function],
Expand Down Expand Up @@ -321,6 +324,7 @@ exports[`defaults - snapshot 1`] = `
"menuShouldScrollIntoView": true,
"minMenuHeight": 140,
"noOptionsMessage": [Function],
"onBlurResetsInput": true,
"onChange": [Function],
"onInputChange": [Function],
"onMenuClose": [Function],
Expand Down Expand Up @@ -419,6 +423,7 @@ exports[`defaults - snapshot 1`] = `
"menuShouldScrollIntoView": true,
"minMenuHeight": 140,
"noOptionsMessage": [Function],
"onBlurResetsInput": true,
"onChange": [Function],
"onInputChange": [Function],
"onMenuClose": [Function],
Expand Down Expand Up @@ -654,6 +659,7 @@ exports[`defaults - snapshot 1`] = `
"menuShouldScrollIntoView": true,
"minMenuHeight": 140,
"noOptionsMessage": [Function],
"onBlurResetsInput": true,
"onChange": [Function],
"onInputChange": [Function],
"onMenuClose": [Function],
Expand Down Expand Up @@ -751,6 +757,7 @@ exports[`defaults - snapshot 1`] = `
"menuShouldScrollIntoView": true,
"minMenuHeight": 140,
"noOptionsMessage": [Function],
"onBlurResetsInput": true,
"onChange": [Function],
"onInputChange": [Function],
"onMenuClose": [Function],
Expand Down Expand Up @@ -856,6 +863,7 @@ exports[`defaults - snapshot 1`] = `
"menuShouldScrollIntoView": true,
"minMenuHeight": 140,
"noOptionsMessage": [Function],
"onBlurResetsInput": true,
"onChange": [Function],
"onInputChange": [Function],
"onMenuClose": [Function],
Expand Down
8 changes: 8 additions & 0 deletions src/__tests__/__snapshots__/AsyncCreatable.test.js.snap
Expand Up @@ -77,6 +77,7 @@ exports[`defaults - snapshot 1`] = `
menuShouldScrollIntoView={true}
minMenuHeight={140}
noOptionsMessage={[Function]}
onBlurResetsInput={true}
onChange={[Function]}
onInputChange={[Function]}
onMenuClose={[Function]}
Expand Down Expand Up @@ -150,6 +151,7 @@ exports[`defaults - snapshot 1`] = `
"menuShouldScrollIntoView": true,
"minMenuHeight": 140,
"noOptionsMessage": [Function],
"onBlurResetsInput": true,
"onChange": [Function],
"onInputChange": [Function],
"onMenuClose": [Function],
Expand Down Expand Up @@ -260,6 +262,7 @@ exports[`defaults - snapshot 1`] = `
"menuShouldScrollIntoView": true,
"minMenuHeight": 140,
"noOptionsMessage": [Function],
"onBlurResetsInput": true,
"onChange": [Function],
"onInputChange": [Function],
"onMenuClose": [Function],
Expand Down Expand Up @@ -363,6 +366,7 @@ exports[`defaults - snapshot 1`] = `
"menuShouldScrollIntoView": true,
"minMenuHeight": 140,
"noOptionsMessage": [Function],
"onBlurResetsInput": true,
"onChange": [Function],
"onInputChange": [Function],
"onMenuClose": [Function],
Expand Down Expand Up @@ -466,6 +470,7 @@ exports[`defaults - snapshot 1`] = `
"menuShouldScrollIntoView": true,
"minMenuHeight": 140,
"noOptionsMessage": [Function],
"onBlurResetsInput": true,
"onChange": [Function],
"onInputChange": [Function],
"onMenuClose": [Function],
Expand Down Expand Up @@ -706,6 +711,7 @@ exports[`defaults - snapshot 1`] = `
"menuShouldScrollIntoView": true,
"minMenuHeight": 140,
"noOptionsMessage": [Function],
"onBlurResetsInput": true,
"onChange": [Function],
"onInputChange": [Function],
"onMenuClose": [Function],
Expand Down Expand Up @@ -808,6 +814,7 @@ exports[`defaults - snapshot 1`] = `
"menuShouldScrollIntoView": true,
"minMenuHeight": 140,
"noOptionsMessage": [Function],
"onBlurResetsInput": true,
"onChange": [Function],
"onInputChange": [Function],
"onMenuClose": [Function],
Expand Down Expand Up @@ -918,6 +925,7 @@ exports[`defaults - snapshot 1`] = `
"menuShouldScrollIntoView": true,
"minMenuHeight": 140,
"noOptionsMessage": [Function],
"onBlurResetsInput": true,
"onChange": [Function],
"onInputChange": [Function],
"onMenuClose": [Function],
Expand Down
7 changes: 7 additions & 0 deletions src/__tests__/__snapshots__/Select.test.js.snap
Expand Up @@ -48,6 +48,7 @@ exports[`snapshot - defaults 1`] = `
"menuShouldScrollIntoView": true,
"minMenuHeight": 140,
"noOptionsMessage": [Function],
"onBlurResetsInput": true,
"openMenuOnClick": true,
"openMenuOnFocus": false,
"options": Array [],
Expand Down Expand Up @@ -138,6 +139,7 @@ exports[`snapshot - defaults 1`] = `
"menuShouldScrollIntoView": true,
"minMenuHeight": 140,
"noOptionsMessage": [Function],
"onBlurResetsInput": true,
"openMenuOnClick": true,
"openMenuOnFocus": false,
"options": Array [],
Expand Down Expand Up @@ -220,6 +222,7 @@ exports[`snapshot - defaults 1`] = `
"menuShouldScrollIntoView": true,
"minMenuHeight": 140,
"noOptionsMessage": [Function],
"onBlurResetsInput": true,
"openMenuOnClick": true,
"openMenuOnFocus": false,
"options": Array [],
Expand Down Expand Up @@ -304,6 +307,7 @@ exports[`snapshot - defaults 1`] = `
"menuShouldScrollIntoView": true,
"minMenuHeight": 140,
"noOptionsMessage": [Function],
"onBlurResetsInput": true,
"openMenuOnClick": true,
"openMenuOnFocus": false,
"options": Array [],
Expand Down Expand Up @@ -436,6 +440,7 @@ exports[`snapshot - defaults 1`] = `
"menuShouldScrollIntoView": true,
"minMenuHeight": 140,
"noOptionsMessage": [Function],
"onBlurResetsInput": true,
"openMenuOnClick": true,
"openMenuOnFocus": false,
"options": Array [],
Expand Down Expand Up @@ -519,6 +524,7 @@ exports[`snapshot - defaults 1`] = `
"menuShouldScrollIntoView": true,
"minMenuHeight": 140,
"noOptionsMessage": [Function],
"onBlurResetsInput": true,
"openMenuOnClick": true,
"openMenuOnFocus": false,
"options": Array [],
Expand Down Expand Up @@ -609,6 +615,7 @@ exports[`snapshot - defaults 1`] = `
"menuShouldScrollIntoView": true,
"minMenuHeight": 140,
"noOptionsMessage": [Function],
"onBlurResetsInput": true,
"openMenuOnClick": true,
"openMenuOnFocus": false,
"options": Array [],
Expand Down
1 change: 1 addition & 0 deletions src/__tests__/__snapshots__/StateManaged.test.js.snap
Expand Up @@ -33,6 +33,7 @@ exports[`defaults > snapshot 1`] = `
menuShouldScrollIntoView={true}
minMenuHeight={140}
noOptionsMessage={[Function]}
onBlurResetsInput={true}
onChange={[Function]}
onInputChange={[Function]}
onMenuClose={[Function]}
Expand Down