This repository was archived by the owner on Mar 4, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 51
feat(dropdown): control highlightedIndex from Dropdown #966
Merged
Merged
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
3f0c42f
updated Downshift version
ac621fc
controlled the highlightedIndex
55c1b08
fixed a compile issue
194d4e3
fixed wrapping index on key downs
274f419
increased downshift version
72ea9f8
calculates new index from filtered items
be3eb22
fixed a unit test
62a260a
changed the api to provide a boolean value for first item highlight
f29a3f0
added a new line
e1512ef
extracted highlightedIndex logic in separate methods
90d17f9
some changes in the methods
a311be1
adressed comments from Alex
1305cbd
fixed some things in handleStateChange
aadf824
added some unit tests
4b49067
fixed a rollup issue
887be97
added Delete to rollup
b72d01f
Merge branch 'master' into feat/control-highlighted-index
silviuaavram 73cea03
Merge branch 'master' into feat/control-highlighted-index
silviuaavram 7d1f200
Merge branch 'master' into feat/control-highlighted-index
jurokapsiar 7b5b2ca
changelog
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,6 +2,7 @@ import * as React from 'react' | |
| import * as PropTypes from 'prop-types' | ||
| import * as _ from 'lodash' | ||
| import cx from 'classnames' | ||
| import * as keyboardKey from 'keyboard-key' | ||
|
|
||
| import { | ||
| Extendable, | ||
|
|
@@ -29,7 +30,6 @@ import { | |
| handleRef, | ||
| UIComponentProps, | ||
| } from '../../lib' | ||
| import keyboardKey from 'keyboard-key' | ||
| import Indicator, { IndicatorProps } from '../Indicator/Indicator' | ||
| import List from '../List/List' | ||
| import Ref from '../Ref/Ref' | ||
|
|
@@ -67,6 +67,9 @@ export interface DropdownProps extends UIComponentProps<DropdownProps, DropdownS | |
| /** Initial value for 'open' in uncontrolled mode */ | ||
| defaultOpen?: boolean | ||
|
|
||
| /** The initial value for the index of the list item to be highlighted. */ | ||
| defaultHighlightedIndex?: number | ||
|
|
||
| /** The initial value for the search query, if the dropdown is also a search. */ | ||
| defaultSearchQuery?: string | ||
|
|
||
|
|
@@ -96,6 +99,12 @@ export interface DropdownProps extends UIComponentProps<DropdownProps, DropdownS | |
| */ | ||
| getA11yStatusMessage?: (options: DownshiftA11yStatusMessageOptions<ShorthandValue>) => string | ||
|
|
||
| /** A dropdown can open with the first option already highlighted. */ | ||
| highlightFirstItemOnOpen?: boolean | ||
|
|
||
| /** The index of the list item to be highlighted. */ | ||
| highlightedIndex?: number | ||
|
|
||
| /** A dropdown can be formatted to appear inline in the content of other components. */ | ||
| inline?: boolean | ||
|
|
||
|
|
@@ -187,12 +196,12 @@ export interface DropdownProps extends UIComponentProps<DropdownProps, DropdownS | |
| } | ||
|
|
||
| export interface DropdownState { | ||
| activeSelectedIndex: number | ||
| a11ySelectionStatus: string | ||
| defaultHighlightedIndex: number | ||
| activeSelectedIndex: number | ||
| focused: boolean | ||
| open: boolean | ||
| searchQuery: string | ||
| highlightedIndex: number | ||
| value: ShorthandValue | ShorthandCollection | ||
| } | ||
|
|
||
|
|
@@ -225,6 +234,7 @@ class Dropdown extends AutoControlledComponent<Extendable<DropdownProps>, Dropdo | |
| clearIndicator: customPropTypes.itemShorthand, | ||
| defaultActiveSelectedIndex: PropTypes.number, | ||
| defaultOpen: PropTypes.bool, | ||
| defaultHighlightedIndex: PropTypes.number, | ||
| defaultSearchQuery: PropTypes.string, | ||
| defaultValue: PropTypes.oneOfType([ | ||
| customPropTypes.itemShorthand, | ||
|
|
@@ -233,6 +243,8 @@ class Dropdown extends AutoControlledComponent<Extendable<DropdownProps>, Dropdo | |
| fluid: PropTypes.bool, | ||
| getA11ySelectionMessage: PropTypes.object, | ||
| getA11yStatusMessage: PropTypes.func, | ||
| highlightFirstItemOnOpen: PropTypes.bool, | ||
| highlightedIndex: PropTypes.number, | ||
| inline: PropTypes.bool, | ||
| items: customPropTypes.collectionShorthand, | ||
| itemToString: PropTypes.func, | ||
|
|
@@ -273,20 +285,25 @@ class Dropdown extends AutoControlledComponent<Extendable<DropdownProps>, Dropdo | |
| triggerButton: {}, | ||
| } | ||
|
|
||
| static autoControlledProps = ['activeSelectedIndex', 'open', 'searchQuery', 'value'] | ||
| static autoControlledProps = [ | ||
| 'activeSelectedIndex', | ||
| 'highlightedIndex', | ||
| 'open', | ||
| 'searchQuery', | ||
| 'value', | ||
| ] | ||
|
|
||
| static Item = DropdownItem | ||
| static SearchInput = DropdownSearchInput | ||
| static SelectedItem = DropdownSelectedItem | ||
|
|
||
| getInitialAutoControlledState({ multiple, search }: DropdownProps): DropdownState { | ||
| return { | ||
| activeSelectedIndex: multiple ? null : undefined, | ||
| a11ySelectionStatus: '', | ||
| // used on single selection to open the dropdown with the selected option as highlighted. | ||
| defaultHighlightedIndex: this.props.multiple ? undefined : null, | ||
| activeSelectedIndex: multiple ? null : undefined, | ||
| focused: false, | ||
| open: false, | ||
| highlightedIndex: this.props.highlightFirstItemOnOpen ? 0 : null, | ||
| searchQuery: search ? '' : undefined, | ||
| value: multiple ? [] : null, | ||
| } | ||
|
|
@@ -309,7 +326,7 @@ class Dropdown extends AutoControlledComponent<Extendable<DropdownProps>, Dropdo | |
| itemToString, | ||
| toggleIndicator, | ||
| } = this.props | ||
| const { defaultHighlightedIndex, open, searchQuery, value } = this.state | ||
| const { highlightedIndex, open, searchQuery, value } = this.state | ||
|
|
||
| return ( | ||
| <ElementType className={classes.root} {...unhandledProps}> | ||
|
|
@@ -322,7 +339,7 @@ class Dropdown extends AutoControlledComponent<Extendable<DropdownProps>, Dropdo | |
| itemToString={itemToString} | ||
| selectedItem={null} | ||
| getA11yStatusMessage={getA11yStatusMessage} | ||
| defaultHighlightedIndex={defaultHighlightedIndex} | ||
| highlightedIndex={highlightedIndex} | ||
| onStateChange={this.handleStateChange} | ||
| > | ||
| {({ | ||
|
|
@@ -632,11 +649,25 @@ class Dropdown extends AutoControlledComponent<Extendable<DropdownProps>, Dropdo | |
|
|
||
| private handleStateChange = (changes: StateChangeOptions<ShorthandValue>) => { | ||
| if (changes.isOpen !== undefined && changes.isOpen !== this.state.open) { | ||
| this.trySetStateAndInvokeHandler('onOpenChange', null, { open: changes.isOpen }) | ||
| const newState = { open: changes.isOpen, highlightedIndex: this.state.highlightedIndex } | ||
|
|
||
| if (changes.isOpen) { | ||
| const highlightedIndexOnArrowKeyOpen = this.getHighlightedIndexOnArrowKeyOpen(changes) | ||
| if (_.isNumber(highlightedIndexOnArrowKeyOpen)) { | ||
| newState.highlightedIndex = highlightedIndexOnArrowKeyOpen | ||
| } | ||
| if (!this.props.search) { | ||
| this.listRef.current.focus() | ||
| } | ||
| } else { | ||
| newState.highlightedIndex = this.getHighlightedIndexOnClose() | ||
| } | ||
|
|
||
| this.trySetStateAndInvokeHandler('onOpenChange', null, newState) | ||
| } | ||
|
|
||
| if (changes.isOpen && !this.props.search) { | ||
| this.listRef.current.focus() | ||
| if (this.state.open && _.isNumber(changes.highlightedIndex)) { | ||
| this.trySetState({ highlightedIndex: changes.highlightedIndex }) | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -803,21 +834,20 @@ class Dropdown extends AutoControlledComponent<Extendable<DropdownProps>, Dropdo | |
| private handleClear = (e: React.SyntheticEvent<HTMLElement>) => { | ||
| const { | ||
| activeSelectedIndex, | ||
| defaultHighlightedIndex, | ||
| highlightedIndex, | ||
| searchQuery, | ||
| value, | ||
| } = this.getInitialAutoControlledState(this.props) | ||
|
|
||
| _.invoke(this.props, 'onSelectedChange', e, { | ||
| ...this.props, | ||
| activeSelectedIndex, | ||
| defaultHighlightedIndex, | ||
| highlightedIndex, | ||
| searchQuery, | ||
| value, | ||
| }) | ||
|
|
||
| this.trySetState({ activeSelectedIndex, searchQuery, value }) | ||
| this.setState({ defaultHighlightedIndex }) | ||
| this.trySetState({ activeSelectedIndex, highlightedIndex, searchQuery, value }) | ||
|
|
||
| this.tryFocusSearchInput() | ||
| this.tryFocusTriggerButton() | ||
|
|
@@ -878,7 +908,7 @@ class Dropdown extends AutoControlledComponent<Extendable<DropdownProps>, Dropdo | |
| }) | ||
|
|
||
| if (!multiple) { | ||
| this.setState({ defaultHighlightedIndex: items.indexOf(item) }) | ||
| this.setState({ highlightedIndex: items.indexOf(item) }) | ||
| } | ||
|
|
||
| if (getA11ySelectionMessage && getA11ySelectionMessage.onAdd) { | ||
|
|
@@ -1021,6 +1051,48 @@ class Dropdown extends AutoControlledComponent<Extendable<DropdownProps>, Dropdo | |
| private isValueEmpty = (value: ShorthandValue | ShorthandCollection) => { | ||
| return _.isArray(value) ? value.length < 1 : !value | ||
| } | ||
|
|
||
| private getHighlightedIndexOnArrowKeyOpen = ( | ||
| changes: StateChangeOptions<ShorthandValue>, | ||
| ): number => { | ||
| const itemsLength = this.getItemsFilteredBySearchQuery().length | ||
| switch (changes.type) { | ||
| // if open by ArrowUp, index should change by -1. | ||
| case Downshift.stateChangeTypes.keyDownArrowUp: | ||
| if (_.isNumber(this.state.highlightedIndex)) { | ||
| const newIndex = this.state.highlightedIndex - 1 | ||
| return newIndex < 0 ? itemsLength - 1 : newIndex | ||
| } | ||
| return itemsLength - 1 | ||
| // if open by ArrowDown, index should change by +1. | ||
| case Downshift.stateChangeTypes.keyDownArrowDown: | ||
| if (_.isNumber(this.state.highlightedIndex)) { | ||
| const newIndex = this.state.highlightedIndex + 1 | ||
| return newIndex >= itemsLength ? 0 : newIndex | ||
| } | ||
| return 0 | ||
| default: | ||
| return undefined | ||
| } | ||
| } | ||
|
|
||
| private getHighlightedIndexOnClose = (): number => { | ||
|
||
| const { highlightFirstItemOnOpen, items, multiple, search } = this.props | ||
| const { value } = this.state | ||
|
|
||
| if (!multiple && !search && value) { | ||
| // in single selection, if there is a selected item, highlight it. | ||
| return items.indexOf(value) | ||
| } | ||
|
|
||
| if (highlightFirstItemOnOpen) { | ||
| // otherwise, if highlightFirstItemOnOpen prop is provied, highlight first item. | ||
| return 0 | ||
| } | ||
|
|
||
| // otherwise, highlight no item. | ||
| return null | ||
| } | ||
| } | ||
|
|
||
| Dropdown.slotClassNames = { | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, but I lost there 😴 If you will try to cover this code with UTs you will not survive 🦇
highlightedIndexbased on changes/props?componentDidUpdate()? I.e. in a single place based on the component's state.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I extracted the code, you can take a look. as for the DOM effects, it's out of scope, but we can create a separate issue. I also want to move the methods around in the Dropdown, since it's kind of chaos there. should be render, then renderWhatevers, then event handlers, then helpers. Or something like that, but grouped better, since it's a 1100 line file.