Skip to content
This repository was archived by the owner on Jul 19, 2019. It is now read-only.

[fixed] #309 - autoHighlight not working unless match is beginning of… #310

Closed
wants to merge 1 commit into from
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
2 changes: 1 addition & 1 deletion lib/Autocomplete.js
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ class Autocomplete extends React.Component {
const itemValue = getItemValue(matchedItem)
const itemValueDoesMatch = (itemValue.toLowerCase().indexOf(
value.toLowerCase()
) === 0)
) > -1)
Copy link
Author

@dsc8x dsc8x Jan 31, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd argue, that itemValueDoesMatch is even unnecessary.
If an item is displayed because it passed the check of shouldItemRender then we can also auto-highlight it if it's the first item in the list.

if (itemValueDoesMatch) {
return { highlightedIndex: index }
}
Expand Down
2 changes: 1 addition & 1 deletion lib/__tests__/Autocomplete-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ describe('Autocomplete acceptance tests', () => {
it('should highlight top match when `props.value` changes', () => {
const tree = mount(AutocompleteComponentJSX({}))
expect(tree.state('highlightedIndex')).toEqual(null)
tree.setProps({ value: 'a' })
tree.setProps({ value: 'ri' })
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The item we are matching here is Arizona.
In the old test we typed a, which put the Arizona item first and auto-highlighted it.

But if you typed ri then still Arizona was put as first item, but it was not auto-highlighted, since ri does not match the beginning of Arizona.

With the changes in this PR, Arizona will now correctly be highlighted.

expect(tree.state('highlightedIndex')).toEqual(0)
})

Expand Down