mirror of
https://github.com/naturalcrit/homebrewery.git
synced 2026-01-02 23:42:44 +00:00
add ability to filter options on any number of attributes
This commit is contained in:
@@ -2,6 +2,7 @@ const React = require('react');
|
|||||||
const createClass = require('create-react-class');
|
const createClass = require('create-react-class');
|
||||||
const _ = require('lodash');
|
const _ = require('lodash');
|
||||||
const cx = require('classnames');
|
const cx = require('classnames');
|
||||||
|
const { filter } = require('lodash');
|
||||||
require('./combobox.less');
|
require('./combobox.less');
|
||||||
|
|
||||||
const Combobox = createClass({
|
const Combobox = createClass({
|
||||||
@@ -15,7 +16,7 @@ const Combobox = createClass({
|
|||||||
autoSuggest : {
|
autoSuggest : {
|
||||||
clearAutoSuggestOnClick : true,
|
clearAutoSuggestOnClick : true,
|
||||||
suggestMethod : 'includes',
|
suggestMethod : 'includes',
|
||||||
filterOn : 'data-value' // should allow as array to filter on multiple attributes, or even custom filter
|
filterOn : [] // should allow as array to filter on multiple attributes, or even custom filter
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
@@ -81,23 +82,23 @@ const Combobox = createClass({
|
|||||||
},
|
},
|
||||||
renderDropdown : function(dropdownChildren){
|
renderDropdown : function(dropdownChildren){
|
||||||
if(!this.state.showDropdown) return null;
|
if(!this.state.showDropdown) return null;
|
||||||
|
|
||||||
if(this.props.autoSuggest && !this.state.inputFocused){
|
if(this.props.autoSuggest && !this.state.inputFocused){
|
||||||
const suggestMethod = this.props.autoSuggest.suggestMethod;
|
const suggestMethod = this.props.autoSuggest.suggestMethod;
|
||||||
const filterOn = this.props.autoSuggest.filterOn;
|
const filterOn = this.props.autoSuggest.filterOn;
|
||||||
dropdownChildren = dropdownChildren.map((item)=>({
|
|
||||||
...item,
|
|
||||||
value : item.props[filterOn]
|
|
||||||
}));
|
|
||||||
if(suggestMethod === 'includes'){
|
|
||||||
dropdownChildren = dropdownChildren.filter((item)=>item.value.includes(this.state.value));
|
|
||||||
} else if(suggestMethod === 'sequential'){
|
|
||||||
dropdownChildren = dropdownChildren.filter((item)=>item.value.startsWith(this.state.value));
|
|
||||||
}
|
|
||||||
|
|
||||||
|
const filteredArrays = filterOn.map((attr)=>{
|
||||||
|
const children = dropdownChildren.filter((item)=>{
|
||||||
|
if(suggestMethod === 'includes'){
|
||||||
|
return item.props[attr].toLowerCase().includes(this.state.value.toLowerCase());
|
||||||
|
} else if(suggestMethod === 'startsWith'){
|
||||||
|
return item.props[attr].toLowerCase().startsWith(this.state.value.toLowerCase());
|
||||||
|
}
|
||||||
|
});
|
||||||
|
return children;
|
||||||
|
});
|
||||||
|
dropdownChildren = _.uniq(filteredArrays.flat(1));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className='dropdown-options'>
|
<div className='dropdown-options'>
|
||||||
{dropdownChildren}
|
{dropdownChildren}
|
||||||
|
|||||||
@@ -244,9 +244,9 @@ const MetadataEditor = createClass({
|
|||||||
onEntry={(e)=>{this.handleFieldChange('lang', e);}}
|
onEntry={(e)=>{this.handleFieldChange('lang', e);}}
|
||||||
options={listLanguages()}
|
options={listLanguages()}
|
||||||
autoSuggest={{
|
autoSuggest={{
|
||||||
suggestMethod : 'includes',
|
suggestMethod : 'startsWith',
|
||||||
clearAutoSuggestOnClick : true,
|
clearAutoSuggestOnClick : true,
|
||||||
filterOn : 'data-value'
|
filterOn : ['data-value', 'data-detail', 'title']
|
||||||
}}>
|
}}>
|
||||||
</Combobox>
|
</Combobox>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
Reference in New Issue
Block a user