fmoo / react-typeahead

Pure react-based typeahead and typeahead-tokenizer
ISC License
677 stars 232 forks source link

How to implement search matching sort function? #252

Open invious opened 6 years ago

invious commented 6 years ago

I have this function which

    _handleSort = (a, b, input_string) => {
        let distance = this.levenshtein.get(a.name, input_string) - this.levenshtein.get(b.name, input_string)
        console.log(`a:${a.name}, b:${b.name}, input:${input_string}`)
        return distance;
    }

How can I make it so that the results are sorted by this function as I type in the AsyncTypeAhead

invious commented 6 years ago

something like this?

<AsyncTypeahead
                    onSearch={this._handleSearch}
                    searchOptions={this._handleSort}
    ...

_handleSort = (value, options) => {
        let sorter = (a, b) => {
            let distance = this.levenshtein.get(a.name, value) - this.levenshtein.get(b.name, value)
            console.log(`a:${a.name}, b:${b.name}, input:${value}`)
            return distance;
        }
        return options.sort(sorter)
    }

it doesn't seem like the sorter function is ever getting called