Skip to content

Commit 2bc171e

Browse files
committed
[DDW-620] Focus search input on open
1 parent 1d245b6 commit 2bc171e

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

source/components/Options.js

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import type {
1414

1515
// external libraries
1616
import { isFunction } from 'lodash';
17+
import createRef from 'create-react-ref/lib/createRef';
1718

1819
// internal utility functions
1920
import { createEmptyContext, withTheme } from './HOC/withTheme';
@@ -95,6 +96,8 @@ class OptionsBase extends Component<Props, State> {
9596

9697
const { context, themeId, theme, themeOverrides } = props;
9798

99+
this.searchInputRef = createRef();
100+
98101
this.state = {
99102
composedTheme: composeTheme(
100103
addThemeId(theme || context.theme, themeId),
@@ -115,9 +118,9 @@ class OptionsBase extends Component<Props, State> {
115118
componentDidUpdate(prevProps: Props) {
116119
if (prevProps !== this.props) {
117120
if (!prevProps.isOpen && this.props.isOpen) {
118-
document.addEventListener('keydown', this._handleKeyDown, false);
121+
this.onOpen();
119122
} else if (prevProps.isOpen && !this.props.isOpen) {
120-
document.removeEventListener('keydown', this._handleKeyDown, false);
123+
this.onClose();
121124
}
122125
didThemePropsChange(prevProps, this.props, this.setState.bind(this));
123126
}
@@ -127,6 +130,16 @@ class OptionsBase extends Component<Props, State> {
127130
document.removeEventListener('keydown', this._handleKeyDown, false);
128131
}
129132

133+
onOpen = () => {
134+
document.addEventListener('keydown', this._handleKeyDown, false);
135+
const { current: input } = this.searchInputRef;
136+
if (input && input.focus) input.focus();
137+
}
138+
139+
onClose = () => {
140+
document.removeEventListener('keydown', this._handleKeyDown, false);
141+
}
142+
130143
close = () => {
131144
const { isOpen, onClose, resetOnClose, toggleOpen } = this.props;
132145
if (isOpen && toggleOpen) toggleOpen();
@@ -250,7 +263,7 @@ class OptionsBase extends Component<Props, State> {
250263
// ========= PRIVATE HELPERS =========
251264

252265
_handleSelectionOnKeyDown = (event: SyntheticKeyboardEvent<>) => {
253-
const { options } = this.props;
266+
const options = this.getFilteredOptions();
254267
if (options.length) {
255268
const { isOpeningUpward } = this.props;
256269
const currentIndex = this.state.highlightedOptionIndex;
@@ -371,6 +384,7 @@ class OptionsBase extends Component<Props, State> {
371384
isSelectedOption={this.isSelectedOption}
372385
options={this.getFilteredOptions()}
373386
optionsRef={optionsRef}
387+
searchInputRef={this.searchInputRef}
374388
searchValue={searchValue}
375389
setHighlightedOptionIndex={this.setHighlightedOptionIndex}
376390
targetRef={targetRef}

source/skins/simple/OptionsSkin.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// @flow
22
import React from 'react';
3-
import type { Element, ElementRef } from 'react';
3+
import type { Element, ElementRef, RefObject } from 'react';
44

55
// external libraries
66
import classnames from 'classnames';
@@ -37,6 +37,7 @@ type Props = {
3737
optionsMaxHeight: number,
3838
render: Function,
3939
searchHeight: number,
40+
searchInputRef?: RefObject,
4041
searchValue: string,
4142
selectedOption: any,
4243
setHighlightedOptionIndex: Function,
@@ -150,6 +151,7 @@ export const OptionsSkin = (props: Props) => {
150151
value={searchValue}
151152
onChange={props.onSearch}
152153
autoFocus={true}
154+
inputRef={props.searchInputRef || null}
153155
/>
154156
</div>
155157
)

0 commit comments

Comments
 (0)