Skip to content

Commit

Permalink
Merge b770eac into b340fd6
Browse files Browse the repository at this point in the history
  • Loading branch information
i-like-robots committed May 13, 2019
2 parents b340fd6 + b770eac commit 19f1292
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 10 deletions.
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,21 @@ const suggestions = [

#### suggestionsFilter (optional)

A function to filter suggestion items on; takes a suggestion `item` as the single argument.
A callback function to filter suggestion items with. The callback receives two arguments; a `suggestion` and the current `query` and must return a boolean value.

If no function is supplied the default filter is applied. Default: `null`.

Example of a function which returns items containing `query`:

```js
import stringScore from 'string-score'

function suggestionsFilter(item, query) {
const score = stringScore(item.name, query)
return score > 0.5
}
```

#### placeholder (optional)

The placeholder string shown for the input. Default: `'Add new tag'`.
Expand Down
2 changes: 1 addition & 1 deletion lib/Suggestions.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ function filterSuggestions (query, suggestions, length, suggestionsFilter) {
suggestionsFilter = (item) => regex.test(item.name)
}

return suggestions.filter(suggestionsFilter).slice(0, length)
return suggestions.filter((item) => suggestionsFilter(item, query)).slice(0, length)
}

class Suggestions extends React.Component {
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
"react-dom": "^16.8.0",
"sinon": "^7.3.0",
"standard": "^12.0.1",
"string-score": "^1.0.1",
"webpack": "^3.6.0",
"webpack-dev-server": "^2.8.2"
}
Expand Down
16 changes: 8 additions & 8 deletions spec/ReactTags.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const React = require('react')
const ReactDOM = require('react-dom')
const TestUtils = require('react-dom/test-utils')
const keycode = require('keycode')
const stringScore = require('string-score')
const sinon = require('sinon')
const fixture = require('../example/countries')
const Subject = require('../dist-es5/ReactTags')
Expand Down Expand Up @@ -268,20 +269,19 @@ describe('React Tags', () => {
})
})

it('uses provided suggestionsFilter', () => {
let expectedName = fixture[0].name

it('uses provided suggestionsFilter callback', () => {
createInstance({
minQueryLength: 3,
suggestions: fixture,
suggestionsFilter: (item) => (item.name === expectedName)
suggestionsFilter: (item, currentQuery) => {
const score = stringScore(item.name, currentQuery)
return score > 0.4
}
})

type(query)
type('uni')

$$('li[role="option"]').forEach((option) => {
expect(option.textContent).toMatch(expectedName)
})
expect($$('li[role="option"]').length).toEqual(5)
})

it('can handle non-ascii characters', () => {
Expand Down

0 comments on commit 19f1292

Please sign in to comment.