Skip to content
Open
Show file tree
Hide file tree
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
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@ The component will update its display if its `options` list changes length or is

`disabled` - disables each child element if `true`.

`groupProp` - name of the property in each object in `options` in order to
group `<option>` tags with the same group within an `<optgroup>` tag.

`placeholder` - placeholder text to be displayed in the filter `<input>`.

`selectedOptions` - list of option objects which are selected, so should no
Expand Down
31 changes: 27 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export default React.createClass({
classNames: PropTypes.object,
defaultFilter: PropTypes.string,
disabled: PropTypes.bool,
groupProp: PropTypes.string,
placeholder: PropTypes.string,
selectedOptions: PropTypes.array,
size: PropTypes.number,
Expand All @@ -61,6 +62,7 @@ export default React.createClass({
classNames: {},
defaultFilter: '',
disabled: false,
groupProp: false,
placeholder: 'type to filter',
size: 6,
selectedOptions: [],
Expand Down Expand Up @@ -185,8 +187,31 @@ export default React.createClass({

render() {
let {filter, filteredOptions, selectedValues} = this.state
let {className, disabled, placeholder, size, textProp, valueProp} = this.props
let {className, disabled, groupProp, placeholder, size, textProp, valueProp} = this.props
let hasSelectedOptions = selectedValues.length > 0
let options = []
if (groupProp) {
let groups = filteredOptions.reduce((result, option) => {
if (!result[option[groupProp]]) {
result[option[groupProp]] = []
}
result[option[groupProp]].push(option)
return result
}, {})
options = Object.keys(groups).map((group) => {
return (
<optgroup key={group} label={group}>
{groups[group].map((option) => {
return <option key={option[valueProp]} value={option[valueProp]}>{option[textProp]}</option>
})}
</optgroup>
)
})
} else {
options = filteredOptions.map((option) => {
return <option key={option[valueProp]} value={option[valueProp]}>{option[textProp]}</option>
})
}
return <div className={className}>
<input
type="text"
Expand All @@ -205,9 +230,7 @@ export default React.createClass({
onChange={this._updateSelectedValues}
onDoubleClick={this._addSelectedToSelection}
disabled={disabled}>
{filteredOptions.map((option) => {
return <option key={option[valueProp]} value={option[valueProp]}>{option[textProp]}</option>
})}
{options}
</select>
<button type="button"
className={this._getClassName('button', hasSelectedOptions && 'buttonActive')}
Expand Down