| 971 | } |
| 972 | |
| 973 | focusOption(direction: FocusDirection = 'first') { |
| 974 | const { pageSize } = this.props; |
| 975 | const { focusedOption } = this.state; |
| 976 | const options = this.getFocusableOptions(); |
| 977 | |
| 978 | if (!options.length) return; |
| 979 | let nextFocus = 0; // handles 'first' |
| 980 | let focusedIndex = options.indexOf(focusedOption!); |
| 981 | if (!focusedOption) { |
| 982 | focusedIndex = -1; |
| 983 | } |
| 984 | |
| 985 | if (direction === 'up') { |
| 986 | nextFocus = focusedIndex > 0 ? focusedIndex - 1 : options.length - 1; |
| 987 | } else if (direction === 'down') { |
| 988 | nextFocus = (focusedIndex + 1) % options.length; |
| 989 | } else if (direction === 'pageup') { |
| 990 | nextFocus = focusedIndex - pageSize; |
| 991 | if (nextFocus < 0) nextFocus = 0; |
| 992 | } else if (direction === 'pagedown') { |
| 993 | nextFocus = focusedIndex + pageSize; |
| 994 | if (nextFocus > options.length - 1) nextFocus = options.length - 1; |
| 995 | } else if (direction === 'last') { |
| 996 | nextFocus = options.length - 1; |
| 997 | } |
| 998 | this.scrollToFocusedOptionOnUpdate = true; |
| 999 | this.setState({ |
| 1000 | focusedOption: options[nextFocus], |
| 1001 | focusedValue: null, |
| 1002 | focusedOptionId: this.getFocusedOptionId(options[nextFocus]), |
| 1003 | }); |
| 1004 | } |
| 1005 | onChange = ( |
| 1006 | newValue: OnChangeValue<Option, IsMulti>, |
| 1007 | actionMeta: ActionMeta<Option> |