Skip to content

Commit

Permalink
feat: indexSelector widget
Browse files Browse the repository at this point in the history
  • Loading branch information
pixelastic committed Sep 8, 2015
1 parent dabe537 commit b60ed36
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 0 deletions.
28 changes: 28 additions & 0 deletions components/IndexSelector/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
var React = require('react');

class IndexSelector extends React.Component {
handleChange(event) {
this.props.setIndex(event.target.value).search();
}

render() {
var currentIndex = this.props.currentIndex;
var indices = this.props.indices;

return (
<select onChange={this.handleChange.bind(this)} value={currentIndex}>
{indices.map(function(index) {
return <option key={index.name} value={index.name}>{index.label}</option>;
})}
</select>
);
}
}

IndexSelector.propTypes = {
currentIndex: React.PropTypes.string,
indices: React.PropTypes.array,
setIndex: React.PropTypes.func
};

module.exports = IndexSelector;
11 changes: 11 additions & 0 deletions example/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,17 @@ search.addWidget(
})
);

search.addWidget(
instantsearch.widgets.indexSelector({
container: '#index-selector',
indices: [
{'name': 'instant_search', 'label': 'Most relevant'},
{'name': 'instant_search_price_asc', 'label': 'Lowest price'},
{'name': 'instant_search_price_desc', 'label': 'Highest price'}
]
})
);

search.addWidget(
instantsearch.widgets.hits({
container: '#hits',
Expand Down
3 changes: 3 additions & 0 deletions example/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ <h1>Instant search demo <small>using instantsearch.js</small></h1>
</div>
<div class="col-md-9">
<div id="stats"></div>
<div>
Sort by: <span id="index-selector"></span>
</div>
<div id="hits"></div>
<div id="pagination" class="text-center"></div>
</div>
Expand Down
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ module.exports = {
InstantSearch: require('./lib/InstantSearch'),
widgets: {
hits: require('./widgets/hits'),
indexSelector: require('./widgets/index-selector'),
menu: require('./widgets/menu'),
multipleChoiceList: require('./widgets/multiple-choice-list'),
pagination: require('./widgets/pagination'),
Expand Down
28 changes: 28 additions & 0 deletions widgets/index-selector/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
var React = require('react');

var utils = require('../../lib/widgetUtils.js');

function indexSelector({container = null, indices = null}) {
var IndexSelector = require('../../components/IndexSelector');
var containerNode = utils.getContainerNode(container);

var usage = 'Usage: indexSelector({container, indices})';
if (container === null || indices === null) {
throw new Error(usage);
}

return {
render: function(results, state, helper) {
React.render(
<IndexSelector
currentIndex={helper.getIndex()}
indices={indices}
setIndex={helper.setIndex.bind(helper)}
/>,
containerNode
);
}
};
}

module.exports = indexSelector;
Empty file.

0 comments on commit b60ed36

Please sign in to comment.