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
18 changes: 14 additions & 4 deletions src/Select.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ export type Props = {
'aria-labelledby'?: string,
/* Focus the control when it is mounted */
autoFocus?: boolean,
/* Focus first focusable option on menu open and when user is typing */
autoFocusOption: boolean,
/* Remove the currently focused option when the user presses backspace */
backspaceRemovesValue: boolean,
/* Remove focus from the input when the user selects an option (handy for dismissing the keyboard on touch devices) */
Expand Down Expand Up @@ -276,6 +278,7 @@ export const defaultProps = {
noOptionsMessage: () => 'No options',
openMenuOnFocus: false,
openMenuOnClick: true,
autoFocusOption: true,
options: [],
pageSize: 5,
placeholder: 'Select...',
Expand Down Expand Up @@ -399,8 +402,14 @@ export default class Select extends Component<Props, State> {
) {
const selectValue = cleanValue(nextProps.value);
const menuOptions = this.buildMenuOptions(nextProps, selectValue);
const focusedValue = this.getNextFocusedValue(selectValue);
const focusedOption = this.getNextFocusedOption(menuOptions.focusable);
const { focusedOption, focusedValue } =
nextProps.autoFocusOption === false && this.state.focusedOption === null
? { focusedValue: null, focusedOption: null }
: {
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
Expand Down Expand Up @@ -481,7 +490,8 @@ export default class Select extends Component<Props, State> {

openMenu(focusOption: 'first' | 'last') {
const { menuOptions, selectValue, isFocused } = this.state;
const { isMulti } = this.props;
const { isMulti, autoFocusOption } = this.props;

let openAtIndex =
focusOption === 'first' ? 0 : menuOptions.focusable.length - 1;

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

this.announceAriaLiveContext({ event: 'menu' });
Expand Down
9 changes: 9 additions & 0 deletions src/__tests__/__snapshots__/Async.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ exports[`defaults - snapshot 1`] = `
options={Array []}
>
<Select
autoFocusOption={true}
backspaceRemovesValue={true}
blurInputOnSelect={true}
cacheOptions={false}
Expand Down Expand Up @@ -83,6 +84,7 @@ exports[`defaults - snapshot 1`] = `
selectOption={[Function]}
selectProps={
Object {
"autoFocusOption": true,
"backspaceRemovesValue": true,
"blurInputOnSelect": true,
"cacheOptions": false,
Expand Down Expand Up @@ -186,6 +188,7 @@ exports[`defaults - snapshot 1`] = `
selectOption={[Function]}
selectProps={
Object {
"autoFocusOption": true,
"backspaceRemovesValue": true,
"blurInputOnSelect": true,
"cacheOptions": false,
Expand Down Expand Up @@ -281,6 +284,7 @@ exports[`defaults - snapshot 1`] = `
selectOption={[Function]}
selectProps={
Object {
"autoFocusOption": true,
"backspaceRemovesValue": true,
"blurInputOnSelect": true,
"cacheOptions": false,
Expand Down Expand Up @@ -376,6 +380,7 @@ exports[`defaults - snapshot 1`] = `
selectOption={[Function]}
selectProps={
Object {
"autoFocusOption": true,
"backspaceRemovesValue": true,
"blurInputOnSelect": true,
"cacheOptions": false,
Expand Down Expand Up @@ -475,6 +480,7 @@ exports[`defaults - snapshot 1`] = `
onFocus={[Function]}
selectProps={
Object {
"autoFocusOption": true,
"backspaceRemovesValue": true,
"blurInputOnSelect": true,
"cacheOptions": false,
Expand Down Expand Up @@ -656,6 +662,7 @@ exports[`defaults - snapshot 1`] = `
selectOption={[Function]}
selectProps={
Object {
"autoFocusOption": true,
"backspaceRemovesValue": true,
"blurInputOnSelect": true,
"cacheOptions": false,
Expand Down Expand Up @@ -750,6 +757,7 @@ exports[`defaults - snapshot 1`] = `
selectOption={[Function]}
selectProps={
Object {
"autoFocusOption": true,
"backspaceRemovesValue": true,
"blurInputOnSelect": true,
"cacheOptions": false,
Expand Down Expand Up @@ -852,6 +860,7 @@ exports[`defaults - snapshot 1`] = `
selectOption={[Function]}
selectProps={
Object {
"autoFocusOption": true,
"backspaceRemovesValue": true,
"blurInputOnSelect": true,
"cacheOptions": false,
Expand Down
9 changes: 9 additions & 0 deletions src/__tests__/__snapshots__/AsyncCreatable.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ exports[`defaults - snapshot 1`] = `
>
<Select
allowCreateWhileLoading={false}
autoFocusOption={true}
backspaceRemovesValue={true}
blurInputOnSelect={true}
cacheOptions={false}
Expand Down Expand Up @@ -108,6 +109,7 @@ exports[`defaults - snapshot 1`] = `
selectProps={
Object {
"allowCreateWhileLoading": false,
"autoFocusOption": true,
"backspaceRemovesValue": true,
"blurInputOnSelect": true,
"cacheOptions": false,
Expand Down Expand Up @@ -216,6 +218,7 @@ exports[`defaults - snapshot 1`] = `
selectProps={
Object {
"allowCreateWhileLoading": false,
"autoFocusOption": true,
"backspaceRemovesValue": true,
"blurInputOnSelect": true,
"cacheOptions": false,
Expand Down Expand Up @@ -316,6 +319,7 @@ exports[`defaults - snapshot 1`] = `
selectProps={
Object {
"allowCreateWhileLoading": false,
"autoFocusOption": true,
"backspaceRemovesValue": true,
"blurInputOnSelect": true,
"cacheOptions": false,
Expand Down Expand Up @@ -416,6 +420,7 @@ exports[`defaults - snapshot 1`] = `
selectProps={
Object {
"allowCreateWhileLoading": false,
"autoFocusOption": true,
"backspaceRemovesValue": true,
"blurInputOnSelect": true,
"cacheOptions": false,
Expand Down Expand Up @@ -520,6 +525,7 @@ exports[`defaults - snapshot 1`] = `
selectProps={
Object {
"allowCreateWhileLoading": false,
"autoFocusOption": true,
"backspaceRemovesValue": true,
"blurInputOnSelect": true,
"cacheOptions": false,
Expand Down Expand Up @@ -706,6 +712,7 @@ exports[`defaults - snapshot 1`] = `
selectProps={
Object {
"allowCreateWhileLoading": false,
"autoFocusOption": true,
"backspaceRemovesValue": true,
"blurInputOnSelect": true,
"cacheOptions": false,
Expand Down Expand Up @@ -805,6 +812,7 @@ exports[`defaults - snapshot 1`] = `
selectProps={
Object {
"allowCreateWhileLoading": false,
"autoFocusOption": true,
"backspaceRemovesValue": true,
"blurInputOnSelect": true,
"cacheOptions": false,
Expand Down Expand Up @@ -912,6 +920,7 @@ exports[`defaults - snapshot 1`] = `
selectProps={
Object {
"allowCreateWhileLoading": false,
"autoFocusOption": true,
"backspaceRemovesValue": true,
"blurInputOnSelect": true,
"cacheOptions": false,
Expand Down
8 changes: 8 additions & 0 deletions src/__tests__/__snapshots__/Select.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ exports[`snapshot - defaults 1`] = `
selectOption={[Function]}
selectProps={
Object {
"autoFocusOption": true,
"backspaceRemovesValue": true,
"blurInputOnSelect": true,
"captureMenuScroll": false,
Expand Down Expand Up @@ -112,6 +113,7 @@ exports[`snapshot - defaults 1`] = `
selectOption={[Function]}
selectProps={
Object {
"autoFocusOption": true,
"backspaceRemovesValue": true,
"blurInputOnSelect": true,
"captureMenuScroll": false,
Expand Down Expand Up @@ -194,6 +196,7 @@ exports[`snapshot - defaults 1`] = `
selectOption={[Function]}
selectProps={
Object {
"autoFocusOption": true,
"backspaceRemovesValue": true,
"blurInputOnSelect": true,
"captureMenuScroll": false,
Expand Down Expand Up @@ -278,6 +281,7 @@ exports[`snapshot - defaults 1`] = `
selectOption={[Function]}
selectProps={
Object {
"autoFocusOption": true,
"backspaceRemovesValue": true,
"blurInputOnSelect": true,
"captureMenuScroll": false,
Expand Down Expand Up @@ -365,6 +369,7 @@ exports[`snapshot - defaults 1`] = `
onFocus={[Function]}
selectProps={
Object {
"autoFocusOption": true,
"backspaceRemovesValue": true,
"blurInputOnSelect": true,
"captureMenuScroll": false,
Expand Down Expand Up @@ -450,6 +455,7 @@ exports[`snapshot - defaults 1`] = `
selectOption={[Function]}
selectProps={
Object {
"autoFocusOption": true,
"backspaceRemovesValue": true,
"blurInputOnSelect": true,
"captureMenuScroll": false,
Expand Down Expand Up @@ -533,6 +539,7 @@ exports[`snapshot - defaults 1`] = `
selectOption={[Function]}
selectProps={
Object {
"autoFocusOption": true,
"backspaceRemovesValue": true,
"blurInputOnSelect": true,
"captureMenuScroll": false,
Expand Down Expand Up @@ -623,6 +630,7 @@ exports[`snapshot - defaults 1`] = `
selectOption={[Function]}
selectProps={
Object {
"autoFocusOption": true,
"backspaceRemovesValue": true,
"blurInputOnSelect": true,
"captureMenuScroll": false,
Expand Down
1 change: 1 addition & 0 deletions src/__tests__/__snapshots__/StateManaged.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

exports[`defaults > snapshot 1`] = `
<Select
autoFocusOption={true}
backspaceRemovesValue={true}
blurInputOnSelect={true}
captureMenuScroll={false}
Expand Down