Skip to content

Commit b60ed36

Browse files
committed
feat: indexSelector widget
1 parent dabe537 commit b60ed36

File tree

6 files changed

+71
-0
lines changed

6 files changed

+71
-0
lines changed

components/IndexSelector/index.js

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
var React = require('react');
2+
3+
class IndexSelector extends React.Component {
4+
handleChange(event) {
5+
this.props.setIndex(event.target.value).search();
6+
}
7+
8+
render() {
9+
var currentIndex = this.props.currentIndex;
10+
var indices = this.props.indices;
11+
12+
return (
13+
<select onChange={this.handleChange.bind(this)} value={currentIndex}>
14+
{indices.map(function(index) {
15+
return <option key={index.name} value={index.name}>{index.label}</option>;
16+
})}
17+
</select>
18+
);
19+
}
20+
}
21+
22+
IndexSelector.propTypes = {
23+
currentIndex: React.PropTypes.string,
24+
indices: React.PropTypes.array,
25+
setIndex: React.PropTypes.func
26+
};
27+
28+
module.exports = IndexSelector;

example/app.js

+11
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,17 @@ search.addWidget(
2222
})
2323
);
2424

25+
search.addWidget(
26+
instantsearch.widgets.indexSelector({
27+
container: '#index-selector',
28+
indices: [
29+
{'name': 'instant_search', 'label': 'Most relevant'},
30+
{'name': 'instant_search_price_asc', 'label': 'Lowest price'},
31+
{'name': 'instant_search_price_desc', 'label': 'Highest price'}
32+
]
33+
})
34+
);
35+
2536
search.addWidget(
2637
instantsearch.widgets.hits({
2738
container: '#hits',

example/index.html

+3
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ <h1>Instant search demo <small>using instantsearch.js</small></h1>
4040
</div>
4141
<div class="col-md-9">
4242
<div id="stats"></div>
43+
<div>
44+
Sort by: <span id="index-selector"></span>
45+
</div>
4346
<div id="hits"></div>
4447
<div id="pagination" class="text-center"></div>
4548
</div>

index.js

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ module.exports = {
22
InstantSearch: require('./lib/InstantSearch'),
33
widgets: {
44
hits: require('./widgets/hits'),
5+
indexSelector: require('./widgets/index-selector'),
56
menu: require('./widgets/menu'),
67
multipleChoiceList: require('./widgets/multiple-choice-list'),
78
pagination: require('./widgets/pagination'),

widgets/index-selector/index.js

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
var React = require('react');
2+
3+
var utils = require('../../lib/widgetUtils.js');
4+
5+
function indexSelector({container = null, indices = null}) {
6+
var IndexSelector = require('../../components/IndexSelector');
7+
var containerNode = utils.getContainerNode(container);
8+
9+
var usage = 'Usage: indexSelector({container, indices})';
10+
if (container === null || indices === null) {
11+
throw new Error(usage);
12+
}
13+
14+
return {
15+
render: function(results, state, helper) {
16+
React.render(
17+
<IndexSelector
18+
currentIndex={helper.getIndex()}
19+
indices={indices}
20+
setIndex={helper.setIndex.bind(helper)}
21+
/>,
22+
containerNode
23+
);
24+
}
25+
};
26+
}
27+
28+
module.exports = indexSelector;

widgets/index-selector/template.html

Whitespace-only changes.

0 commit comments

Comments
 (0)