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

[added] Configurable autoHighlightValueMatches function #386

Closed
wants to merge 1 commit into from
Closed
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
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -68,6 +68,12 @@ Default value: `true`
Whether or not to automatically highlight the top match in the dropdown
menu.

#### `autoHighlightValueMatches: Function` (optional)
Default value: `(itemValue, value) => (itemValue.toLowerCase().indexOf(value.toLowerCase()) === 0)`

When `autoHighlight` is true, this is invoked to determine whether the top item
in the dropdown menu should be considered a match.

#### `inputProps: Object` (optional)
Default value: `{}`

14 changes: 10 additions & 4 deletions lib/Autocomplete.js
Original file line number Diff line number Diff line change
@@ -137,6 +137,11 @@ class Autocomplete extends React.Component {
* menu.
*/
autoHighlight: PropTypes.bool,
/**
* Custom function to determine whether the top match in the dropdown should
* highlight
*/
autoHighlightValueMatches: PropTypes.func,
/**
* Whether or not to automatically select the highlighted item when the
* `<input>` loses focus.
@@ -186,6 +191,9 @@ class Autocomplete extends React.Component {
maxHeight: '50%', // TODO: don't cheat, let it flow to the bottom
},
autoHighlight: true,
autoHighlightValueMatches: (itemValue, value) => (itemValue.toLowerCase().indexOf(
value.toLowerCase()
) === 0),
selectOnBlur: false,
onMenuVisibilityChange() {},
}
@@ -391,7 +399,7 @@ class Autocomplete extends React.Component {

maybeAutoCompleteText(state, props) {
const { highlightedIndex } = state
const { value, getItemValue } = props
const { value, getItemValue, autoHighlightValueMatches } = props
let index = highlightedIndex === null ? 0 : highlightedIndex
let items = this.getFilteredItems(props)
for (let i = 0; i < items.length ; i++) {
@@ -402,9 +410,7 @@ class Autocomplete extends React.Component {
const matchedItem = items[index] && props.isItemSelectable(items[index]) ? items[index] : null
if (value !== '' && matchedItem) {
const itemValue = getItemValue(matchedItem)
const itemValueDoesMatch = (itemValue.toLowerCase().indexOf(
value.toLowerCase()
) === 0)
const itemValueDoesMatch = autoHighlightValueMatches(itemValue, value)
if (itemValueDoesMatch) {
return { highlightedIndex: index }
}