Created
September 2, 2016 09:47
-
-
Save venelin-mihaylov/43f5d75c1eda7dbf9a3035949e334825 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from 'react' | |
import {Dropdown} from 'stardust' | |
import _ from 'lodash' | |
export default class OsnovaDropdown extends Dropdown { | |
static propTypes = Object.assign(Dropdown.propTypes, { | |
filter: React.PropTypes.func | |
}) | |
getMenuOptions = () => { | |
console.log('getMenuOptions') | |
const {multiple, search, allowAdditions, additionPosition, additionLabel, options, filter} = this.props | |
const {searchQuery, value} = this.state | |
let filteredOptions = options | |
// filter out active options | |
if (multiple) { | |
filteredOptions = _.filter(filteredOptions, opt => !_.includes(value, opt.value)) | |
} | |
// filter by search query | |
if (search && searchQuery) { | |
if (_.isFunction(filter)) { | |
filteredOptions = filter(filteredOptions, searchQuery) | |
} else { | |
const re = new RegExp(_.escapeRegExp(searchQuery), 'i') | |
filteredOptions = _.filter(filteredOptions, (opt) => re.test(opt.text)) | |
} | |
} | |
// insert the "add" item | |
if (allowAdditions && search && searchQuery && !_.some(filteredOptions, {text: searchQuery})) { | |
const addItem = { | |
text: additionLabel ? `${additionLabel} ${searchQuery}` : searchQuery, | |
value: searchQuery, | |
} | |
if (additionPosition === 'top') filteredOptions.unshift(addItem) | |
else filteredOptions.push(addItem) | |
} | |
return filteredOptions | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment