Skip to content

Commit

Permalink
Merge pull request #37 from AddSearch/filters-set-sorting
Browse files Browse the repository at this point in the history
Add setSorting on filter change
  • Loading branch information
anttiai authored Jun 15, 2020
2 parents 6c2576e + 205f464 commit cba87c7
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,7 @@ Settings that can be passed to the ```filters``` function:
| type | AddSearchUI.FILTER_TYPE.CHECKBOX_GROUP, AddSearchUI.FILTER_TYPE.RADIO_GROUP, AddSearchUI.FILTER_TYPE.SELECT_LIST, AddSearchUI.FILTER_TYPE.TABS, AddSearchUI.FILTER_TYPE.TAGS, AddSearchUI.FILTER_TYPE.RANGE | n/a | Component's type |
| template | String | [Default templates](https://github.com/AddSearch/search-ui/blob/master/src/components/filters/templates.js) | Override the default template with a custom [Handlebars](https://handlebarsjs.com/) template |
| clearOtherFilters | boolean | false | Clear all other filters when the value of this filter changes. Works with RADIO_GROUP, SELECT_LIST, and TABS filters |
| setSorting | Object | n/a | Set sorting when the value of this filter changes. The object must contain *field* and *order*. Works with RADIO_GROUP, SELECT_LIST, and TABS filters. |
| options | Object | n/a | Object containing filtering options |

The *options* object can contain multiple filtering options. If the type is *Tabs*, *Select list*, or *Radio group*, just one of the
Expand Down
4 changes: 4 additions & 0 deletions examples/components/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,10 @@
containerId: 'tabs-container',
type: AddSearchUI.FILTER_TYPE.TABS,
clearOtherFilters: true,
setSorting: {
field: 'relevance',
order: 'desc'
},
options: {
nofilter: {
label: 'All results'
Expand Down
5 changes: 5 additions & 0 deletions src/components/filters/filters.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
import { FILTER_TYPE } from './index';
import { observeStoreByKey } from '../../store';
import { toggleFilter, setRangeFilter, registerFilter, clearSelected } from '../../actions/filters';
import { sortBy } from '../../actions/sortby';
import { renderToContainer, attachEventListeners, validateContainer } from '../../util/dom';

export const NO_FILTER_NAME = 'nofilter';
Expand Down Expand Up @@ -148,6 +149,10 @@ export default class Filters {
return;
}

if (this.conf.setSorting) {
store.dispatch(sortBy(this.client, this.conf.setSorting.field, this.conf.setSorting.order));
}

// Remove all other filters. Refresh results if there is no next filter
if (this.conf.clearOtherFilters === true) {
store.dispatch(clearSelected(isNoFilter));
Expand Down

0 comments on commit cba87c7

Please sign in to comment.