Skip to content
This repository was archived by the owner on Mar 4, 2020. It is now read-only.

Commit e2fe1e3

Browse files
authored
fix(Dropdown): Highlights selected option in single selection (#726)
* applied changes on Stardust * bumped downshift version * updated yarn lock * updated changelog * moved the change to fixes * fixed rebase issue in changelog * added explanatory comment on state defaultHighlightedIndex * moved the logic from state reducer to onchange
1 parent 52461b1 commit e2fe1e3

File tree

4 files changed

+17
-7
lines changed

4 files changed

+17
-7
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
4040
### Fixes
4141
- Make `headerMedia` visible for screen readers in `ListItem` @layershifter ([#772](https://github.com/stardust-ui/react/pull/772))
4242
- Cleanup for `Dropdown` examples' accessibility and added localisation example. @silviuavram ([#771](https://github.com/stardust-ui/react/pull/771))
43+
- Fix highlighted selected option in single selection `Dropdown` when opened @silviuavram ([#726](https://github.com/stardust-ui/react/pull/726))
4344

4445
<!--------------------------------[ v0.18.0 ]------------------------------- -->
4546
## [v0.18.0](https://github.com/stardust-ui/react/tree/v0.18.0) (2019-01-24)

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@
6666
"homepage": "https://github.com/stardust-ui/react#readme",
6767
"dependencies": {
6868
"classnames": "^2.2.5",
69-
"downshift": "^3.1.12",
69+
"downshift": "^3.2.0",
7070
"fela": "^10.1.3",
7171
"fela-plugin-fallback-value": "^10.1.3",
7272
"fela-plugin-placeholder-prefixer": "^10.1.3",

src/components/Dropdown/Dropdown.tsx

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ export interface DropdownState {
144144
backspaceDelete: boolean
145145
focused: boolean
146146
searchQuery?: string
147+
defaultHighlightedIndex: number
147148
}
148149

149150
/**
@@ -228,6 +229,8 @@ export default class Dropdown extends AutoControlledComponent<
228229
focused: false,
229230
searchQuery: search ? '' : undefined,
230231
value: multiple ? [] : null,
232+
// used on single selection to open the dropdown with the selected option as highlighted.
233+
defaultHighlightedIndex: !this.props.search && !this.props.multiple ? null : undefined,
231234
}
232235
}
233236

@@ -239,19 +242,20 @@ export default class Dropdown extends AutoControlledComponent<
239242
unhandledProps,
240243
}: RenderResultConfig<DropdownProps>) {
241244
const { search, multiple, getA11yStatusMessage, itemToString, toggleIndicator } = this.props
242-
const { searchQuery } = this.state
245+
const { defaultHighlightedIndex, searchQuery } = this.state
243246

244247
return (
245248
<ElementType className={classes.root} {...unhandledProps}>
246249
<Downshift
247250
onChange={this.handleSelectedChange}
248-
inputValue={search ? searchQuery : undefined}
251+
inputValue={search ? searchQuery : null}
249252
stateReducer={this.handleDownshiftStateChanges}
250253
itemToString={itemToString}
251254
// If it's single search, don't pass anything. Pass a null otherwise, as Downshift does
252255
// not handle selection by default for single/multiple selection and multiple search.
253256
selectedItem={search && !multiple ? undefined : null}
254257
getA11yStatusMessage={getA11yStatusMessage}
258+
defaultHighlightedIndex={defaultHighlightedIndex}
255259
onStateChange={changes => {
256260
if (changes.isOpen && !search) {
257261
this.listRef.current.focus()
@@ -703,6 +707,11 @@ export default class Dropdown extends AutoControlledComponent<
703707
value: newValue,
704708
searchQuery: '',
705709
})
710+
if (!this.props.search && !this.props.multiple) {
711+
this.setState({
712+
defaultHighlightedIndex: this.props.items.indexOf(item),
713+
})
714+
}
706715
if (getA11ySelectionMessage && getA11ySelectionMessage.onAdd) {
707716
this.setA11yStatus(getA11ySelectionMessage.onAdd(item))
708717
}

yarn.lock

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2969,10 +2969,10 @@ dot-prop@^4.1.0:
29692969
dependencies:
29702970
is-obj "^1.0.0"
29712971

2972-
downshift@^3.1.12:
2973-
version "3.1.12"
2974-
resolved "https://registry.yarnpkg.com/downshift/-/downshift-3.1.12.tgz#70da0bfb92711393717206cc008b865e7f9fde4d"
2975-
integrity sha512-dkDlHVIHFT9kFxvPcmlp/kIwV1Uq/aI4rC70nNQkDGZNrpcexfnv4sIVK0hkIuewr9ZNo+Keqcwr3ao476YqJQ==
2972+
downshift@^3.2.0:
2973+
version "3.2.0"
2974+
resolved "https://registry.yarnpkg.com/downshift/-/downshift-3.2.0.tgz#820f40575683d3fc521a9ffd409ef4a69cef531d"
2975+
integrity sha512-IXZ3IB7xU51R04olRGSp8GBmJhPlCL8tW5FjZrJh8e4kB4BGUWsQHZ9tNsO+qNLABieR5DIgYBZ9MncPlwZSDg==
29762976
dependencies:
29772977
"@babel/runtime" "^7.1.2"
29782978
compute-scroll-into-view "^1.0.9"

0 commit comments

Comments
 (0)