Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions docs/examples/NoDefaultMenuOptionFocus.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import React from 'react';
import Select from 'react-select';

import { colourOptions } from '../data';

const NoDefaultMenuOptionFocus = () => (
<Select
options={colourOptions}
focusDefaultOption={false}
/>
);

export default NoDefaultMenuOptionFocus;
1 change: 1 addition & 0 deletions docs/examples/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,4 @@ export { default as MenuBuffer } from './MenuBuffer';
export { default as MenuPortal } from './MenuPortal';
export { default as Theme } from './Theme';
export { default as StyleCompositionExample } from './StyleCompositionExample';
export { default as NoDefaultMenuOptionFocus } from './NoDefaultMenuOptionFocus';
16 changes: 16 additions & 0 deletions docs/pages/advanced/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
Popout,
MenuBuffer,
MenuPortal,
NoDefaultMenuOptionFocus,
MultiSelectSort,
} from '../../examples';

Expand Down Expand Up @@ -199,6 +200,21 @@ export default function Advanced() {
<ControlledMenu />
</ExampleWrapper>
)}

## Focus Default Option
For when you want to prevent React Select from focusing the default option in the dropdown menu you can set this to false.
It can be quite usefull when you want React Select to act as a search input. The user needs to be able to enter text,
press the enter key and search what the user entered into the input, not what the first option in the menu is.

${(
<ExampleWrapper
label="Example of disabling the default dropdown option focus"
urlPath="docs/examples/NoDefaultMenuOptionFocus.js"
raw={require('!!raw-loader!../../examples/NoDefaultMenuOptionFocus.js')}
>
<NoDefaultMenuOptionFocus />
</ExampleWrapper>
)}

## Accessing Internals
${(
Expand Down
25 changes: 20 additions & 5 deletions packages/react-select/src/Select.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,8 @@ export type Props = {
formatGroupLabel: typeof formatGroupLabel,
/* Formats option labels in the menu and control as React components */
formatOptionLabel?: (OptionType, FormatOptionLabelMeta) => Node,
/* Applies focus to the first option in the menu when opened */
focusDefaultOption: boolean,
/* Resolves option data to a string to be displayed as the label by components */
getOptionLabel: typeof getOptionLabel,
/* Resolves option data to a string to compare options and specify value attributes */
Expand Down Expand Up @@ -256,6 +258,7 @@ export const defaultProps = {
escapeClearsValue: false,
filterOption: createFilter(),
formatGroupLabel: formatGroupLabel,
focusDefaultOption: true,
getOptionLabel: getOptionLabel,
getOptionValue: getOptionValue,
isDisabled: false,
Expand Down Expand Up @@ -402,9 +405,21 @@ export default class Select extends Component<Props, State> {
const menuOptions = nextProps.menuIsOpen
? this.buildMenuOptions(nextProps, selectValue)
: { render: [], focusable: [] };
const focusedValue = this.getNextFocusedValue(selectValue);
const focusedOption = this.getNextFocusedOption(menuOptions.focusable);
this.setState({ menuOptions, selectValue, focusedOption, focusedValue });

let focusedValue = null;
let focusedOption = null;

if (nextProps.focusDefaultOption) {
focusedValue = this.getNextFocusedValue(selectValue);
focusedOption = this.getNextFocusedOption(menuOptions.focusable);
}

this.setState({
menuOptions,
selectValue,
focusedOption,
focusedValue,
});
}
// some updates should toggle the state of the input visibility
if (this.inputIsHiddenAfterUpdate != null) {
Expand Down Expand Up @@ -484,7 +499,7 @@ export default class Select extends Component<Props, State> {

openMenu(focusOption: 'first' | 'last') {
const { menuOptions, selectValue, isFocused } = this.state;
const { isMulti } = this.props;
const { isMulti, focusDefaultOption } = this.props;
let openAtIndex =
focusOption === 'first' ? 0 : menuOptions.focusable.length - 1;

Expand All @@ -502,7 +517,7 @@ export default class Select extends Component<Props, State> {
this.onMenuOpen();
this.setState({
focusedValue: null,
focusedOption: menuOptions.focusable[openAtIndex],
focusedOption: focusDefaultOption ? menuOptions.focusable[openAtIndex] : null,
});

this.announceAriaLiveContext({ event: 'menu' });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ exports[`defaults - snapshot 1`] = `
defaultOptions={false}
escapeClearsValue={false}
filterOption={null}
focusDefaultOption={true}
formatGroupLabel={[Function]}
getOptionLabel={[Function]}
getOptionValue={[Function]}
Expand Down Expand Up @@ -230,6 +231,7 @@ exports[`defaults - snapshot 1`] = `
"defaultOptions": false,
"escapeClearsValue": false,
"filterOption": null,
"focusDefaultOption": true,
"formatGroupLabel": [Function],
"getOptionLabel": [Function],
"getOptionValue": [Function],
Expand Down Expand Up @@ -333,6 +335,7 @@ exports[`defaults - snapshot 1`] = `
"defaultOptions": false,
"escapeClearsValue": false,
"filterOption": null,
"focusDefaultOption": true,
"formatGroupLabel": [Function],
"getOptionLabel": [Function],
"getOptionValue": [Function],
Expand Down Expand Up @@ -428,6 +431,7 @@ exports[`defaults - snapshot 1`] = `
"defaultOptions": false,
"escapeClearsValue": false,
"filterOption": null,
"focusDefaultOption": true,
"formatGroupLabel": [Function],
"getOptionLabel": [Function],
"getOptionValue": [Function],
Expand Down Expand Up @@ -523,6 +527,7 @@ exports[`defaults - snapshot 1`] = `
"defaultOptions": false,
"escapeClearsValue": false,
"filterOption": null,
"focusDefaultOption": true,
"formatGroupLabel": [Function],
"getOptionLabel": [Function],
"getOptionValue": [Function],
Expand Down Expand Up @@ -622,6 +627,7 @@ exports[`defaults - snapshot 1`] = `
"defaultOptions": false,
"escapeClearsValue": false,
"filterOption": null,
"focusDefaultOption": true,
"formatGroupLabel": [Function],
"getOptionLabel": [Function],
"getOptionValue": [Function],
Expand Down Expand Up @@ -805,6 +811,7 @@ exports[`defaults - snapshot 1`] = `
"defaultOptions": false,
"escapeClearsValue": false,
"filterOption": null,
"focusDefaultOption": true,
"formatGroupLabel": [Function],
"getOptionLabel": [Function],
"getOptionValue": [Function],
Expand Down Expand Up @@ -899,6 +906,7 @@ exports[`defaults - snapshot 1`] = `
"defaultOptions": false,
"escapeClearsValue": false,
"filterOption": null,
"focusDefaultOption": true,
"formatGroupLabel": [Function],
"getOptionLabel": [Function],
"getOptionValue": [Function],
Expand Down Expand Up @@ -1001,6 +1009,7 @@ exports[`defaults - snapshot 1`] = `
"defaultOptions": false,
"escapeClearsValue": false,
"filterOption": null,
"focusDefaultOption": true,
"formatGroupLabel": [Function],
"getOptionLabel": [Function],
"getOptionValue": [Function],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ exports[`defaults - snapshot 1`] = `
defaultOptions={false}
escapeClearsValue={false}
filterOption={null}
focusDefaultOption={true}
formatCreateLabel={[Function]}
formatGroupLabel={[Function]}
getNewOptionData={[Function]}
Expand Down Expand Up @@ -256,6 +257,7 @@ exports[`defaults - snapshot 1`] = `
"defaultOptions": false,
"escapeClearsValue": false,
"filterOption": null,
"focusDefaultOption": true,
"formatCreateLabel": [Function],
"formatGroupLabel": [Function],
"getNewOptionData": [Function],
Expand Down Expand Up @@ -364,6 +366,7 @@ exports[`defaults - snapshot 1`] = `
"defaultOptions": false,
"escapeClearsValue": false,
"filterOption": null,
"focusDefaultOption": true,
"formatCreateLabel": [Function],
"formatGroupLabel": [Function],
"getNewOptionData": [Function],
Expand Down Expand Up @@ -464,6 +467,7 @@ exports[`defaults - snapshot 1`] = `
"defaultOptions": false,
"escapeClearsValue": false,
"filterOption": null,
"focusDefaultOption": true,
"formatCreateLabel": [Function],
"formatGroupLabel": [Function],
"getNewOptionData": [Function],
Expand Down Expand Up @@ -564,6 +568,7 @@ exports[`defaults - snapshot 1`] = `
"defaultOptions": false,
"escapeClearsValue": false,
"filterOption": null,
"focusDefaultOption": true,
"formatCreateLabel": [Function],
"formatGroupLabel": [Function],
"getNewOptionData": [Function],
Expand Down Expand Up @@ -668,6 +673,7 @@ exports[`defaults - snapshot 1`] = `
"defaultOptions": false,
"escapeClearsValue": false,
"filterOption": null,
"focusDefaultOption": true,
"formatCreateLabel": [Function],
"formatGroupLabel": [Function],
"getNewOptionData": [Function],
Expand Down Expand Up @@ -856,6 +862,7 @@ exports[`defaults - snapshot 1`] = `
"defaultOptions": false,
"escapeClearsValue": false,
"filterOption": null,
"focusDefaultOption": true,
"formatCreateLabel": [Function],
"formatGroupLabel": [Function],
"getNewOptionData": [Function],
Expand Down Expand Up @@ -955,6 +962,7 @@ exports[`defaults - snapshot 1`] = `
"defaultOptions": false,
"escapeClearsValue": false,
"filterOption": null,
"focusDefaultOption": true,
"formatCreateLabel": [Function],
"formatGroupLabel": [Function],
"getNewOptionData": [Function],
Expand Down Expand Up @@ -1062,6 +1070,7 @@ exports[`defaults - snapshot 1`] = `
"defaultOptions": false,
"escapeClearsValue": false,
"filterOption": null,
"focusDefaultOption": true,
"formatCreateLabel": [Function],
"formatGroupLabel": [Function],
"getNewOptionData": [Function],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ exports[`snapshot - defaults 1`] = `
"controlShouldRenderValue": true,
"escapeClearsValue": false,
"filterOption": [Function],
"focusDefaultOption": true,
"formatGroupLabel": [Function],
"getOptionLabel": [Function],
"getOptionValue": [Function],
Expand Down Expand Up @@ -121,6 +122,7 @@ exports[`snapshot - defaults 1`] = `
"controlShouldRenderValue": true,
"escapeClearsValue": false,
"filterOption": [Function],
"focusDefaultOption": true,
"formatGroupLabel": [Function],
"getOptionLabel": [Function],
"getOptionValue": [Function],
Expand Down Expand Up @@ -203,6 +205,7 @@ exports[`snapshot - defaults 1`] = `
"controlShouldRenderValue": true,
"escapeClearsValue": false,
"filterOption": [Function],
"focusDefaultOption": true,
"formatGroupLabel": [Function],
"getOptionLabel": [Function],
"getOptionValue": [Function],
Expand Down Expand Up @@ -287,6 +290,7 @@ exports[`snapshot - defaults 1`] = `
"controlShouldRenderValue": true,
"escapeClearsValue": false,
"filterOption": [Function],
"focusDefaultOption": true,
"formatGroupLabel": [Function],
"getOptionLabel": [Function],
"getOptionValue": [Function],
Expand Down Expand Up @@ -374,6 +378,7 @@ exports[`snapshot - defaults 1`] = `
"controlShouldRenderValue": true,
"escapeClearsValue": false,
"filterOption": [Function],
"focusDefaultOption": true,
"formatGroupLabel": [Function],
"getOptionLabel": [Function],
"getOptionValue": [Function],
Expand Down Expand Up @@ -459,6 +464,7 @@ exports[`snapshot - defaults 1`] = `
"controlShouldRenderValue": true,
"escapeClearsValue": false,
"filterOption": [Function],
"focusDefaultOption": true,
"formatGroupLabel": [Function],
"getOptionLabel": [Function],
"getOptionValue": [Function],
Expand Down Expand Up @@ -542,6 +548,7 @@ exports[`snapshot - defaults 1`] = `
"controlShouldRenderValue": true,
"escapeClearsValue": false,
"filterOption": [Function],
"focusDefaultOption": true,
"formatGroupLabel": [Function],
"getOptionLabel": [Function],
"getOptionValue": [Function],
Expand Down Expand Up @@ -632,6 +639,7 @@ exports[`snapshot - defaults 1`] = `
"controlShouldRenderValue": true,
"escapeClearsValue": false,
"filterOption": [Function],
"focusDefaultOption": true,
"formatGroupLabel": [Function],
"getOptionLabel": [Function],
"getOptionValue": [Function],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ exports[`defaults > snapshot 1`] = `
controlShouldRenderValue={true}
escapeClearsValue={false}
filterOption={[Function]}
focusDefaultOption={true}
formatGroupLabel={[Function]}
getOptionLabel={[Function]}
getOptionValue={[Function]}
Expand Down