Skip to content

Commit

Permalink
feat(Dropdown): support custom search function (#469) (#484)
Browse files Browse the repository at this point in the history
  • Loading branch information
dylankiss authored and levithomason committed Sep 9, 2016
1 parent d471306 commit ab0c13d
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 6 deletions.
19 changes: 13 additions & 6 deletions src/modules/Dropdown/Dropdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,15 @@ export default class Dropdown extends Component {
/** Message to display when there are no results. */
noResultsMessage: PropTypes.string,

/** Define whether the highlighted item should be selected on blur */
/** Define whether the highlighted item should be selected on blur. */
selectOnBlur: PropTypes.bool,

/** Make the dropdown options searchable by substring matching (default) or with a custom search function. */
search: PropTypes.oneOfType([
PropTypes.bool,
PropTypes.func,
]),

// ------------------------------------
// Callbacks
// ------------------------------------
Expand Down Expand Up @@ -185,9 +191,6 @@ export default class Dropdown extends Component {
PropTypes.oneOf(_meta.props.pointing),
]),

/** Make the dropdown options searchable. */
search: PropTypes.bool,

/** The text displayed in the dropdown, usually for the active item. */
text: PropTypes.string,

Expand Down Expand Up @@ -538,8 +541,12 @@ export default class Dropdown extends Component {

// filter by search query
if (search && searchQuery) {
const re = new RegExp(_.escapeRegExp(searchQuery), 'i')
filteredOptions = _.filter(filteredOptions, (opt) => re.test(opt.text))
if (_.isFunction(search)) {
filteredOptions = search(filteredOptions, searchQuery)
} else {
const re = new RegExp(_.escapeRegExp(searchQuery), 'i')
filteredOptions = _.filter(filteredOptions, (opt) => re.test(opt.text))
}
}

// insert the "add" item
Expand Down
17 changes: 17 additions & 0 deletions test/specs/modules/Dropdown/Dropdown-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1027,6 +1027,23 @@ describe('Dropdown Component', () => {
.should.have.lengthOf(1, "Searching for an item's text did not yield any results.")
})

it('filters the items based on custom search function', () => {
const searchFunction = sandbox.stub().returns(options.slice(0, 2))
const search = wrapperMount(<Dropdown options={options} selection search={searchFunction} />)
.find('input.search')
const searchQuery = '__nonExistingSearchQuery__'

// search for value yields 2 results as per our custom search function
search.simulate('change', { target: { value: searchQuery } })

searchFunction.should.have.been.calledOnce()
searchFunction.should.have.been.calledWithMatch(options, searchQuery)

wrapper
.find('DropdownItem')
.should.have.lengthOf(2, 'Searching with custom search function did not yield 2 results.')
})

it('sets the selected item to the first search result', () => {
const search = wrapperMount(<Dropdown options={options} selection search />)
.find('input.search')
Expand Down

0 comments on commit ab0c13d

Please sign in to comment.